- Home /
Help regarding World-Local Transfrom Rotation?
Hello,
I have an object in a Top-Down view. By which I mean to say that the object moves along X and Z axis. Due to it's motion at times, it experiences slight rotation along Y axis. Now the problem is when I try to tilt my iOS device, I would like the object to move along the direction I tilt and rotate at the same time. i.e. by X and Z axis.
CODE :
//Implementing Rotation
angx = Mathf.SmoothDampAngle(transform.rotation.eulerAngles.x, reqAngle.x, ref yVelocity, Time.deltaTime * 2f);
angz = Mathf.SmoothDampAngle(transform.rotation.eulerAngles.z, reqAngle.z, ref yVelocity, Time.deltaTime * 2f);
transform.rotation = Quaternion.Euler(angx, transform.rotation.eulerAngles.y, angz);
// Getting the Input from the accelerometer and determining the angle
{
if(dir.x>0)
reqAngle.z = -(dir.x * 100);
if(dir.x<0)
reqAngle.z = -(dir.x * 100);
}
//END
EXPLANATION
I get the Input and depending upon the Input and calculate the angle to be rotated. Then the angle is used to calculate the Rotation. Now, the object by itself has a rotation along Y axis. Here I never intend to manually rotate the Y axis. The code I have used is transform.rotation
and from the unity documentation it says it is a world rotation rather than a local rotation.
PROBLEM :
When I tilt my iOS device and at that particular instance if the object has experienced some Y degree rotation - to my input the object rotates locally.
I hope my explanation is clear and I am not sure where I have gone wrong, if so. Could someone help me with this.
Thank you and I highly appreciate your help...
Answer by ScroodgeM · Aug 01, 2012 at 09:09 PM
it's bad practice to use euler angles to read data. you can got two different Euler Angles with same rotation. it's normal. so don't use it in this case. try something like:
Quaternion targetRotation = Quaternion.Euler(reqAngle.x, 0f, reqAngle.z); // insert some default Y rotation if needed transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 2f);
Thanks Scroodge. I did implement it. $$anonymous$$y code goes like this.
Quaternion targetRotation = Quaternion.Euler(reqAngle.x, transform.eulerAngles.y, reqAngle.z);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 100f);
But yet again, it rotates along it's local and not world. I am not sure why and what is the issue? The notable changes I made from your reference is that I have kept Y angle to be the same & the speed is Time.deltaTime * 100f.
Is there a problem if I happen to pass transform.eulerAngles.y in Quaternion.Euler?
like i said already, be sure in what are you doing if you read euler angles and use it.
if you need just store initial eulerAngles.y and keep it, then store it in local variable once before any rotation and use it when rotating.
these lines will rotate an object always by it's Z and X axis
if you need rotation always in world space (like trackball), use Rotate method:
transform.Rotate(Vector3.forward, deltaZrotation, Space.World); transform.Rotate(Vector3.right, deltaXrotation, Space.World);
say if you need explain