RPG Death animation not working

Hello, I completed the RPG Core Combat Creator and the other RPG courses. The death animation is not triggering. I have inserted all the methods in an animation event that I could think of that calls the Die() method, and it didn’t work. I checked the transition from any state to the death state and it is correct. I need help, thank you in advance.


Eric Montoya

My first thought is it may be related to the error messages. Any time a script encounters an error, it effectively stops working. In the case of the NavMesh errors, that isn’t likely to affect the Death state, but we may want to investigate the first one… trying to instantiate an item that is null. Click on that error and there should be a stack trace, a list of method calls leading up to the null reference with line numbers. The first script is generally the culprit.

The NavMesh error seems to be saying that at least one of your characters is too far away from the NavMesh (or that the NavMesh isn’t baked).

Let’s start with a look at your Health.cs. Be sure to paste in the text of the script, not a screenshot.

Here is the script… These errors only occur when the player defeats the enemy.

using UnityEngine;
using RPG.Saving;
using RPG.Stats;
using RPG.Core;
using System;
using RPG.Utils;
using UnityEngine.Events;

namespace RPG.Attributes
{
    public class Health : MonoBehaviour, ISaveable
    {
        [SerializeField] float regenerationPercentage = 70;
        [SerializeField] TakeDamageEvent takeDamage;
        public UnityEvent onDie;

        [System.Serializable]
        public class TakeDamageEvent : UnityEvent<float>
        {

        }

        LazyValue <float> healthPoints;

        bool wasDeadLastFrame = false;

        void Awake()
        {
            healthPoints = new LazyValue<float>(GetInitialHealth);
        }

        private float GetInitialHealth()
        {
            return GetComponent<BaseStats>().GetStat(Stat.Health);
        }

        void Start()
        {
            healthPoints.ForceInit();
        }

        void OnEnable()
        {
            GetComponent<BaseStats>().onLevelUp += RegenerateHealth;
        }
       
        void OnDisable()
        {
            GetComponent<BaseStats>().onLevelUp -= RegenerateHealth;
        }

        public bool IsDead()
        {
            return healthPoints.value <=0;
        }
        
        public void TakeDamage(GameObject instigator, float damage)
        {
            healthPoints.value = Mathf.Max(healthPoints.value - damage, 0);

            if(IsDead())
            {
                onDie.Invoke();
                AwardExperience(instigator);
            }
            else
            {
                takeDamage.Invoke(damage);
            }
            UpdateState();
        }

        public void Heal(float healthToRestore)
        {
            healthPoints.value = Mathf.Min(healthPoints.value + healthToRestore, GetMaxHealthPoints());
            UpdateState();
        }

        public float GetHealthPoints()
        {
            return healthPoints.value;
        }

        public float GetMaxHealthPoints()
        {
            return GetComponent<BaseStats>().GetStat(Stat.Health);
        }
     
        public float GetPercentage() //To Display the health percentage in text.  Subject to change.
        {
            return 100 * GetFraction();
        }

        public float GetFraction()
        {
            return healthPoints.value / GetComponent<BaseStats>().GetStat(Stat.Health);
        }

        private void UpdateState()
        {
            Animator animator = GetComponent<Animator>();
            if (!wasDeadLastFrame && IsDead())
            {
                GetComponent<Animator>().SetTrigger("Die");
                GetComponent<ActionScheduler>().CancelCurrentAction();
            }

            if(wasDeadLastFrame && !IsDead())
            {
                animator.Rebind();
            }
           
            wasDeadLastFrame = IsDead();
        }

        private void AwardExperience(GameObject instigator)
        {
            Experience experience = instigator.GetComponent<Experience>();
            if (experience == null) return;

            experience.GainExperience(GetComponent<BaseStats>().GetStat(Stat.ExperienceReward));
        }

        private void RegenerateHealth()
        {
            float regenHealthPoints = GetComponent<BaseStats>().GetStat(Stat.Health) * (regenerationPercentage / 100);
            healthPoints.value = Mathf.Max(healthPoints.value, regenHealthPoints);
        }

        public object CaptureState()
        {
            return healthPoints.value;
        }

        public void RestoreState(object state)
        {
            healthPoints.value = (float)state;
            UpdateState();           
        }
    }
}

Here is the stack of error messages.

@Brian_Trotter I fixed it, I had to create a drop library for the inventory and add something to it in the inspector. So I create a prototype coin drop when the enemy is defeated. Thank you for pointing out my errors and teaching me.

Thank you,
Eric

Great job! All I did was point in the right direction. You did the heavy lifting and found the problem.

1 Like

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

Privacy & Terms