S
S
Shikita2020-05-02 02:47:10
Unity
Shikita, 2020-05-02 02:47:10

How to implement a health boost system in Unity?

In general, the maximum value of the fill amount in unity can be 1, and the minimum is 0. how to make this limit more than the amount of health?

Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthTest : MonoBehaviour
{
    public float curHealth; // Здоровье
    public Text healthtext; // Кол-во здоровья
    public Transform spawn ; // место, на котором появляется после смерти
    public float maxHealth = 300f;
    public Image bar; // Картинка здоровья
    GameObject Player; // Игрок, к которому привязано здоровье
    private float showTxt; // число, показанное в игре

    void Start()
    {
        GameObject Player = GameObject.FindGameObjectWithTag("Player"); // Инициализация игрока
        curHealth = bar.fillAmount ; // Инициализация здоровья как числа
        showTxt = curHealth * 100;  // Инициализация Кол-во здоровья, показанного в игре 
    }

    // Update is called once per frame
    void Update()
    {
        healthtext.text = curHealth.ToString("0");  // Перевод в string, и отображение в самой игре

        if (curHealth  >= 1f)
        {
            curHealth = 1f;
        }
        if (curHealth <= 0)
        {
            curHealth = 0;
            Death();
        }
    }
    void Death() // Смерть
    {
        curHealth = 1f;
        Player.transform.position = spawn.transform.position;
    }
    public void TakeDamage(float damage) // Урон
    {
        curHealth -= damage;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag.Equals("Ground") && Player_Control.rb.velocity.y < -85) // Смерть падении
        {
            Death();
        }
        if (collision.gameObject.tag.Equals("Ground") && Player_Control.rb.velocity.y <= -60 && Player_Control.rb.velocity.y >= -85) // Урон при падении
        {
            TakeDamage(0.5f); 
            //звук приземления(с потерей хп)
        }
    }
}

5eacb4dc21e75858062969.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-05-02
@Shikita

Normalize the parameter.
https://habr.com/ru/post/131931/
And specifically your case
https://answers.unity.com/questions/275638/how-to-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question