I am noticing that after I Trigger the attack animation by clicking for some reason my character is going back to the Free Look State, even though I have not clicked the esc button. Everything else is working perfectly and it did not do this prior.
I am pretty sure that the code is correct,
public class PlayerTargetingState : PlayerBaseState
{
private readonly int TargetingBlendTreeHash = Animator.StringToHash("TargetingBlendTree");
private readonly int TargetingForwardHash = Animator.StringToHash("TargetingForward");
private readonly int TargetingRightHash = Animator.StringToHash("TargetingRight");
private const float CrossFadeDuration = 0.1f;
public PlayerTargetingState(PlayerStateMachine stateMachine) : base(stateMachine)
{
}
public override void Enter()
{
stateMachine.InputReader.CancelEvent += OnCancel;
stateMachine.Animator.CrossFadeInFixedTime(TargetingBlendTreeHash, CrossFadeDuration);
}
public override void Tick(float deltaTime)
{
//Attacking
if(stateMachine.InputReader.IsAttacking)
{
stateMachine.SwitchState(new PlayerAttackingState(stateMachine,0));
return;
}
//Targeting
if(stateMachine.Targeter.CurrentTarget == null)
{
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
return;
}
Vector3 movement = CalculateMovement();
Move(movement * stateMachine.TargetingMovementSpeed, deltaTime);
UpdateAnimator(deltaTime);
FaceTarget();
}
public override void Exit()
{
stateMachine.Targeter.Cancel();
stateMachine.InputReader.CancelEvent -= OnCancel;
}
private void OnCancel()
{
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
}
private Vector3 CalculateMovement()
{
Vector3 movement = new Vector3();
movement += stateMachine.transform.right * stateMachine.InputReader.MovementValue.x;
movement += stateMachine.transform.forward * stateMachine.InputReader.MovementValue.y;
return movement;
}
private void UpdateAnimator(float deltaTime)
{
if(stateMachine.InputReader.MovementValue.y == 0)
{
stateMachine.Animator.SetFloat(TargetingForwardHash, 0, 0.1f, deltaTime);
}
else
{
float value = stateMachine.InputReader.MovementValue.y > 0 ? 1f : -1f;
stateMachine.Animator.SetFloat(TargetingForwardHash, value, 0.1f, deltaTime);
}
if (stateMachine.InputReader.MovementValue.x == 0)
{
stateMachine.Animator.SetFloat(TargetingRightHash, 0, 0.1f, deltaTime);
}
else
{
float value = stateMachine.InputReader.MovementValue.x > 0 ? 1f : -1f;
stateMachine.Animator.SetFloat(TargetingRightHash, value, 0.1f, deltaTime);
}
}
}
}
public class PlayerFreeLookState : PlayerBaseState
{
private readonly int FreeLookSpeedHash = Animator.StringToHash("FreeLookSpeed");
private readonly int FreeLookBlendTreeHash = Animator.StringToHash("FreeLookBlendTree");
private const float AnimatorDampTime = 0.1f;
private const float CrossFadeDuration = 0.1f;
public PlayerFreeLookState(PlayerStateMachine stateMachine) : base(stateMachine) { }
public override void Enter()
{
stateMachine.InputReader.TargetEvent += OnTarget;
stateMachine.Animator.CrossFadeInFixedTime(FreeLookBlendTreeHash, CrossFadeDuration);
}
public override void Tick(float deltaTime)
{
//Attacking
if (stateMachine.InputReader.IsAttacking)
{
stateMachine.SwitchState(new PlayerAttackingState(stateMachine, 0));
return;
}
//Movement
Vector3 movement = CalculateMovement();
Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero)
{
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0, AnimatorDampTime, deltaTime);
return;
}
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1, AnimatorDampTime, deltaTime);
FaceMovementDirection(movement, deltaTime);
}
public override void Exit()
{
stateMachine.InputReader.TargetEvent -= OnTarget;
}
private void OnTarget()
{
if (!stateMachine.Targeter.SelectTarget()) { return; }
stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
}
private Vector3 CalculateMovement()
{
Vector3 forward = stateMachine.MainCameraTransform.forward;
Vector3 right = stateMachine.MainCameraTransform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
return forward * stateMachine.InputReader.MovementValue.y + right *stateMachine.InputReader.MovementValue.x;
}
private void FaceMovementDirection(Vector3 movement, float deltaTime)
{
stateMachine.transform.rotation = Quaternion.Lerp(stateMachine.transform.rotation, Quaternion.LookRotation(movement), deltaTime*stateMachine.RotationDamping);
}
}