Why doesn't my script turn the way i want it too
I'm creating a third-person character controller for a personal project, specifically working on the way I rotate the character to face directions with standard WASD controls. My script
void GetInput ()
{
player.velocity = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
}
void PlayerRotation ()
{
Vector3 heading = player.transform.position - mCam.transform.position;
heading.y = 0;
float Distance = heading.magnitude;
Vector3 Direction = heading / Distance;
Quaternion lookRot = Quaternion.LookRotation(Direction);
lookRot.y = lookRot.y + player.velocity.x;
player.transform.rotation = lookRot;
}
i was expecting it to rotate fairly freely when i hold left or right but it doesn't.
i'm open to suggestions thanks in advance.
Comment
What is the behaviour that you're going after? Rotate in the direction of movement over time?
ill change my question. is there any way to do this:
if (player.velocity != Vector3.zero)
{
Vector3 movement = new Vector3(player.velocity.x, 0.0f, player.velocity.z);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
}
but in camera space, so left right and forward are relative to the cameras facing direction.
never$$anonymous$$d I solved it. I added these lines:
if (player.velocity != Vector3.zero)
{
Vector3 movement = new Vector3(player.velocity.x, 0.0f, player.velocity.z);
movement = mCam.transform.TransformDirection(movement);
movement.y = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
}