I’ve got an issue where the enemy can’t move after the player is knocked back. He just play his chasing animation but the velocity is 0.
He only moves again to get in range if the player moves.
i think my enemy chasing state script is pretty much the same as the one in the lecture. I can’t figure out the origin of the bug when the navmesh destination seems to be correct.
public class EnemyChasingState : EnemyBaseState
{
private readonly int LocomotionBlendTreeHash = Animator.StringToHash("LocomotionBlendTree");
private readonly int SpeedHash = Animator.StringToHash("Speed");
private const float AnimatorDampTime = 0.1f;
private const float CrossFadeDuration = 0.1f;
public EnemyChasingState(EnemyStateMachine stateMachine) : base(stateMachine) { }
public override void Enter()
{
stateMachine.Animator.CrossFadeInFixedTime(LocomotionBlendTreeHash, CrossFadeDuration);
}
public override void Tick(float deltaTime)
{
if (!IsInChaseRange())
{
stateMachine.SwitchState(new EnemyIdleState(stateMachine));
return;
}
else if (IsInAttackRange())
{
stateMachine.SwitchState(new EnemyAttackingState(stateMachine));
}
MoveToPlayer(deltaTime);
FacePlayer();
stateMachine.Animator.SetFloat(SpeedHash, 1f, AnimatorDampTime, deltaTime);
}
public override void Exit()
{
stateMachine.Agent.ResetPath();
stateMachine.Agent.velocity= Vector3.zero;
}
private void MoveToPlayer(float deltaTime)
{
if(stateMachine.Agent.isOnNavMesh)
{
stateMachine.Agent.destination = stateMachine.Player.transform.position;
Move(stateMachine.Agent.desiredVelocity.normalized * stateMachine.MovementSpeed, deltaTime);
Debug.Log("Destination" + stateMachine.Agent.destination);
Debug.Log("player pos" + stateMachine.Player.transform.position);
Debug.Log("speed" + stateMachine.MovementSpeed);
}
stateMachine.Agent.velocity = stateMachine.Controller.velocity;
Debug.Log(" stateMachine.Agent.velocity " + stateMachine.Agent.velocity);
Debug.Log("stateMachine.Controller.velocity" + stateMachine.Controller.velocity);
}
private bool IsInAttackRange()
{
float playerDistanceSqr = (stateMachine.Player.transform.position - stateMachine.transform.position).sqrMagnitude;
return playerDistanceSqr <= stateMachine.AttackRange * stateMachine.AttackRange;
}
}