- Home /
How to, restrict Quaternion.Slerp to the Y axis?
Hello fellow uniteres!
I have a third person game where the camera is free to rotate around when the player is not moving. And when the player is moving the players Y axis lines with the cameras Y axis, but when doing so the player "snaps" to the cameras Y axis.
I want to make this smooth and therefor considering using Quaternion.Slerp, but the problem using Quaternion.Slerp is that it is "Slerping" all axis X, Y, Z, but as said I only want to "Slerp" the Y axis. Is this possible(It most likely, but I do not know how to) I have tried Mathf.Clamp, but that causes "shaky" movements and slow fall through the floor.
The current code I use for the rotation right now is this(small sample only):
var GameCam : Transform; //Camera goes here.
if(Input.GetAxisRaw("Vertical") > 0.1 && !Input.GetAxisRaw("Horizontal"))
{
transform.eulerAngles.y = GameCam.eulerAngles.y; // This is used for the rotation
transform.Translate(0,0,Input.GetAxisRaw("Vertical")/20);//Movement no need to look at that.
}
So can you please tell me a way to restricting "Slerping" to the Y axis only. I will be greatfull!
I do not have any Slerp specific code since I could not for the life of me restrict it to the Y axis only so I sticked to the code in the updated post.
Answer by testure · Jun 08, 2011 at 09:57 PM
yourRotationQuaternion = Quaternion.Slerp(to, from, time);
yourRotationQuaternion = Quaternion.Euler(new Vector3(0f, yourRotationQuaternion.eulerAngles.y, 0f));
done like dinner
Thank you. tried messing with it, but importunately the player only turns/rotates 0.8 degrees each direction and nothing more and then the player is kinda "clamped" there and can not rotate further.
You corrected Anxo, but you made the same mistake. yourRotationQuaternion.y
is not an euler value so putting it into Quaternion.Euler is a mistake
Ah- yes. I've edited my post to clarify.
Just more proof that we need intellisense here too :)
Works like a charm. Thanks! The original poster should mark this "answered".
Answer by flim · Jun 09, 2011 at 05:21 AM
After the Quaternion.Slerp statment
transform.localEulerAngles = Vector3(0, transform.localEulerAngles.y, 0);
Answer by Anxo · Jun 08, 2011 at 10:29 PM
transform.rotation.y = Quaternion.Slerp(bla bla bla).y;
yeah, but Y is not what you're thinking it is- he's talking about restricting to the Y euler angle, this would give you the Y quaternion component, which is not the same thing:
http://unity3d.com/support/documentation/ScriptReference/Quaternion.html
oh I guess I tried to answer the title without reading the question.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101619im_/https://answers.unity.com/themes/thub/images/avi.jpg)