- Home /
Quaternion.Slerp on local axis
I have a Playercontroller gameobject which is looking at the center of the game world with LookAt function. That Playercontroller has a child object which has the graphics on it and I would like to rotate that child gameobject in its local axis(for example when A is pressed it rotates it to the left and when D is pressed then to the right)
This is what i use for now in the child gameobject for WASD input:
if (Input.GetKey ("a")) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(-90, Vector3.up), Time.deltaTime * rotationSpeed);
}
It works at the beginning because the playercontroller has 0, 0, 0 rotations, but obviously the child object doesnt rotate correctly when i start to move. I have tried transform.localRotation but it gets really weird results(continuously rotation, does not stop at wanted Y axis degrees).
Any help or guidance would be greatly appreciated
Answer by Baste · Mar 23, 2015 at 04:53 PM
If you want to rotate your current rotation a certain number of degrees, you multiply the rotation you want "added" with your current rotation. It's weird, I know, but Quaternions are Quaternions.
So I'm pretty sure this would work:
if(Input.GetKey ("a")) {
Vector3 currentRotation = transform.rotation;
Vector3 wantedRotation = currentRotation * Quaternion.AngleAxis(-90, Vector3.up);
transform.rotation = Quaternion.Slerp(currentRotation, wantedRotation, Time.deltaTime * rotationSpeed);
}
This means that you will, when holding down a, Slerp towards a rotation that's 90 degrees counterclockwise from your current rotation. This should give a constant rotation speed.
Hope that helps!
Your answer
Follow this Question
Related Questions
Lookat via Single Axis 0 Answers
Rotate object on one axis with slerp 1 Answer
Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers
Finding the Euler angles of an object's rotation in terms of a different set of axes 0 Answers
LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers