Math in Unity : Cross Product (Part I)

Matteo Lo Piccolo
3 min readOct 28, 2021

We have seen how the Dot Product works, today we will see the, Cross Product.
As always I leave you the formula.

If we have two Vectors, v and w :

v = (v.x, v.y, v.z);
w = (w.x, w.y, w.z);

v Cross w=
(v.y * w.z - v.z * w.y,
v.z * w.x - v.x * w.z
v.x * w.y - v.y * w.x)

I know, it sounds very complex, but it’s actually very very simple:
as the name suggests, CROSS,
x =
the product of v.y * w.z - the product of v.z * w.y

y =
the product of v.z * w.x - the product of v.x * w.z

z =
the product of v.x * w.y - the product of v.y * w.x

Here is the reference to Vector3.Cross in Unity:

The result is a third Vector which is PERPENDICULAR to the other two Vectors.

VERY IMPORTANT NOTE: Cross product is NOT REVERSIBLE, so v cross w IS NOT EQUAL TO w cross v.
The problem is that the result will be the same but the DIRECTION, which will be EXACTLY OPPOSITE. And it is exactly the problem that the rotation is clockwise or counterclockwise

To better understand the rotation problem, let’s take a very, very simple example.

Imagine these two scenes:
the forward of the Player is 0, 1, 0,
let’s say for example that the distance between the player and the enemy is 0.7, 0.7, 0.

Rotation Clockwise to the right

and we have another scene completely specular, like this.

Rotation AntiClockwise to the left

Foeward is always 0, 1, 0,
but the distance from enemy here is
-0.7, 0.7, 0

If we now calculate the Cross product, the result would be exactly this:
first scene, 0, 0, -0.7
second scene is 0, 0, 0.7

The first thing you notice is that the result is the same except that in the first scene the Z axis is NEGATIVE, while in the second scene it is POSITIVE.
So in the first scene, where our sprite must rotate to the RIGHT, the rotation is NEGATIVE to be CLOCKWISE, while in the second scene, where the sprite must rotate to the LEFT, the rotation is POSITIVE to be ANTICLOCKWISE.

To reduce everything to a formula, we could simply say that :
if the Cross product of Z axis < 0 then the rotation will be CLOCKWISE,
while if
the Cross product of Z axis > 0 the rotation will be ANTICLOCKWISE.

In the next article we will see all of this applied to the code!



Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!