- Home /
Best rotation function to use
What would be the best rotation function (Quaternion.Lerp, Transform.Rotate...) to rotate an object around it's center axis. The rotation needs to be smooth, there needs to be an option to set the speed, and most important of all, for example if I want it to rotate from 0 degrees to -225 degrees, I don't want the object to choose the shortest rotation and rotate 135 degrees counterclockwise, but I need it to rotate 225 degrees clockwise until it reaches -220 degrees.
I'm a bit lost with all the functions Unity offers and it would be awesome if some of you guys could help me out!
Want to help, but not into the "write it for me" Q's.
I'll suggest you take a look at the docs for all the members of the Quaternion
class, in particular Euler (converts the Quaternion to a number of decrees around each axis), and Lerp
(use this one to animate the rotation, fractionally, towards another rotation), or operator *
(which is how you ADD Quaternion rotations together) ( suggest using Lerp
OR operator *
, not both)
Also take a look at Transform.rotation, this member contains the Quaternion that defines the orientation of the object in world-space and on screen. You'll want to change this value, over time, in order to animate the object.
Lastly, you will need to use Time.deltaTime to compute the amount of rotation to apply in a given frame, based on rotation speed you select.
Hope that helps clear it up a bit!
I'm not asking you to write code for me at all. I'm just asking what you guys think would be the best function to achieve my goal.
For the Quaternion.Euler, do you mean I should use a a Lerp
function nested in a public static Quaternion Euler(float x, float y, float z);
? I tried using Quaternion.Euler but I deemed it not useful because for example:
hinge.transform.rotation = Quaternion.Euler(0,30,0);
didn't give me a smooth interpolation. Am I using this function wrong then?
Thank you for your answer!
"I'm just asking what you guys think would be the best function to achieve my goal." cool :)
I would suggest a very limited use of Euler: in particular just when specifying your "Offset rotation" (this is the one that kind-of needs to be euler angles, since a human specifies it). Once it's a quaternion, you can use operator * to add it to your "Initial" rotation (transform.rotation), to get the "final" rotation.
I would store the "Initial" and computed "Final" rotations as Quatenions, and use Quatenion.Lerp to interpolate between them.
Answer by AurimasBlazulionis · Mar 05, 2017 at 04:43 PM
You have a few options.
1) Multiply the rotation by the specific quaternion. It would be really efficient in most cases, but would require you to do it in fixed update.
private Quaternion _rotation;
public float speed;
private float _speed;
public Quaternion rotation {
get {
if (_speed != speed) {
_speed = speed;
_rotation = Quaternion.Euler (0, speed * Time.fixedDeltaTime, 0);
}
return _rotation;
}
}
void FixedUpdate () {
transform.rotation = transform.rotation * rotation;
}
Or if you want to do it in Update, then you will need to call Quaternion.Euler every frame (which is not that good. You will only need speed variable. That is it.
void Update () {
transform.rotation = transform.rotation * Quaternion.Euler(0, speed * Time.deltaTime, 0);
}
Or you can use Quaternion.LerpUnclamped. It is really cool. The code would look like this:
private Quaternion _rotation;
public float rot;
private float _rot;
private Quaternion startRotation;
private float startTime;
public Quaternion rotation {
get {
return _rotation;
}
}
void Update () {
if (_rot != rot) {
startTime = Time.time;
_rot = rot;
startRotation = transform.rotation;
_rotation = Quaternion.Euler (0, transform.rotation.eulerAngles.y + rot / 3f, 0);
}
if (Time.time - startTime <= 1f)
transform.rotation = Quaternion.LerpUnclamped (startRotation, rotation, (Time.time - startTime) * 3f);
}
Sure you will need to change it. What it does, is when the target rotation changes, it creates a rotation Quaternion of the target rotation and divides the y target rotation by 3 so no the closes path is chosen.
Actually, other than these things (not even sure about the last one), Transform.rotate would work the best and simplest.
with transform.rotate, can I set it to rotate for a specific amount of degrees and then stop?
you can rotate for the time you want. That is basically Until angle / speed seconds pass. So it would be if (Time.time - startTime <= (angle / speed)) transform.Rotate(blah blah blah);
where startTime is when you started to rotate.
Thanks!
I also found someone that wrote a custom Quaternion.Slerp that bypasses the shortest-rotation problem and it works :)
Follow this Question
Related Questions
Basic AI Locked Axis 1 Answer
Change rotation of an object based on an HTC Vive Controller 1 Answer
Rigidbody2d Collision Breaks Quaternion.Slerp 2 Answers
Use Lerp to rotate object 2 Answers