Disable healthbar after set period of time after combat

I want to add additional functionality to combat and healthbar so that Fighter tracks how long it is not in combat for and after a certain amount of time, it invokes the event to tell healthbar that this is not in combat.

 private void Update() 
        {
            timeSinceLastAttack += Time.deltaTime;
            timeOutOfCombat += Time.deltaTime;
            
            // If target is null or dead, do nothing
            if (target == null) return;
            if (target.IsDead()) return; 

            if (timeOutOfCombat > timeToIndicateNotInCombat)
            {
                onNotInCombat.Invoke();
            }

            // If target is out of weapon range, move to target
            if (!GetIsInRange(target.transform))
            {
                mover.MoveTo(target.transform.position, 1f);
            }
            // else cancel movement and start attacking
            else
            {
                mover.Cancel();
                AttackBehaviour();
            }
        }

void AttackBehaviour()
        {
            // Reset time out of combat
            timeOutOfCombat = 0f;
            
            transform.LookAt(target.transform);
            
            // Only trigger attack if time since last attack reached cooldown
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                TriggerAttack();
                timeSinceLastAttack = 0;
            }
        }

This is my Healthbar.cs. I basically added an extra subscription to disable the healthbar which is out of combat.

using RPG.Combat;
using RPG.Attributes;
using UnityEngine;

namespace RPG.UI.HealthUI
{
    public class HealthBar : MonoBehaviour
    {
        [SerializeField] Health health = null;
        [SerializeField] RectTransform foreground = null;
        [SerializeField] Canvas rootCanvas = null;

        Fighter fighter;
        
        private void Awake() 
        {
            fighter = GetComponentInParent<Fighter>();
        }

        private void OnEnable()    
        {
            health.onHealthUpdated += UpdateHealthBar;
            health.onNoHealthLeft += DisableHealthBar;
            fighter.onNotInCombat += DisableHealthBar;
        }

        private void OnDisable()    
        {
              health.onHealthUpdated -= UpdateHealthBar;
              health.onNoHealthLeft -= DisableHealthBar;
        }
   
        private void UpdateHealthBar()
        {   
            // If health is zero/full, disable health bar
            if (Mathf.Approximately(health.GetFraction(), 0) || Mathf.Approximately(health.GetFraction(), 1)) 
            {
                rootCanvas.enabled = false;
                return;
            }
            
            rootCanvas.enabled = true;
            foreground.localScale = new Vector3(health.GetFraction(), 1.0f, 1.0f);      
        }

        private void DisableHealthBar()
        {
            rootCanvas.enabled = false;
        }
    }
}

I have set time to indicate out of combat to 8 to test but it does not seem to disable after 8 seconds and I was wondering if I was missing anything in implementation?
Any help is much appreciated, cheers!

The issue has to do with the order of what you’re doing in Fighter.Update.

When you’re not in combat, the target is either null or dead, meaning that you’ve left the Update() method before evaluating timeOutOfCombat.

Move the if(timeOutOfCombat > timetoIndicateNotInCombat) block up to before the if(target==null) block, and the event should fire. You may also wish to null check that onNotInCombat.Invoke by calling it like this

onNotInCombat?.Invoke();

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms