Any questions or comments about triggering death animations?

This was a pretty quick, easy lecture… any questions or comments?


Before the fix. Laughed a little.

I like the decorations you’ve added. :slight_smile:

Ah, the crates. I attached Rigidbodies for realism. Enjoyed flinging them across the sandbox. :sweat_smile:

Hi! Random Q. 3 years on (no worries if it doesn’t get a reply)…

I tried writing the following if statement…

if (objectHealth == 0)
{
GetComponent().SetTrigger(“isDying”);
}

…inside the Update() method of the Health.cs.

But it didn’t work correctly.

After I’d cut/paste this if statement it into the TakeDamage() method (as instructed in the video) it worked fine. However, I’m unsure as to why this is.

What about my putting the if statement in Health.cs Update() was making it bug out?

Any light you can shed onto this would be appreciated :slight_smile:

The “isDying” trigger is from Any state. This includes the state that the trigger leads to.
So what’s happening when you put it in Update(), is it keeps going back to the beginning of the Dead state, over and over and over and over again.

Thank you for the explanation :slight_smile:

I dont know but we could just Untick the "can transition to Self " ?

That actually should work, yes. It’s still best not to set the trigger every frame.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Combat
{
    public class Health : MonoBehaviour
    {
        [SerializeField] float healthPoints = 100f;

        bool isDead = false;

        public bool GetIsDead()
        {
            return isDead;
        }

        public void TakeDamage(float damage)
        {
            healthPoints = Mathf.Max(healthPoints - damage, 0);
            if (healthPoints == 0)
            {
                Die();
            }
        }

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

            isDead = true;
            GetComponent<Animator>().SetTrigger("die");
        }
    }
}

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        // ...

        private void AttackBehavior()
        {
            if (target.GetComponent<Health>().GetIsDead()) return;

            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                // This will trigger the Hit() event.
                GetComponent<Animator>().SetTrigger("attack");
                timeSinceLastAttack = 0;
            }

        }

        // ...
    }
}

I did it like this.

Is it bad to check ‘isDead’ in ‘Fighter.cs’?

Privacy & Terms