- Home /
Rotate character to camera orientation(direction camera is facing)
Im using root motion with a character controller ,current trying to have the character while moving to rotate in the direction the camera is facing(third person).
This is the code im using it works:
public void CameraCharacterRotation()
{
float yawCamera = playerCamera.transform.rotation.eulerAngles.y;
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.Euler(transform.rotation.x, yawCamera,
transform.rotation.z), turnSpeed * Time.fixedDeltaTime);
}
It will rotate character to face camera direction will moving. The issue lies when i turn a character left /right/back
The code for that works as well:
public void DirectionalRotation(float characterRotaionSpeed)
{
//Converts playerInput.directionalInput(Vector2) to a Vector3 playerMovement, this will be used for the rotation of the
character,y is set to zero due as we only want to manipulate x and z axis
playerMovement = new Vector3(playerSettings.DirectionalInput.x, 0f, playerSettings.DirectionalInput.y);
//Gets the current rotation
var currentRotation = transform.rotation;
//Rotates the transform to look at the directional key pressed
transform.LookAt(playerMovement + transform.position, Vector3.up);
//Gets the new rotation after a key has been pressed
var newRotation = transform.rotation;
//Lerps the rotation from the charcters previous rotation(before key press) to its new rotation (after key pressed). You can
adjust characterRotaionSpeed to determine the speed of the rotation
transform.rotation = Quaternion.Lerp(currentRotation, newRotation, characterRotaionSpeed);
}
I call DirectionalRotation() in the MovementState
Root motion: private void OnAnimatorMove() { rootMotion += animator.deltaPosition; }
private void FixedUpdate()
{
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
playerVelocity.y -= playerSettings.Gravity * Time.deltaTime;
characterController.Move(rootMotion + playerVelocity);
rootMotion = Vector3.zero;
}
The code for the DirectionalRotation works and the code for CameraCharacterRotation works, but together they cause issues. The character will rotate in camera direction but the character will not rotate according to which movement direction is pressed.
If i dont use root motion it is able to do what i need, but im trying with root motion, as for me it looks and feels better.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011421im_/https://answers.unity.com/themes/thub/images/avi.jpg)