Hi Unity Community.
I’m currently working on having an enemy AI shoot at the player if both in range and if the enemy has turned to face the player. I’ve been able to calculate the range and have the enemy look at the player as well as create a crude debug cone of sight:
And the cone:
Using the help Unity tutorial here: http://unity3d.com/learn/tutorials/projects/stealth/enemy-sight , I tried to adapt it so that I can calculate if the chopper is inside the cone, though it didn’t seem to work. It’s hard to explain, but it seems that there’s a single area just south of the enemy that will shoot (despite the cone spinning with the launcher).
Here’s the code, please forgive the dirtiness since I’m just trying to get it to work at the moment haha.
public float FireRange = 75f;
public float fieldOfViewAngle = 45f;
void FixedUpdate()
{
// Create the debug cone
Quaternion spreadAngle = Quaternion.AngleAxis(fieldOfViewAngle, new Vector3(0, 1, 0));
Vector3 newVector = spreadAngle * Turret.transform.forward;
Ray ray = new Ray(Turret.transform.position, newVector);
lineRight.SetPosition(0, ray.origin);
lineRight.SetPosition(1, ray.GetPoint(FireRange));
spreadAngle = Quaternion.AngleAxis(-fieldOfViewAngle, new Vector3(0, 1, 0));
newVector = spreadAngle * Turret.transform.forward;
Ray ray2 = new Ray(Turret.transform.position, newVector);
lineLeft.SetPosition(0, ray2.origin);
lineLeft.SetPosition(1, ray2.GetPoint(FireRange));
// Determine if chopper is in range
Vector3 direction = _target.transform.position - Turret.transform.position;
float angle = Vector3.Angle(direction, Turret.transform.forward);
if(angle < fieldOfViewAngle * 0.5f)
{
print ("It's PEW TIME");
}
}
I had to redo the the code by hand (forgot to commit last night), but I think that’s everything.
Thanks!