Answer the question
In order to leave comments, you need to log in
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?
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);
//звук приземления(с потерей хп)
}
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question