- Home /
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!
What you need is to perform a dot product between the ship's forward vector, and the vector between the ship and your player target. This should be close to 90 in order for the player to be on the left or right side of the ship.
float cosAngle = Vector3.Dot( ship.transform.forward, player.transform.position - ship.transform.position );
float angle = $$anonymous$$athf.Acos( cosAngle ) * $$anonymous$$athf.Rad2Deg;
if ( $$anonymous$$athf.Abs( angle - 90 ) < angleOfView )
TargetPlayer();
Something like that should work. You can optimize further by removing the Acos, as cos(90) == 0. So you'd just have to know the cos of your angleOfView variable. I think.
Hope this helps!
Thanks! This is pretty much working out! How do I do as you suggested and remove the Acos? I figure that is what is causing a lot of the "NaN" I'm receiving.
Answer by robertbu · Jun 01, 2013 at 01:34 AM
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.
As your original code was trying to do, you could use the Dot product ins$$anonymous$$d:
if (Vector3.Dot((v3ToPlayer, transform.right).normalized) > some_value) {
// Do the right side facing stuff
}
some_value here would be greater than 0. 0 is at 90 degrees.
Thanks. This is somewhat the approach I took previously and it almost works. The problem is that occasionally the ship side will be facing an angle that would satisfy it, but it is not facing the player. I'm not sure, it has weird behaviour still.
This code calculates angles between vectors in 3D space. If you are shooting at flying objects, or if you ships are pitching and rolling, you may want to project both vectors down to a plane before doing the calculation. You can do that by:
var v3ToPlayer = goPlayer.transform.position - transform.position;
v3Player.y = transform.position.y;
var v3 = transform.right;
v3.y = 0;
if (Vector3.Angle(v3ToPlayer, v3) < some_value) {
// Do the right side facing stuff
}
I believe that worked like a charm! I'll just finish up the code and see if I can get it running with the shots going off. Then -v3 for left side facing stuff I guess?
Also, why don't I have permission to thumbs up you?
EDIT: Yup, my ai boat is officially destroying me anytime it gets the opportunity to broadside. Thanks a ton robert, I love how helpful this community is
Your answer
Follow this Question
Related Questions
NavMesh flee. Ai flee from player. 4 Answers
How can make rank in racing game? 1 Answer
Keeping Character From Walking Through Walls 1 Answer
Smoke Follow Player 1 Answer
AI enemy script (same as in Hitman) 0 Answers