Question by
solocs · Jul 14, 2017 at 12:05 AM ·
collider2dontriggerstay
Trying to use OnTriggerStay to continuously decrease my health bar but only triggers once. How to fix this?
I've been tackling this problem for hours and have looked up similar questions on Unity but to no avail. Basically, this part of the code is supposed to trigger whenever the player collider touches the trigger collider of the object using this script.....:
public float damage = 10;
void OnTriggerStay2D(Collider2D other) {
if (other.tag == "Player") {
other.SendMessage ("TakeDamage", Time.fixedDeltaTime * damage);
Debug.Log ("HIIII");
}
}
...where SendMessage calls the 'TakeDamage' function in this script
public Image currentHealthbar;
public Text ratioText;
private float hitpoint = 150;
private float maxHitpoint = 150;
private void TakeDamage(float damage) { //called everytime sendmessage sends TakeDamage
hitpoint -= damage;
if (hitpoint < 0) {
hitpoint = 0;
}
UpdateHealthBar ();
}
private void UpdateHealthBar() {
float ratio = hitpoint / maxHitpoint;
currentHealthbar.rectTransform.localScale = new Vector3 (ratio, 1, 1);
ratioText.text = (ratio*100).ToString("0") + '%';
}
// Use this for initialization
void Start () {
UpdateHealthBar (); //initializes health bar
}
...But it only triggers once before stopping. It only triggers continuously if I'm moving continuously...
Edit: Still can't find out why OnTriggerStay2D isn't continuous....
Comment
Still can't find out why OnTriggerStay2D isn't continuous....