Player sneak attack from behind

What ways can I make the player snap the neck of an enemy from behind?

I’m using the third person template modified to true fps.
I just want to know the best way of doing it.

The snapping itself is an animation. Don’t know how this is done, but the sneak and attack from behind is just some math.

Sneaking should be marked with a bool. Something like “bIsSneaking”.

To calculate if you are behind the enemy, you just need to use the dot product of 2 normalized vectors.

The first one should be the forwardvector of the enemy (which is normalized from beginning) and the second one should be the direction vector from the player to the enemy (enemy location minus player location and normalize it).

If both vectors are the same, then the dot product will be 1. If the direction vector is pointing in the opposite direction, the dot product will be -1.

If we only look at the x and y value, then it is (x,y) * (x,y)

With forward = (1,0) and the direction vector = (-1,0), you will get the -1.

If the vector is facing right or left, it will be (0,1) or (0,-1). So the product will be 0.

With this you can check if the value is between 0.3 and 1 or something like that, it is likely that you are behind the enemy.