Comments and answers for "Best rotation function to use"
http://answers.unity.com/questions/1321864/best-rotation-function-to-use.html
The latest comments and answers for the question "Best rotation function to use"Comment by alexanderameye on alexanderameye's comment
http://answers.unity.com/comments/1321981/view.html
Thanks!
I also found someone that wrote a custom Quaternion.Slerp that bypasses the shortest-rotation problem and it works :)
http://answers.unity3d.com/questions/478617/not-interpolate-quaternion-on-shortest-path.html#answer-1304777Sun, 05 Mar 2017 18:55:26 GMTalexanderameyeComment by AurimasBlazulionis on AurimasBlazulionis's comment
http://answers.unity.com/comments/1321978/view.html
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.Sun, 05 Mar 2017 18:53:33 GMTAurimasBlazulionisComment by alexanderameye on alexanderameye's answer
http://answers.unity.com/comments/1321945/view.html
with transform.rotate, can I set it to rotate for a specific amount of degrees and then stop?Sun, 05 Mar 2017 17:38:03 GMTalexanderameyeAnswer by AurimasBlazulionis
http://answers.unity.com/answers/1321908/view.html
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.Sun, 05 Mar 2017 16:43:27 GMTAurimasBlazulionisComment by Glurth on Glurth's comment
http://answers.unity.com/comments/1321905/view.html
I guess I suggested the wrong method, sorry bout that!
Quaternions in unity are actually "unit quaternions": meaning, they specify an orientation, not an amount of rotation. So that" lack of direction control" makes sense for Quaternion.Lerp.
So, rather than Lerp to a "final" Quaternion, you will need to increment manually (using operator \* ), by an offset Quaternion. It's this offset rotation that will need to specify your direction. At this point I should mention you can also use operator \* to scale a Quaternion by a float. You'll need to this to compute both the offsetPerSecond Quaternion (initially), and the currentFrameOffset (using time.deltaTime) while animating.Sun, 05 Mar 2017 16:33:58 GMTGlurthComment by alexanderameye on alexanderameye's comment
http://answers.unity.com/comments/1321899/view.html
Indeed, that's what I got (I think).. this is a piece of my code:
hinge.transform.rotation = Quaternion.Slerp(hinge.transform.rotation, FinalRotation, Time.deltaTime * RotationTimeline[m].Speed);
so it Slerps between the rotation of the hinge, and a Final Rotation. Both rotations are Quaternions and it works fine. Except that the function uses the quickest route when interpolating. And I have no control over the clockwise/counter-clockwise aspect of the rotation.Sun, 05 Mar 2017 16:15:15 GMTalexanderameyeComment by Glurth on Glurth's comment
http://answers.unity.com/comments/1321897/view.html
"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.Sun, 05 Mar 2017 16:07:08 GMTGlurthComment by alexanderameye on alexanderameye's comment
http://answers.unity.com/comments/1321885/view.html
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!Sun, 05 Mar 2017 15:41:35 GMTalexanderameyeComment by Glurth
http://answers.unity.com/comments/1321872/view.html
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!Sun, 05 Mar 2017 15:27:26 GMTGlurth