- Home /
Manipulate an objects rotation
I am using the accelerometer to control my object, it sits in one spot and the accelerometer controls its rotation, turning left and right. At the moment, it spins the entire way around, doing a 360.
What I would like is to limit its turning so that I can never look behind my object, hopefully the code explains it better:
//This turns the object but allows 360 turning
var accelDirection = Input.acceleration.x;
transform.Rotate(Vector3.up * accelDirection * 5);
//I would like something like
if(myObject.transform.rotation >= 90)
{
//Stop turning
}
As it stands, the editor is saying I can't compare a Quaternion and an int (obviously), so is there a way I can set up what I'm looking to achieve?
Any help would be appreciated
Because you cant compare 2 objects of different types. Rotation have 3 member variables in it: X, Y, Z try doing it like this:
if(obj.transform.rotation.y >= maxRotAngle)
{ /* StopTurning */ }
Cheers for the input lads, i'll test it out when I get home
Answer by robertbu · Jul 24, 2014 at 01:49 PM
'transform.rotation' is a quaternion, and it's x, y, z, and w values are not angles. You can use transform.eulerAngles as long as you are only rotating on the 'y' axis.
@robertbu, eulerAngles proved the most effective method for me. If you convert your comment into an answer i'll select it, cheers
Restricting rotations can get ugly in Unity. As long as the you are only rotating on the 'y' axis, and the 'x' and 'z' rotations are at or near 0.0, you should be fine checking eulerAngles. For more complex rotation situations, you will find that checking eulerAngles will fail, and therefore you must take other approaches.
For what i want,I'm only rotating the y-axis, x and z won't be touched. But thanks for the info, good to know
Your answer
Follow this Question
Related Questions
Rotate with raw gyro data. 0 Answers
How can i rotate a object more than once (90 degrees)? 1 Answer
Yaw Pitch Roll order? 0 Answers
Can this be rewitten to control axis locally instead of globally? 1 Answer
Rotate object permanently around Y-axis while rotating around Z-axis to face mouse? 1 Answer