Enemies staying animated on dead but acting like they are alive

So if i save my scene then kill an enemy, after killing them i press L to load to go before the fight, when it loads the enemy is laying down as dead but is still alive and moves to attack me while laying down.

its definitely something to do with the Health script based on the error unity is passing me:

Parameter 'load' does not exist.
UnityEngine.Animator:SetTrigger (string)
RPG.Core.Health:RestoreState (object) (at Assets/Scripts/Core/Health.cs:67)
RPG.Saving.SaveableEntity:RestoreState (object) (at Assets/Scripts/Saving/SaveableEntity.cs:39)
RPG.Saving.SavingSystem:RestoreState (System.Collections.Generic.Dictionary`2<string, object>) (at Assets/Scripts/Saving/SavingSystem.cs:85)
RPG.Saving.SavingSystem:Load (string) (at Assets/Scripts/Saving/SavingSystem.cs:35)
RPG.SceneManagement.SavingWrapper:Load () (at Assets/Scripts/SceneManagement/SavingWrapper.cs:42)
RPG.SceneManagement.SavingWrapper:Update () (at Assets/Scripts/SceneManagement/SavingWrapper.cs:30)
namespace RPG.Core
{
    public class Health : MonoBehaviour, ISaveable
    {

        [SerializeField] private float healthPoints = 100f;

        private Animator animator;
        private ActionScheduler actionScheduler;

        private bool isDead = false;

        private void Awake()
        {
            actionScheduler = GetComponent<ActionScheduler>();
            animator = GetComponent<Animator>();
        }


        public void TakeDamage(float damage)
        {
            //bounded health between 0 and 100
            healthPoints = Mathf.Max(healthPoints - damage, 0);

            if(healthPoints == 0)
            {
                Die();
            }
        }

        private void Die()
        {
            if (isDead) return;

            isDead = true;
            animator.SetTrigger("Die");
            actionScheduler.CancelCurrentAction();
        }

        public bool IsDead()
        {
            return isDead;
        }

        public object CaptureState()
        {
            return healthPoints;
        }

        public void RestoreState(object state)
        {
            healthPoints = (float)state;

            if (healthPoints > 0)
            {
                isDead = false;
                GetComponent<Animator>().SetTrigger("load");
            }
            else Die();
        }
    }
}

One of the issues with the “L” key to Load() is that over the course series, it leaves a few holes like the zombie enemies (and we’re not making a zombie game!).

It looks like you’re working around it by making an Any State → Locomotion transition with a trigger of “load”. This is a good workaround (in the final course, our workaround will be eliminating the “L” key altogether in favor of a proper menu system). I believe the issue in this case is that your trigger’s name isn’t “load”. Remember that the trigger’s name must match exactly the string you’re using in SetTrigger(). This means, for example, that “load” will not match a Trigger in the animator named “Load”.

There is another simpler trick to this. Rather than using a trigger and state transition from AnyState, you can call

GetComponent<Animator>().Rebind();

This will force the Animator back into the default state (locomotion).

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

Privacy & Terms