- Home /
How to make character rotate in the direction of movement and direction of the camera?
Here is an example of what I am trying to accomplish that I found when trying to find a solution
I am attempting to make a third person character controller with 8 direction movement where the character rotates towards the direction that it is moving, while also using the mouse to control it's direction. For example, when walking forwards, the character will walk away from the camera. When walking sideways, they will walk towards the left or the right of the camera and towards the camera if backwards.
Here is what I currently have:
Vector3 movementDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (movementDirection != Vector3.zero)
{
// This works to make the character move towards the direction of the camera
this.transform.rotation = Quaternion.Euler(0, mainCamera.transform.rotation.eulerAngles.y , 0);
// This works to rotate the character in the direction of it's movement
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(movementDirection), 0.15f);
}
I feel like adding the camera's rotation to the Quaternion.LookRotation is what I need to do but I am not sure how to go about this.
Answer by Poseidon1145 · Sep 20, 2017 at 10:13 PM
If you want your character to turn in the direction that moves here I have a script
If your rotation does not match the bone direction the character rotates 90 degrees more as he tries to reimport your already rotated character
void Update() {
float angle = Mathf.Atan2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * Mathf.Rad2Deg;
transform.GetChild(0).rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
That does work to replace the Quaternion.LookRotation but is there a way that I can add the angle of the camera to the angle variable to make the camera rotate the character as well as the keys changing his direction?