Enemy detecting multiple players and engages one closest

the best way I would think is raycast but I’m sorta stuck how would I go about doing it using unity’s nav mesh system

physics.Spherecastall()

is what your looking for

it returns ALL colliders in a given radius.

int his case for example spherecastall(5) at the enemy position would give you all colliders within five meters.

now that you’ve got only those within 5 you can then sort by closest.

This is probably easiest done by simply assigning straight off the first in the list as closest then comparing.

collider nearest;
float distance = 999; // by setting to 999 to start we ensure the first time we check, the first item is //always closer (than 999) so when we having nothing to compare to, the first item is always the closest, 

foreach player in physics.spherecastall()
{
if (vector3.distance(player.transform.position, this.transform.position) < distance)
{
nearest = player;
distance = vector3.distance(player.transform.position, this.transform.position) ;
}
}

That will search the list for the closest collider.

NOTE THIS IS PSEUDOCODE

that can’t just be copy pasted, it’s like 90% code but it’ll need minor changes to be actual able to compile.

at the end of that for loop, the collider stored in nearest will be the well nearest, that will be your desired target.

The float awarenessDistance variable coupled with the Gizmo to draw a visual representation of the radius, is the reason I’m posting this helpful (I hope), snippet with a zip containing the meshes you need. (The other reason is I wanted to give back to the community if I can).

In the public variable Area Mesh assign the mesh called pTorus1 found in the AreaRing prefab. For the Target Arrow assign the TargetArrow mesh found from the TargetArrow prefab.

In the end you can use the awarenessDistance variable to check the distance in the IF block that sparkzbarka provided. At the same time you can visibly see the radius to help you out.

Just attached this code to the NPC you want and put the prefabs in the zip files somewhere in your asset folder.

PS: You don’t need to use all the variables defined in my code snippet.

[109439-targetarrow.zip|109439]

[109440-arearing.zip|109440]
[Header(“AI Sensor Settings”)]

    public float awarnessDistance = 20;
    public float aimingDistance = 15;
    public float attackDistance = 8;
    private float playerActualDistance = 99999;

    [Range(10f, 360f)]
    public float lookAngle = 90f;
    public float hearingDistance = 20;
    public Transform eyesAndEarTransform;
    private Transform actualSensorTrans;
    private Vector3 actualSensorPos;
    public Mesh AreaMesh;
    public Mesh TargetArrow;

public virtual void OnDrawGizmos()
    {
        //Gizmos.color = Color.white;
        //if (playerTransform && playerIsVisible)
        //{
        //    if (eyesAndEarTransform)
        //        Gizmos.DrawLine(playerTransform.position + new Vector3(0, eyesAndEarTransform.position.y, 0), eyesAndEarTransform.position);
        //    else
        //        Gizmos.DrawLine(playerTransform.position + new Vector3(0f, 1.6f, 0f), transform.position + new Vector3(0f, 1.6f, 0f));
        //}

        Quaternion lRayRot = Quaternion.AngleAxis(-lookAngle * 0.5f, Vector3.up);
        Quaternion rRayRot = Quaternion.AngleAxis(lookAngle * 0.5f, Vector3.up);
        Vector3 lRayDir = lRayRot * transform.forward;
        Vector3 rRayDir = rRayRot * transform.forward;
        if (eyesAndEarTransform)
        {
            Gizmos.DrawRay(eyesAndEarTransform.position, lRayDir * awarnessDistance);
            Gizmos.DrawRay(eyesAndEarTransform.position, rRayDir * awarnessDistance);
        }
        else
        {
            Gizmos.DrawRay(transform.position + new Vector3(0f, 1.6f, 0f), lRayDir * awarnessDistance);
            Gizmos.DrawRay(transform.position + new Vector3(0f, 1.6f, 0f), rRayDir * awarnessDistance);
        }

        Gizmos.DrawMesh(AreaMesh, transform.position, Quaternion.identity, Vector3.one * awarnessDistance);

        Gizmos.color = Color.yellow;
        Gizmos.DrawMesh(AreaMesh, transform.position, Quaternion.identity, Vector3.one * aimingDistance);
        //Gizmos.DrawSphere(lastNoisePos, 1.0f);

        Gizmos.color = Color.red;
        Gizmos.DrawMesh(AreaMesh, transform.position, Quaternion.identity, Vector3.one * attackDistance);

        Gizmos.color = Color.blue;
        Gizmos.DrawMesh(AreaMesh, transform.position, Quaternion.identity, Vector3.one * hearingDistance);

        //Gizmos.color = Color.magenta;
        //Gizmos.DrawMesh(TargetArrow, finalGoal, Quaternion.identity, Vector3.one);


    }