Stop Attacking Already - Lecture

Any questions or comments regarding this lecture?

This lecture doesn’t seem to be on my list of videos?

@skiddeh I believe the phrase is “Soon™”

Fair enough, could have sworn I saw it on my list of videos this morning haha

These are not the droids (cough) videos you were looking for… Im sure it will be there soon

1 Like

Sorry guys, I found an issue in the video so did a sneaky unpublish before too many people dove in. I’m going to publish it again in a moment.

2 Likes

So i cant tell if i missed something or not much was explained about the bool. 2 lectures ago i had basically done everything that made the character stop attacking etc and i come to compare my code to the lecture and 1.) maybe its my lack of coding thats not helping me understand how the public bool works with the return 2.) i went and tried to put the same exact code in as shown in the video and im getting errors for using the same name (isDead) so many times which im still wondering how you got away with in the lecture. Here is the code explaining that.
My Code that works

namespace RPG.Combat
{
    public class Healths : MonoBehaviour
    {       
        public float Health = 100f;

        public bool isAlive = true;
             
        public void TakeDamage(float damage)
        {
            Health = Mathf.Max(Health - damage, 0);
            if (Health == 0)
            {
                Die();
            }

        }

        private void Die()
        {
            
            isAlive = false;
            GetComponent<Animator>().SetTrigger("died");  
        }
    }
}
private void Update()
        {
            //timeSinceLastAttack += Time.deltaTime;
            timeSinceLastAttack += Time.deltaTime;

            if (target == null) return;
            if (target.isAlive == false) return;

This is me Changing my code to the lecture

namespace RPG.Combat
{
    public class Healths : MonoBehaviour
    {       
        public float Health = 100f;

        bool isDead = false;
        
        public bool isDead()
        {
            return isDead;
        }

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

        }

        private void Die()
        {

            isDead = true;
            GetComponent<Animator>().SetTrigger("died");  
        }
    }
}
    ```
    private void Update()
    {
        //timeSinceLastAttack += Time.deltaTime;
        timeSinceLastAttack += Time.deltaTime;

        if (target == null) return;
        if (target.isDead()) return;
        ```

this is a screenshot of the errors


so what i now did was change the name of the method and it works. So id like to know if i need to have my code like yours where both the method and the bool are the same name because unity isnt allowing me to use multiple names or am i supposed to use a different name for my bool and method?

how did no one respond to you almost a year ago?

lol the answer is that you named your method exactly the same as your bool variable both named isDead you should name the bool variable isDead then name the method IsDead, notice that the “i” is capitalized on the method to differentiate them. variables always start with a lower case and methods always start capitalized to prevent this issue from happening.

Can’t we just call Cancel() method in Fighter when the enemy is dead?

For some reason, my character is always wanting to just do one more attack before stopping. He punches, the enemy dies, and he punches one more time. I start an attack, cancel it, and he stops attacking, then after he has finished leaving the punch animation, he punches again. I cannot figure out why it is doing this, it should not be able to get to the code to call an attack, since it is telling me that when it punches after disengaging that it has not target, and I have 2 checks to make sure that you are not punching null. Has anyone else experienced this or have a solution?

The StopAttack begins the transition from attacking back to locomotion, but during the transition, the two animations are blended, meaning you may still see attacks in progress… If you wanted a real world example of this, think of a football play when the whistle is blown. Some players are still in the progress of their maneuver and can’t really undo them.

Another issue is that sometimes the Attack and StopAttack triggers get stuck. You can mitigate this by resetting the trigger before setting it. For example:

anim.ResetTrigger("stopAttack");
anim.SetTrigger("stopAttack");

This will ensure that the trigger functions properly.

1 Like

Nevermind, figured out the issue. For anyone else who as the same issue, try putting the attack delay up higher. Turns out that mine was low enough that it was triggering another attack before the first one was done, so it would punch one more time because when it triggered the enemy was still alive.

1 Like

I used that ResetTrigger() method call to stop the bug where the player starts to attack but immediately stops. I pulled up the animator on the player and realized that the trigger would sometimes get left “on” especially after a kill, so I dropped in the line at the start of the AttackBehaviour() method for now. There’s probably a better solution later on but for now it seems to work okay.

I also added a transfom.LookAt() method call at the begging of the AttackBehaviour and passed the target position to it to stop the player from shadow boxing the enemies to death.

When I right click Attack I don’t see a “Make Transition” option.

It seems I can’t add multiple transitions between states at all. How do I fix this?

I went back to 2020.3.30f1
I think this is a bug with 2020.3.31f1

It’s quite possible. It’s not listed amongst the “known issues” with that version, but that just means it isn’t a known issue.

Shouldn’t we set target back to null when the target is dead? That seems like one of those subtle bugs that could come back to get us later.

I would say yes, in this case.

I tend to start my Fighter.Update() method with this:

if(!target) return; 
if(target.IsDead())
{
    target=null;
    Cancel();
    return;
}

(This assumes that we’ve switched the target to being a Health component, which may actually come later in the course, if not, then if(target.GetComponent<Health>().IsDead())

Privacy & Terms