Enemy Broadside AI

So I’ve got the details of an enemy broadside laid out, the only problem being that I can’t figure out a way to detect if the side of the enemy ship is facing the player target. I also need to know which of the two sides is facing the player target in order to do either a left broad side or right broad side depending.

I have been toying away for hours but I still can’t come up with anything that functions.

Any help would be greatly appreciated!

First your ship script will need access to either the player game object or the player transform. You can either link them in the inspector or use GameObject.Find() to initialize the variable. Once you have the variables you can do something like this:

var v3ToPlayer = goPlayer.transform.position - transform.position;

if (Vector3.Angle(v3ToPlayer, transform.right) < some_value) {
   // Do the right side facing stuff
else if if (Vector3.Angle(v3ToPlayer, -transform.right) < some_value) {
   // Do the left side facing stuff

‘some_value’ defines how much angle you will allow and still shoot the broadside.