- Home /
How to rotate two axes independently, simultaneously
Hi all,
I've done as much searching as possible (and a whole lot of experimenting), but I can't quite seem to solve this problem I'm having. Essentially I want to rotate an object on two axes simultaneously, but independently. I've drawn a quick sketch to show what I mean:
Ideally I'd like both rotations to be physics (rigidbody) based, but I'm happy to settle for an approximation using slerps. The closest I've come so far is by using a rotation for the Y rotation:
transform.RotateAround(transform.position, transform.up, rotateForce);
Whilst using torque for the Z rotation:
rigidbody.AddRelativeTorque(Vector3.forward * tiltForce);
This works, but is nowhere near robust and the object quickly ends up in strange positions, for example rotated around its X axis (I'd like to avoid any rotation in the X axis if at all possible?).
I'm pretty stumped, so some tips to get me looking in the right direction would be very helpful. I've got this nagging feeling I should be doing something with Quaternions, but I'm not quite sure what! Thanks very much.
Answer by adslitw · Jan 06, 2013 at 04:08 PM
OK, as always (in my case at least), resorting to posting on a forum always guarantees I'll figure it out. For those who come here looking for the answer, here's my solution:
// Rotate around Y axis
transform.RotateAround(transform.up,Time.deltaTime * YForce);
// Rotate around Z axis
transform.RotateAround(Vector3.forward, Time.deltaTime * ZForce);
Place these in update() or fixedupdate() and create the YForce/ZForce variables to be whatever you want (I'm getting input from an Xbox controller).
Answer by Piflik · Jan 06, 2013 at 02:53 PM
Th usual approach is to separate the rotation. Parent the object to an Empty, rotate that around the global Z-axis and rotate the child around it's local Y-axis.
Answer by Alexander-Perrin · Jan 06, 2013 at 02:28 PM
Why can't you just use transform.Rotate(new Vector3(0, yRotation, zRotation), Space.Self);
? That should rotate the transform by 'yRotation' units around the local y-axis and 'zRotation' units around the local z-axis
Unfortunately when you do that you end up with a combination of the rotations, i.e. the Y rotation does not occur around the Y-vector in the diagram above. I'm still experimenting, so I'll be sure to post an answer if I figure it out. Whatever I do seems to end up with rotations in X axis!
Answer by IntergalacticSloth · Jul 03, 2017 at 03:47 PM
TL DR
After 16 hours, I got my two axes rotating cleanly. And I did indeed have to use parenting — but even that didn't work for a long time
I just wanted to have code that causes an object to turn in circles and also front flip as it turns. OR IN OTHER WORDS: rotate yaw and pitch without any effect on roll.
Here's what finally worked.
In Unity:
Create an empty. Call it ForYaw.
Give that a child empty, called ForPitch.
Finally, make a cube and move it to z= 5 (forward). Now we can see what we are trying to avoid, which will be twisting the cube (accidental "roll" while we yaw and pitch).
So you have YawObject/PitchObject/Mesh to see. However, the details of the above don't matter much. As long as PitchObject is child, what happens next should work.
Now, using C# in Visual Studio or whatever, do your setup. This goes in Update:
objectForYaw.Rotate(objectForYaw.up, 1f, Space.World);
objectForPitch.Rotate(objectForPitch.right, 3f, Space.World);
//this will work on the pitch object as well
//objectForPitch.RotateAround
// (objectForPitch.position, objectForPitch.right, 3f);
That's what worked for me! Good luck!
PS.
Note that it all broke for me when I tried to vary the order of parenting. It also breaks if I change Space.World for the child pitch object.
Yes, I could use Quaternion functions... on the parent. The parent-Yaw object is pretty tough. But Quaternion approaches failed me for the pitch. I really needed Rotate, or RotateAround (applied carefully) .
I'm confused for sure. I could definitely use some clarification on why I had to take a local axis but apply it through world space. Even in my solution, why does Space.Self ruin everything?
Your answer
![](https://koobas.hobune.stream/wayback/20220613090505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
add force to rotation 1 Answer
Rotation Force on a model 2 Answers
Rotating a rigid body to face left or right smoothly 4 Answers
how can I rotate object reading from excel data & time steps 3 Answers