A dot product is another piece of math that you’ll find invaluable for game dev. It allows you to get a value between 1.0f and -1.0f that tells you the angle between two vectors and their relative orientation.

This example shows the scale of the spheres on the end of the lines being set to the dot product of the angle between them. It illustrates nicely the value of the dot product in a visual form

So, how do you calc a dot product? Pretty simple…

float dot = vecA.x * vecB.x + vecA.y * vecB.y + vecA.z * vecB.z;

‘dot’ will be the dot product between the two vectors (vecA and vecB)

Note: In Unity, you can use Vector3.Dot(vecA, vecB) but I prefer to do the math long-hand so you can see how it works AND so you can use it when you’re not in Unity

It’s important to note that the dot product value will be wrong if the vectors are not normalised.

So, uses…

  • If the dot product of two vectors is 0.0 then the vectors are at right angles
  • If the dot product is 1.0 then the two vectors are facing the same way
  • If the dot product is -1.0 then they’re facing opposite ways
  • If the dot is < 0 then the angle between the vectors is obtuse (> 90 degrees)
  • If the dot is > 0 then the angle is acute (< 90 degrees)

You’ll also find that the dot product is used in…

  • Calc which side of a line a point is on
  • Various lighting calculations
  • Reflection of a vector
  • Figuring out “Field of View” for enemies and the player (“what can I see”)
  • Many more

I’ll add examples of all of these uses as time goes on so you can see how to use them


Discover more from

Subscribe to get the latest posts sent to your email.

Leave a comment

Trending