Enemy chase state Nav Mesh Agent looks broken

after I move out of range of my enemy, the nav mesh is off and not on the enemy. so when i am in range of the enemy it’s like I’m pushing the enemy around.

does anyone know what is wrong?

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

public class EnemyChasingState : EnemyBaseState
{
    private readonly int LocomotionBlendTreeHash = Animator.StringToHash("Locomotion");
    private readonly int SpeedHash = Animator.StringToHash("EnemySpeed");
    private const float CrossFadeDuration = 0.1f;
    private const float AnimatorDampTime = 0.1f;

    public EnemyChasingState(EnemyStateMachine enemyStateMachine) : base(enemyStateMachine)
    {
    }

    public override void Enter()
    {
        enemyStateMachine.Animator.CrossFadeInFixedTime(LocomotionBlendTreeHash, CrossFadeDuration);
    }

     public override void Tick(float deltaTime)
    {
        if(!IsInChaseRange())
        {
            //Transition into Enemy Idle State
            enemyStateMachine.SwitchState(new EnemyIdleState(enemyStateMachine));
            return;
        }      
          MoveToPlayer(deltaTime);
          enemyStateMachine.Animator.SetFloat(SpeedHash, 1f , AnimatorDampTime, deltaTime);
          Debug.Log("In Chasing State!");
    }

    public override void Exit()
    {
       enemyStateMachine.Agent.ResetPath();
       enemyStateMachine.Agent.velocity = Vector3.zero;
       Debug.Log("TEST EXIT");
    }

    public void MoveToPlayer(float deltaTime)
    {
        enemyStateMachine.Agent.destination = enemyStateMachine.Player.transform.position;
        Move(enemyStateMachine.Agent.desiredVelocity.normalized * enemyStateMachine.EnemyMovementSpeed, deltaTime);
        enemyStateMachine.Agent.velocity = enemyStateMachine.Controller.velocity;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyIdleState : EnemyBaseState
{
    private readonly int LocomotionBlendTreeHash = Animator.StringToHash("Locomotion");
    private readonly int SpeedHash = Animator.StringToHash("EnemySpeed");
    private const float CrossFadeDuration = 0.1f;
    private const float AnimatorDampTime = 0.1f;

    public EnemyIdleState(EnemyStateMachine enemyStateMachine) : base(enemyStateMachine)
    {
    }

    public override void Enter()
    {
      enemyStateMachine.Animator.CrossFadeInFixedTime(LocomotionBlendTreeHash, CrossFadeDuration);       
    }

    public override void Tick(float deltaTime)
    {
        Move2(deltaTime);
        if(IsInChaseRange())
        {
            //Transition into Chasing state
            enemyStateMachine.SwitchState(new EnemyChasingState(enemyStateMachine));
            Debug.Log("In Range");
            return;
        }
        enemyStateMachine.Animator.SetFloat(SpeedHash, 0f , AnimatorDampTime, deltaTime);
    }

    public override void Exit()
    {      
    }
}

this is what solved it, from a prior question asked.

public void MoveToPlayer(float deltaTime)

{

    enemyStateMachine.Agent.destination = enemyStateMachine.Player.transform.position;

    Move(enemyStateMachine.Agent.desiredVelocity.normalized * enemyStateMachine.EnemyMovementSpeed, deltaTime);

        //This Was changed from the Tutorial.

        //From enemyStateMachine.Agent.velocity = enemyStateMachine.Controller.velocity;

        //To the following:

    enemyStateMachine.Agent.nextPosition = enemyStateMachine.transform.position;

}
2 Likes

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

Privacy & Terms