Sunday, September 4, 2022
HomeGame Developmentrotation - How does Unity's FromToRotation work? A significant a part of...

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


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

That’s, the rotation in regards to the axis

axis = Vector3.Cross(fromDirection, toDirection);

by an angle of

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

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

I am presently on cellular, so I’ve not examined which axis it makes use of on this particular case, however I might count on it makes use of a routine one thing just like:

var proper = Vector3.Cross(Vector3.up, fromDirection);
if (Math.Roughly(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 if the 2 inputs are vertical. That alternative is bigoted, however appears to make sense for typical 3D navigation use circumstances.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments