After many hours of research and getting in nowhere, I was wonder if anybody here knows how on earth Unity does the Quaternion.LookRotation calculation.
Im asking this because Im working on a school project which I have to create my stuff from scratch (Quaternion class,Vector class etc). So the look operation is the only thing I didn’t managed to work.
I would like to see both ways, meaning the “without the up vector” and “with up vector” if there is any other differences besides the automatic use of Vector3.up in the first one.
If I had to write Quaternion.LookRotation(), I might do something like this:
function MyLookRotation(dir : Vector3, up : Vector3) : Quaternion {
if (dir == Vector3.zero) {
Debug.Log("Zero direction in MyLookRotation");
return Quaternion.identity;
}
if (up != dir) {
up.Normalize();
var v = dir + up * -Vector3.Dot(up, dir);
var q = Quaternion.FromToRotation(Vector3.forward, v);
return Quaternion.FromToRotation(v, dir) * q;
}
else {
return Quaternion.FromToRotation(Vector3.forward, dir);
}
}
Note this is built on Quaternion.FromToRotation(), which you would then have to unravel if you need everything from scratch.