How to Compare the Orientation between a Character and its Camera

I’m making a 3D fighting game where the character can act in 4 directions. Forward, Left, Right, and Rear. The player can input forward, left, right, and back inputs to perform an animation in these 4 directions. Because the camera can rotate around the character to allow the player to better see their surroundings, I need a function the determines which direction the player intends to perform an action in and plays the appropriate animation.

For example, if the camera is facing the player head on and they press forward on their controls to attack behind them, they play the back animation as the character’s rear is the player’s forward. If the camera is facing the characters right side and they press left on their controller, they play the rear attack animation as the character’s rear is the players left.

The function I’m writing takes the current direction the camera is facing using a forward vector, compares its orientation to the forward vector the character is facing and plays an animation accordingly. If the dot product of the two vectors is 1, then the camera is facing the same direction of the player.

The problems is the function doesn’t work as sometimes the player will play the rear animation when the camera and player are facing the same direction and I input a forward attack. How can I fix this?

This is it working correctly.

Rear Animation on Forward input with forward direction

This is it working incorrectly.

Forward Animation on Rear Input with Rear direction

I haven’t really looked at this in detail, but I did notice you need to take ACOS of the dot product to get the angle…