Object Between Two Points

Hi Unity Community. :slight_smile:

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! :slight_smile:

You’re using twice the field of view angle to draw the cone: you rotate the full fieldOfViewAngle from the forward axis of the turret for both the right hand and left hand side of the cone, resulting in your 90 degree cone you show in the screen shot.

But then, in your test to check if the chopper is in the line of sight, you only use half the field of view, so half of the cone. Also, keep in mind that Vector3.Angle() calculates the angle in three dimensional space, whereas you draw the field of view on the XZ plane; if you want to use the field of view in this plane, normalize the position of the chopper on the Y axis so you get the correct angle.

Haha … fresh perspectives really help. Thanks a lot man for pointing out that fieldOfViewAngle, that totally just went passed me.

Also, looking back at the code I wrote for the post (I had to do it by hand because I didn’t commit my work), I realized I might have forgot a reference in the broken version.

I got to stop working on this stuff in the middle of the night. :face_with_spiral_eyes:

1 Like

Just posted to say that it worked! A combination of my noticing the difference in code examples and the angles not being right.

Thanks again for your help!

https://gfycat.com/ApprehensiveYoungIchthyostega

1 Like