- Home /
Question by
mdddarwin · Mar 08, 2019 at 04:15 PM ·
c#unity 5scripting problem
problem with health pick up its going above the max health
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CharacterStats : MonoBehaviour
{
private CharacterController characterController { get { return GetComponent<CharacterController>(); } set { characterController = value; } }
private RagdollManager ragdollManager { get { return GetComponentInChildren<RagdollManager>(); } set { ragdollManager = value; } }
public PlayerUI UI { get { return GetComponent<PlayerUI>(); } set { UI = value; } }
private CharacterStats stats;
private Weapon wp { get { return GetComponent<Weapon>(); } set { wp = value; } }
private GameController gc{get{return GetComponent<GameController>();} set {gc = value;}}
[Range(0, 100)]
public int CurrentHealth = 100;
public int Highheal = 100;
public int Midheal = 50;
public int Lowheal = 25;
public int MaxHealth = 100;
public int faction;
public MonoBehaviour[] scriptsToDisable;
public Image damageImage;
public Image healImage;
public float healFlashSpeed = 10f;
public Color healFlashColor = new Color(1f, 0f, 0f, 1f);
public float flashSpeed = 5f;
public Color flashColor = new Color(1f, 0f, 0f, 0.5f);
bool damage;
bool heal;
public Color LowHealth = Color.red;
public Color FullHealth = Color.green;
void Start()
{
CurrentHealth = Mathf.Clamp(CurrentHealth, 0, 100);
}
void Update()
{
if (gameObject.tag == "Player")
{
if (heal)
{
healImage.color = healFlashColor;
}
else
{
healImage.color = Color.Lerp(healImage.color, Color.clear, healFlashSpeed * Time.deltaTime);
}
heal = false;
}
if (gameObject.tag == "Player")
{
if (damage)
{
damageImage.color = flashColor;
}
else
{
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damage = false;
}
}
public void Damage(int amount)
{
damage = true;
CurrentHealth -= amount;
if (CurrentHealth <= 0)
{
Die();
}
}
public void Die()
{
characterController.enabled = false;
if (scriptsToDisable.Length == 0)
{
return;
}
foreach (MonoBehaviour script in scriptsToDisable)
script.enabled = false;
if (ragdollManager != null)
ragdollManager.RagDoll();
if(gameObject.CompareTag("Enemy"))
{
Destroy(gameObject, Random.Range(4f, 5f));
}
if(gameObject.tag == "Player")
{
if(CurrentHealth <=0)
{
gc.LastCheckPointPos = transform.position;
}
}
}
public void HighHeal()
{
if(gameObject.tag == "Player")
{
if (CurrentHealth > 100)
return;
if(CurrentHealth < 100)
{
CurrentHealth += Highheal;
heal = true;
}
}
}
public void MidHeal()
{
if(gameObject.tag == "Player")
{
if(CurrentHealth > 100)
return;
if(CurrentHealth < 100)
{
CurrentHealth += Midheal;
heal = true;
}
}
}
public void LowHeal()
{
if(gameObject.tag == "Player")
{
if(CurrentHealth > 100)
return;
if(CurrentHealth < 100)
{
CurrentHealth += Lowheal;
heal = true;
}
}
}
}
Comment
Answer by KISP · Mar 08, 2019 at 09:13 PM
In your heal functions, you add a fixed number to your current health without considering whether the resulting sum will be above your health limit. Call Mathf.Clamp after your perform the addition and clamp it to your MaxHealth.
how will i do that i already perform the clamp at start??
For example in your HighHeal you should increase health by something like this: CurrentHealth = $$anonymous$$athf.Clamp(CurrentHealth + Highheal, 0, $$anonymous$$axHealth) I think.
Your answer
![](https://koobas.hobune.stream/wayback/20220612193217im_/https://answers.unity.com/themes/thub/images/avi.jpg)