- Home /
How to rotate without a target vector
I'm feeling dumb. My desired effect is to rotate a sphere 45 degrees to the left, then 45 degrees to the right and then back to the original point in the middle. This rotation needs to be relative to the sphere itself, independent of world space.
So far all the solutions I've looked into involve assigning an object or point as the target for the rotation amount. Is there a clean way to accomplish this without a target?
If I'm just missing something super obvious somewhere, feel free to point me in that direction. Thanks!
Answer by s_guy · Oct 15, 2013 at 02:41 AM
I would use quaternion multiplication to determine your target rotation. You will need to convert Euler angles to quaternion. Left and right need a frame of reference in 3r space, so pick an axis where your 45 degrees will apply. Here's an example using the Z axis.
float degrees = 45;
Quaternion startRotation = transform.rotation;
Quaternion endRotation = startRotation * Quaternion.Euler(0f, 0f, degrees);
Then, presuming you want to do this smoothly over time, you will need some means to iteratively progress to your target. I would use a coroutine. With each iteration, you could have Quaternion.Slerp() or an easing function to enact the incremental rotation each Update().
Take a good read over these reference pages:
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html
http://docs.unity3d.com/Documentation/Manual/Coroutines.html
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html
Good luck!
On the right track here, but order matters. Plus usually when you want a left/right rotation it means rotating around the 'y' axis. I believe you want:
Quaternion endRotation = Quaternion.Euler(0f, degrees, 0) * startRotation;
Order of multiplicands depends on what you're after, yes? The quaternion multiplication will first apply the left hand side, then the right hand side.
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-operator_multiply.html
Since the original poster wanted a delta from the given starting orientation, this seems (experimentally too) to give the right result. Am I confused somehow?
I'm not sure how to parse the web page you reference. Run the test yourself. Start with cube with a rotation of (45, 45, 45). Use Euler(0,45,0). When I the equation in the order I specified, I get (45, 90, 45). When I do them in the order you specified I get (8.42, 75.36, 59.64).
Your answer
Follow this Question
Related Questions
My object isn't rotating to the transform of another object. How do I fix this? 1 Answer
Rotating a bone via scripts 1 Answer
How do I control the transform.position of an instantiated prefab? 2 Answers
transform.rotate moving object away somehow, 0 Answers
Make a Move Vector rotate around axis to run animation. 0 Answers