Home Game Development rotation – How does Unity’s FromToRotation work? A significant a part of its conduct appears undocumented

rotation – How does Unity’s FromToRotation work? A significant a part of its conduct appears undocumented

0
rotation – How does Unity’s FromToRotation work? A significant a part of its conduct appears undocumented

[ad_1]

FromToRotation chooses the shortest doable rotation that maps fromDirection to toDirection.

That is, the rotation concerning the axis

axis = Vector3.Cross(fromDirection, toDirection);

via an angle of

angleRadians = Mathf.Acos(Vector3.Dot(
                  fromDirection.normalized, 
                  toDirection.normalized
               ));

This does turn into indeterminate if the 2 instructions are precisely reverse: then a 180 diploma rotation about any axis perpendicular to them is an equally brief choice, so the engine has to decide on one arbitrarily.

I’m at the moment on cellular, so I’ve not examined which axis it makes use of on this particular case, however I’d anticipate it makes use of a routine one thing much like:

var proper = Vector3.Cross(Vector3.up, fromDirection);
if (Math.Approximately(proper.sqrMagnitude, 0)) {
    axis = Vector3.ahead;
} else {
    axis = Vector3.Cross(fromDirection, proper);
}

This offers the y+ axis if the 2 inputs are within the xz airplane, the axis closest to y+ if the 2 inputs aren’t themselves vertical, and the z+ axis of the 2 inputs are vertical. That selection is unfair, however appears to make sense for typical 3D navigation use instances.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here