- Home /
Rotate object on one axis with slerp
I am trying to make my player model rotate with the camera orbit script, but only on the Y axis. So far I can have it rotate on every axis for the camera. This is what I've tried:
var Player : Transform;
var playerRotateSpeed = 2.0;
function Update () {
rot = transform.rotation; //rotation of the camera
Player.transform.rotation = Quaternion.Slerp(Player.transform.rotation, rot, playerRotateSpeed);
}
If I try (Player.transform.rotation.y, rot, playerRotateSpeed) it gives me an error saying slerp can only use (Quaternion, Quaternion, Float) and I'm giving it (Float, Quaternion, Float). Same if I try rot.y or anything else.
How can I get the rotation of one axis of my camera and Player in Quaternion form?
Using JS. This is probably really simple - I apologize as I only know C# and this mouse orbit script was in JS.
Answer by aldonaletto · Aug 19, 2012 at 07:34 PM
No, that's not so simple: you could use transform.eulerAngles.y to know the rotation around Y, but if the other X and Z components aren't zero, weird values may be returned. A more reliable alternative is to isolate only the horizontal camera direction and Slerp to it:
var Player : Transform; var playerRotateSpeed = 2.0;
function Update () { var dir = transform.forward; // get camera forward vector... dir.y = 0; // but keep only the horizontal direction var rot = Quaternion.LookRotation(dir); // now get the desired rotation Player.transform.rotation = Quaternion.Slerp(Player.transform.rotation, rot, playerRotateSpeed * Time.deltaTime); } Notice that I've multiplied playerRotateSpeed by Time.deltaTime in order to get frame rate independence.
Thank you. That is more complicated then I thought. I wasn't sure to multiply the speed by time either so thanks for that as well!
Your answer
Follow this Question
Related Questions
Lookat via Single Axis 0 Answers
Quaternion.Slerp on local axis 1 Answer
Rotation of an object with quaternion 1 Answer
Choppy rotation of character around y-axis 1 Answer
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers