-2
 GameObject GetTarget()
    {
        GameObject TopTarget=null;
        Collider[] col = Physics.OverlapSphere(transform.position, checkRadius, checkLayers);
        Array.Sort(col, new DistanceComparer(transform));
      
        foreach (Collider item in col)
        {
            if(item.CompareTag("HurbFood"))
            {
                
                return TopTarget;
            }
            else
            {
                Array.Clear(col, 0, 100);
            }
            
        }
        
        Target = TopTarget;

        return Target;
    }

in this i have sorted a array of colliders based on distance and i am trying to assign the target closest for my AI to move to.

i need to eventually add other conditions based on the Object tag that the collider will encounter but i need to know the target will be assigned first.

1 Answers1

0

In

if(item.CompareTag("HurbFood"))
{         
    return TopTarget;
}

you will always return TopTarget (== null). Instead you should return item here.

Then also you clear the array of any item does not match the desired tag ... so if the first entry e.g. is no match you basically throw away your array ... And how do you know there are 100 elements and not more or less ..?


I would rather Linq Where and Linq OrderBy like

using System.Linq;

...

private bool TryGetTarget(out GameObject target)
{
    target = default;

    var colliders = Physics.OverlapSphere(transform.position, checkRadius, checkLayers);

    // Filter by tag etc
    // You could add more filters here
    var filteredColliders = colliders.Where(col => col.CompareTag("HurbFood")).ToArray();

    switch(filteredColliders.Length)
    {
        case 0:
            return false;

        case 1:
            target = filtered colliders[0].gameObject;
            return true;

        default:
            // Now order by distance ascending
            var orderedColliders = filteredColliders.OrderBy(col => Vector3.Distance(col.transform.position, transform.position));
            target = orderedColliders.First().gameObject;
            return true;
    }
}

You would use this like e.g.

private void Update()
{
    if(TryGetTarget (out var target))
    {
        transform.position = Vector3.MoveTowards(transform.position, target.transform, moveSpeed * Time.deltaTime);
    }
    else
    {
        // Probably either stay here and do nothing 
        // but wait for food appearing close to you
        // or wander around in some random pattern in order
        // to try and find some food ;)
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • ok I’m sorry I was not clear with my example code!! – NinjaMirage Jul 03 '20 at 14:50
  • The array of colliders is sorted by distance. I just need to reference the gameobject that colliders in the array are components of to assign it as the target gameobject and then empty the array because when the AI destroys the target it will call this method again to find the next closest target. There will be less then 100 objects in its collider range. – NinjaMirage Jul 03 '20 at 14:54