Strange angle computing from car tutorial

Get the car tutorial from Unity tutorial, and when read the C# source in file CarController.cs, I find this line of code:

float angle = -Mathf.Asin(Mathf.Clamp( Vector3.Cross(veloDir, carDir).y, -1, 1));

I don’t understand what’s angle is, and if you can explain, please also comment why only use y axis. thanks.

This is a smart way to get the angle between two horizontal vectors - Vector3.Angle is easier but only returns the absolute value, thus you can’t say if it’s to the right or to the left.

The cross product A x B returns a vector perpendicular to A and B, and with magnitude proportional to the sine of the angle between them. If A and B are unit vectors (with length = 1), the length of the vector returned is Abs(Sin(angle)).

In the case above, carDir and veloDir are probably horizontal vectors, thus the resulting cross product will be a vertical vector - and its Y value is numerically equal to the sine of the angle between them.

For more information about cross product, read the Mother of All Knowledge (aka Wikipedia): Cross product - Wikipedia