Let’s talk about the dot product in game development

How to use the dot product operation in Unreal Engine using C++ and Blueprints

When I work on gameplay mechanics, I often stumbled across situations where I need to apply maths.
When implementing my NPC (Non-player characters), I face a similar situation like the example I will show you here.

The dot product operation is one of them, it’s handy!
In this example, we have an intimidating giant blue cat stares at my salmon sashimi….

Don’t worry, the cat is friendly and only looks at you if you are facing it, and right there we have a clear situation where we can apply the dot product.
Am I facing the cat or not?

To illustrate this example, I am using C++ and Blueprints on Unreal Engine 4.26.

You can follow the video tutorial here and the project files here.

Dot Product Concept

The dot product is an operation between 2 vectors, which returns a float number.

  • If Dot Product is greater than 0, the cat and the robot face the same direction. (They are looking at each other)
  • If Dot Product is equal to 0, the cat and the robot face perpendicular direction (The robot is looking at the side of the cat)
  • If Dot Product is less than 0, the cat and the robot face the opposite direction (they are back to back)

You are likely using an engine such as Unreal Engine 4, Unity ur other if you are making a game.

These engines provide you with the basic operations, so you don’t have to worry about them.

But just in case you are interested here is how you calculate the dot product:

The code

For this example, I will have the blue cat on the level, and the robot-character you control.

When you approach the cat and enter a box trigger, an event is fired.
At this moment, we calculate the dot product between the Character’s vector forward and the cat’s vector forward.

If the robot and the cat are looking at each other, a simple widget UI will show, and the cat will play a meow sound.

The cat is an actor which extends from Character.

I included an UBoxComponent Trigger to detect when the player is close.
The trigger subscribes to the event OnBeginOverlap and EndOverlap, to detect the presence of the player:

Is that simple!

As you can see, I take the vector forward of the player and the cat. The vector forward is a vector that faces a straight direction from the player, as you can see in this picture:

The forward vector has length 1, and it guarantees gives you satisfactory results.

I recommend using this vector for these operations.
In the original code, (link below) you can see more actions taken when the dot product is 0.

In the video that goes along with this tutorial, I will show how the Cat has been set up and how I use the events as well, make sure to check it out!

With those few lines of code, we can implement different logic.
To illustrate the example better, here are the 3 different situations we can find while controlling the robot.

Different situations with dot product

Dot Product >0.0 , Same direction
As you can see in the picture, we can detect if the player is behind the cat, in this situation we don’t want the UI to be shown

DotProduct < 0.0 Opposite direction
This is our ideal situation for this particular example

Dot Product is 0, The player is looking at the side of the cat
This situation is rather strange but can be useful for my next example.

One extra example

In my next example, I use a simple box with a box trigger, similar to the cat, but in this case, it’s a simple actor.

When the player approaches the box and interacts with it through a key, the box will move on a particular direction.

This specific example comes from a tutorial I made about pushing a box with the player. I use the same principle to detect where the player was going to push the box.

I use the box’s forward vector and the character’s and the right vector of the box.
The right vector is similar to the forward vector, and it’s located on the right side of the actor.
Both the forward vector and right vector has length 1.
Here is a picture of the box:

The long red arrow and the short one on the axis-gizmo indicate the forward direction.

The green arrow indicates the right vector of the box.

In the next piece of code, I calculate the dot product using the forward and right vector for each direction.

These calculations will give me 0 or 1, so I detect which side of the box the player is.
As you can see I use the function Clamp to make sure that the values are between 0 or 1:

Using simple if statements, I can make a decision:

Where you can find the dot product in games:

  • To detect the direction on where a box is being pushed by a player.
  • To check what direction the player and an NPC are facing.
  • In shaders, to check how the light affects an object.

A few words about me

Thank you for reading this article. I genuinely enjoy writing about programming, maths, videogames and combine everything together!

I am currently working on a new indie game called Witchery Academy. The game is made in Unreal 4, and it’s great to share some of my tricks and systems I use.

In Witchery Academy, you live the adventures of a wizard apprentice, live at the academy and attend different classes like the botanic class where you take care of your own garden.
And you have a spirit companion which is a a cat!

If you like cats, wizards, magic and a soothing experience, this game is for you!

If you don’t want to miss any updates, you can sign up for our newsletter:

I am Bee, an in indie developer creating Witchery Academ a life sim where you live the adventures of a wizard apprentice with your spirit companion

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store