Ive been making gradual progress on this. I added a simple method to PlayerFreeLookState:
private void FaceCameraDirection()
{
stateMachine.transform.rotation = Quaternion.Euler(0, stateMachine.MainCameraTransform.eulerAngles.y, 0);
}
which gets called twice in Tick():
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);
stateMachine.Controller.Move(movement * stateMachine.FreeLookMovementSpeed * deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero)
{
FaceCameraDirection();
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0, animatorDampTime, deltaTime);
return;
}
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1, animatorDampTime, deltaTime);
//FaceMovementDirection(movement, deltaTime);
FaceCameraDirection();
}
I call it from within the if statement, otherwise the body won’t rotate if the player isn’t moving. The same effect is achieved by calling FaceCameraDirection() at the beginning of the method, of course. Im not sure which way is better, I only did it this way to hedge against issues down the road, although it probably doesn’t matter which way it’s done.
With this, the body will turn to face the camera direction, so FPS view looks more natural. Im still getting a lot of clipping when the head moves in front of the camera. Im wondering how other games do this? Should I make the head a separate game object that doesn’t get rendered by the freelook camera?