After adding the impact state, two things started happening. For one, after the enemy gets knocked back, sometimes they start backing away instead, until they are past detection range. It first, I thought I’d fixed it by adding another statement to ensure the enemy was facing the player, but that only fixed the situation where they’d lost sight of me. Sometimes it’s backwards, and it stops when they exit detection range. Sometimes, it’s more like they’re running Perpendicular, and get stuck, running in place.
The second issue, but which also showed up around the time I added the impact state (as far as I can tell) is that when I hold down a movement key in one direction, the first cycle or so of the animation looks like, but then it starts restarting the animation over and over again, just sort of jittering in the first part of the animation.
EnemyChaseState:
public override void Tick(float deltaTime)
{
MoveToPlayer(deltaTime);
FacePlayer();
if (!IsInChaseRange())
{
stateMachine.SwitchState(new EnemyIdleState(stateMachine));
return;
} else if (IsInAttackRange())
{
stateMachine.SwitchState(new EnemyAttackingState(stateMachine));
return;
}
stateMachine.Animator.SetFloat(SpeedHash, 1f, AnimatorDampTime, deltaTime);
}
public void MoveToPlayer(float deltaTime)
{
if (stateMachine.Agent.isOnNavMesh)
{
Vector3 destination = this.stateMachine.Player.transform.position;
stateMachine.Agent.destination = destination;
Move(stateMachine.Agent.desiredVelocity.normalized * this.stateMachine.MovementSpeed, deltaTime);
// Debug.Log(string.Format("Destination: {0}, direction: {1}, Speed: {2}", destination, stateMachine.Agent.desiredVelocity.normalized, this.stateMachine.MovementSpeed));
FacePlayer();
}
stateMachine.Agent.velocity = stateMachine.Controller.velocity;
}
PlayerFreeLookState:
public override void Enter() {
stateMachine.InputReader.JumpEvent += OnJump;
stateMachine.InputReader.DodgeEvent += OnDodge;
stateMachine.InputReader.TargetEvent += OnTarget;
stateMachine.Animator.CrossFadeInFixedTime(FreeLookBlendTreeHash, FixedTransitionDuration);
}
public override void Tick(float deltaTime)
{
if (stateMachine.InputReader.IsAttacking)
{
stateMachine.SwitchState(new PlayerAttackingState(stateMachine, 0));
return;
}
Vector3 movement = CalculateMovement();
Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero) {
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0f, AnimatorDampTime, deltaTime);
return;
}
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1.0f, AnimatorDampTime, deltaTime);
FaceMovementDirection(movement, deltaTime);
}
public override void Exit() {
stateMachine.InputReader.DodgeEvent -= OnDodge;
stateMachine.InputReader.JumpEvent -= OnJump;
stateMachine.InputReader.TargetEvent -= OnTarget;
}