Enemy stops moving when adding animations

Hey! This problem has been solved, I just can’t get rid of the topic.

Hello! I’m currently working on the Zombie Runner game in the course Complete C# Unity Game Developer 3D. I’m on chapter 166 (trigger animation in code). The problem I’m having is that once I add the animations (Idle, move, attack)) the enemy stops from moving. It only looks at the player but doesn’t chase it like it did before I added the animations.

Here are some screenshots:


When the game starts, the character plays it’s idle animation. When I get close enough to the enemy the animation switches to “Move” and stops the idle animation. Once it’s at move it doesn’t actually move. It only rotates where the player is located at.

HERE COMES MY ENEMY AI CODE:

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

public class EnemyAI : MonoBehaviour
{

    [SerializeField] Transform target;
    [SerializeField] float chaseRange = 5f;

    NavMeshAgent navMeshAgent;
    float distanceToTarget = Mathf.Infinity;

    bool isProvoked = false;

    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

   
    void Update()
    {
        distanceToTarget = Vector3.Distance(target.position, transform.position);
        if (isProvoked)
        {
            EngageTarget();
        }
        else if (distanceToTarget <= chaseRange)
        {
            isProvoked = true;

        }
    }

    private void EngageTarget()
    {
        if (distanceToTarget >= navMeshAgent.stoppingDistance)
        {
            ChaseTarget();
        }

        if (distanceToTarget <= navMeshAgent.stoppingDistance)
        {
            AttackTarget();
        }
    }

    void ChaseTarget()
    {
        GetComponent<Animator>().SetTrigger("move");
        navMeshAgent.SetDestination(target.position);        
    }

    void AttackTarget()
    {
        Debug.Log("Attacked!");
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, chaseRange);
    }

}


Here’s the enemy just looking at me when I come close.

I really appriciate if someone could help me, I’m really struggeling hah, thanks for your time!

Wait something is really messed up here, when I move my enemy to another place. Once I come close to the enemy it jumps back all the way to where it started, and runs the animation. I think it’s a some sort o timeline this time, not an animation. That’s why it’s stuck in it’s position. Or??

Maybe you have root motion turned on in your animation? If so, the animator may be trying to control your movement instead of letting the navmesh do it.

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

Privacy & Terms