- Home /
How to rotate an object around the world axis but not on the local axis?
Hello Devs,
I have a game object for which I am not sure of it's X or Z angle during run time. The game object is highly altered during runtime and the angles across X,Y and Z would be changing. But when the user gives the Input, it's angles are directly influenced only by the Input given by the user. At any point, the user might give an input as a target angle and I would like the game object to rotate from it's rotation to the desired target angles. The INPUT is basically only for X and Z and never Y. But while performing this I would like the angle on Y axis never to be affected. I would like to have the rotation on WORLD and not LOCAL.
Restrictions :
Target Angle based upon the user INPUT.
GLOBAL rotation
Angle across Y axis should not be affected.
I have hitting across this for a long time, taking breaks in between. Can someone help me out to sort this?
THANK YOU.
regards,
Karsnen
The question is: what do you want to achieve? Please provide screenshots or diagrams of what you mean.
What do you mean by user input? How does the user provide this input.
Answer by Kryptos · Aug 29, 2012 at 02:36 PM
Hints: the Transform class offers two different variables: rotation and localRotation. The first one is the world rotation, the second is the local rotation.
edit: I understand your mistake. Do you know that a Quaternion has 4 elements and that those elements have nothing to do with the xyz axis?
If you want to retrieve the angle around x, y or z-axis, use transform.eulerAngles. But I would recommend to never deal with euler angles. Instead, you can deal with direction.
Yeah I have been using transform.rotation = Quaternion. The below code is what I have been using.
//START
anglcal();
targetRotation = Quaternion.Euler(reqAngle.x, this.transform.rotation.y, reqAngle.z);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime*100f);
//EOF
If you happen to look into this code, Y angle remains the same but there is no Global rotation.
$$anonymous$$ryptos - Do you see a mistake in my code?
What do you mean by there is no Global rotation?
You should consider using direction ins$$anonymous$$d of angle, along with Quaternion.FromToRotation.
http://postimage.org/image/5m4iw8vv5/
If you happen look at the image about. At certain point the object suffers Y rotation but then when I apply the above code - the GameObject rotates locally. The above image has suffered a local X axis rotation but it had to be global X axis.
Sorry but with only one image, how am I supposed to understand what's going on?
I have set of screenshots. I hope it helps to clear what I am trying to say.
Normal position - When there is eulerangles are zero across X, Y and Z http://postimage.org/image/f1b46v7k1/
When the game object suffers rotation across Y axis. http://postimage.org/image/nojq74w01/
Expected GLOBAL rotation http://postimage.org/image/dsin7hq81/
How it actually rotates - LOCAL http://postimage.org/image/8js7ncx6p/
Thank you.
Answer by Tryz · Feb 07, 2015 at 04:09 PM
If you want to just use quaternions, you can do this:
Vector3 lSpeed = new Vector3(0f, 90f * Time.deltaTime, 0f);
Quaternion lCurrent = gameObject.Transform.rotation;
Quaternion lNew = lCurrent * Quaternion.Euler(Quaternion.Inverse(lCurrent) * lSpeed);
In the last line, it counteracts the current rotation. So, when we add the speed to the current rotation, it results in a rotation around the world axes.