Player "jittering" when shooting moving enemies

I’ve just finished the weapons section of the course and there’s two bugs that I noticed.

  1. The player jitters when attacking moving enemies that go out of range. This specifically happens when using the bow.

  2. I’ve decreased the bow range for the archer enemies because I want them to chase the player. However, the enemies now “slide” toward the player if the player is moving away while the archer is attacking.

Any suggestions for fixing these bugs?

The key to resolving both of these is found in lecture 41, Stop Attacking Already.

If a character is in the process of attacking, but the target is no longer in range, then we need to stop the attack using the trigger you’ve set to stop attacking, much the same way as if the target has died.

My preferred solution is to do a quick check on the animation state… First, I put a tag on the Attack state with the name Attack.
Then, in the code block in Update where we decide if the target is in range or not, I add this snippet of code:

        if (anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
        {
            anim.SetTrigger(AbortAttack);
        }

Use whatever name you have for stopping the attack from lecture 41.

This fixed the enemy behavior but its still happening on the player. If an enemy is moving away, the player slowly “jitters” toward the enemy
rpg_bug

Here is my code:
image

So it looks like this is the player approaching the enemy, but the enemy moving away, so it’s fluctuating between a state of shooting and not shooting, thus the “jittering”…

Let’s try a slightly different approach, let’s let the player finish the shot:

if(!IsInRange())
{
     if(animator.GetCurrentAnimatorStateInfo(0).IsTag("Attack");
     {
           return;
     }
     GetComponent<Mover>...
1 Like

Thanks, that worked!

1 Like

For anybody else reading this in the future, here is my code:

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

Privacy & Terms