- Home /
Smoothly Rotate Character Y to Camera Y
Hi again. I'm now trying to make my character's y rot to lerp to the same value as the camera's y rot. This way the character will face the same direction the Camera is, at least that's what I expect to happen. This is the code I'm using:
private void RotateToCamFunc()
{
if (IsMoving())
{
float camY = Camera.transform.eulerAngles.y;
float newY = Mathf.Lerp(transform.eulerAngles.y, camY, camBasedPlayerRotationLerp);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, newY, transform.eulerAngles.z);
}
}
Unfortunately, it's pretty glitchy. The character will just randomly glitch while turning. And when I believe the camera goes from 359 to 0, the Lerp craps itself and does a full 360 degree turn the opposite way.
Any Idea how I could fix this, or a better solution to the problem altogether?
Answer by unity_ek98vnTRplGj8Q · Jan 08, 2020 at 07:51 PM
Always be careful when messing with euler angles, make sure you are only doing as little as possible with them. In general it is bad practice to read, modify, write euler angles because it can result in exactly the kinds of problems you are describing. Instead, use quaternion functions whenever possible.
private void Update()
{
Quaternion targetRot = Quaternion.Euler(0, cam.transform.eulerAngles.y, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, slerpVal);
}
Answer by QuincyOnIce · Jan 08, 2020 at 09:57 PM
Oh, thanks! I had tried Quaternion.Lerp before but it was glitchy too, so I decided to go back to eulers. Looks like Slerp was the solution all along. Thanks again!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
move the object where camera look 0 Answers
Character goes up when the camera looks up 0 Answers
Limit camera RotateAround for an UAV game 0 Answers
Quaternion.Lerp finishing before "t" parameter hits 1 0 Answers