- Home /
What is the most efficient way to rotate an object?
Let's say I have an object with it's rotation center right on the end (see picture)
When I rotate it 90 degrees, it will obviously rotate 90 degrees clockwise, but when I set the rotation to 270 degrees, it will go 270 degrees clockwise. How can I make it so that whenever the rotation exceeds 180 degrees, the object will rotate counter clockwise instead of clock wise, so that wen I put in a rotation of 270 degrees, instead of going 270 degrees clockwise, it will go 90 degrees counter clockwise.
Thank you very much!!!
Answer by Owen-Reynolds · Jan 11, 2015 at 04:40 PM
A Unity trick is to just specify the final angle, and use one of Quaternion RotateTowards or Lerp/Slerp (which homer3 suggests.) RotateTowards is the simplest (you give the actual amount of degrees to rotate this frame, ex: 90*Time.deltaTime
to go a quarter-turn per second,) unless you've already used Lerp lots for movement.
They always take the best 3D path. Sometimes that can surprise you, as they find a better way to flip than the one you really wanted. But if everything is 2D, they work fine.
If you want them to go 180 one way, and they want the other, or you want a 181+ degree rotation, you can fool them by making a midpoint (rotate to this 90 degrees, now this 91 degrees more.)
The other problem is purely coding. Any wrong spins are because you programmed it wrong. If you have your own rotation number, then of course moving it from 0 to 270 is clockwise. So don't. Move it from 0 to -90 or from 360 to 270, or gradually add -90. The Mathf library has some helpful functions: MoveTowardsAngle and DeltaAngle.
A trick with moving your own angles is not to read from rotation.eulerAngle. It can "randomly" add or subtract 360, and in 3D stuff, can even flip +/-180. Just always keep your personal float spin;
variable, and use that to set rotation=Quaternion,Euler(0,spin,0);
Generally, the Quaternion shortest-path is fine for when it's really just a pretty animation. The more complicated "spin your own variable" is needed when the rotation direction means something (like a clock face where you want to show time going forwards or backwards,) or need to specify things like 2.5 spins.
Answer by homer_3 · Jan 10, 2015 at 07:03 PM
Rotations are mathematical operations. Added 2 + 3 isn't going to me any faster than adding 6 + 2.
It's not about how fast the objects rotates, but I want that the object goes left if the new position is at 181 degrees and that it goes right if the new position is 179 degrees (it's for the opening of a door)
The easiest thing to do is deter$$anonymous$$e which way you want to rotate and then pass in a negative or positive rotation based on that.
So if I say like a rotation to -90 degrees, Unity reads that as a rotation to 270 degrees counter clockwise?
Forget what I said before. I think I just ran into the same issue you are trying to solve. If you are trying to animate the rotation of an object you want to do
xform.rotation = Quaternion.Slerp( xform.rotation, Quaternion.Euler(targetRotation), rotateSpeed * Time.deltaTime);
This will cause the object to take the shortest rotation path to the target rotation.
well, I don't know the math behind quaternions, but it's definitely something I will look into. So the code you gave me is basically this?
object.rotation = Slerp(original rotation, new rotation, speed * Time.deltaTime),
Answer by Jignesh G. · Jan 16, 2015 at 07:39 PM
http://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html
Only localEulerAngles to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.
So it means Transform.Rotate is most efficient way to rotate object. you can also use Space.Self or Space.World according to the requirement.
Your answer
Follow this Question
Related Questions
Rotation: rotate by a defined angle 1 Answer
Problem with "if or" statement. 1 Answer
How to rotate about 360 on a coroutine 3 Answers
How can I rotate an object with certain degrees? 2 Answers
Rotate object in 45 degree steps 1 Answer