Level 2 Enemies Not Regenerating Health On Respawn

Another (hopefully quick) issue: Enemies on level two don’t seem to be regenerating health. I can confirm that guards attacked on level one are, so I figure it’s something small I’ve overlooked.

These enemies are tagged as ‘Heavy’, with only one set of progression data. Health is set to max of 80, and regeneration percentage is 25%.

I’ll post the respawner script below:

using Cinemachine;
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
using RPG.Attributes;
using RPG.Combat;
using RPG.SceneManagement;

namespace RPG.Control
{
    public class Respawner : MonoBehaviour
    {
        [SerializeField] public Transform respawnLocation;

        [SerializeField] float respawnDelay = 2;

        [SerializeField] float fadeTime = 1;

        [SerializeField] float healthRegenPercentage = 50;

        [SerializeField] float enemyHealthRegenPercentage = 25;

        void Awake()
        {
            GetComponent<Health>().onDie.AddListener(Respawn);
        }

        void Start()
        {
            if (GetComponent<Health>().IsDead())
            {
                Respawn();
            }
        }

        public void Respawn()
        {
            StartCoroutine(RespawnRoutine());
        }

        private IEnumerator RespawnRoutine()
        {
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
            savingWrapper.Save();
            yield return new WaitForSeconds(respawnDelay);
            Fader fader = FindObjectOfType<Fader>();
            yield return fader.FadeOut(fadeTime);
            RespawnPlayer();
            ResetEnemies();
            savingWrapper.Save();
            yield return fader.FadeIn(2f);
        }

        private void ResetEnemies()
        {
            foreach (AIController enemyControllers in FindObjectsOfType<AIController>())
            {
                Health health = enemyControllers.GetComponent<Health>();
                AggroGroup aggroGroup = FindObjectOfType<AggroGroup>();
                bool activateOnStart = false;
                if (health && !health.IsDead())
                {
                    enemyControllers.Reset();
                    if (aggroGroup == null) { continue; }
                    aggroGroup.Activate(activateOnStart);
                    health.Heal(health.GetMaxHealthPoints() * enemyHealthRegenPercentage / 100);
                }
            }
        }

        private void RespawnPlayer()
        {
            GameObject player = GameObject.FindWithTag("Player");
            Vector3 positionDelta = respawnLocation.position - transform.position;
            GetComponent<NavMeshAgent>().Warp(respawnLocation.position);
            player.transform.rotation = respawnLocation.rotation;
            Health health = GetComponent<Health>();
            health.Heal(health.GetMaxHealthPoints() * healthRegenPercentage / 100);
            ICinemachineCamera activeVirtualCamera = FindObjectOfType<CinemachineBrain>().ActiveVirtualCamera;
            if (activeVirtualCamera.Follow == transform)
            {
                activeVirtualCamera.OnTargetObjectWarped(transform, positionDelta);
            }
        }
    }
}

Thanks!

Edit: Player regeneration is working correctly, so I’m wondering what makes these enemies different (other than their class)?

Found it! I had to change the order of operation so that the enemies without aggroGroups can still heal correctly.

if (health && !health.IsDead())
                {
                    enemyControllers.Reset();
                    health.Heal(health.GetMaxHealthPoints() * enemyHealthRegenPercentage / 100);
                    if (aggroGroup == null) { continue; }
                    aggroGroup.Activate(activateOnStart);
                }
1 Like

Well done finding that!

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

Privacy & Terms