- Home /
Apply a Rotation in World Space based on Quaternions
This seems like a simple question but I couldn't get it to work. I want to rotate a game object using a quaternion, relative to world space.
I have tried this:
GameObject head = GameObject.Find("head");
head.transform.rotation = new Quaternion(0,0,0,0);
newRot = new Quaternion(xrot, yrot, zrot, wrot); //(I have the Quaternion values from mocap software)
head.transform.Rotate(newRot.eulerAngles.x, newRot.eulerAngles.y, newRot.eulerAngles.z, Space.World);
But it still seems to rotate in the object's local space.
Answer by Jesse Anders · Mar 23, 2011 at 10:20 PM
Assigning a quaternion of (0, 0, 0, 0) may have unpredictable results. Without checking I can't tell you exactly how Unity will handle this, but if the intent is to reset the orientation to identity, assign Quaternion.identity instead.
Also, make sure xrot, yrot, zrot, and wrot are actually intended to be used as the elements of a rotation quaternion. (I'm assuming they are based on the use of the 'w' element, but it's probably worth checking nonetheless.)
Finally, I'm not sure what the behavior of Rotate() is when the object is a child object and the rotation is specified in world space. If Rotate() doesn't handle this case correctly though, you can apply the rotation directly by modifying transform.localRotation (with a little quaternion math, you can achieve the effect of the rotation being applied in world space).
Sorry if my answer is a little vague, but I'd have to check some things in order to be certain of the above answers.
Answer by MLF · May 09, 2013 at 06:13 AM
It's pretty easy:
head.transform.Rotate(xrot, yrot, zrot, Space.World);
Or
head.transform.rotation = newRot * head.transform.rotation;
Your answer
Follow this Question
Related Questions
Converting Raycasted Point to Local Space 2 Answers
Problem with quaternion rotation zxis 0 Answers
Move object relative to its axis 2 Answers