Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question 2D raycast to find and track closest target

Discussion in 'Scripting' started by Smoggs, Apr 13, 2024.

  1. Smoggs

    Smoggs

    Joined:
    Apr 5, 2020
    Posts:
    1
    So I am trying to write an ai behavior for my game where the npc characters will rotate to look at the closest target (a piece of food) to them. I have written code I thought would work, but when applied they just rapidly switch between all targets in the raycast, instead of focusing on the closest one.

    Code (CSharp):
    1.     Vector3 GetTargetLocation()
    2.     {
    3.  
    4.      
    5.  
    6.         RaycastHit2D[] hit = Physics2D.CircleCastAll(transform.position, SenseDepth, Vector2.up, SenseDepth, mask) ;
    7.         for (int i = 0;i < hit.Length; i++)
    8.         {
    9.            
    10.             float currentTarget;
    11.             currentTarget = 10000;
    12.  
    13.             if (hit[i].distance< currentTarget)
    14.             {
    15.                 currentTarget = hit[i].distance;
    16.                 senseTarget = true;
    17.                 Vector3 target = hit[i].point;
    18.                 Debug.DrawLine(transform.position, target);
    19.  
    20.                 return target;
    21.             }
    22.            
    23.          
    24.  
    25.            
    26.         }
    27.  
    28.         senseTarget = false;
    29.         return Vector3.zero;
    30.      
    31.     }
    This is the code I have to get the targets location to then rotate towards. I might've made some obvious mistake, slightly new at this.
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    Not tested:
    Code (CSharp):
    1.     Transform GetTarget()
    2.     {
    3.         float closestDistance=100000;
    4.         Transform target=null;
    5.         // OverlapCircleAll ahead of the AI might be better for this:
    6.         RaycastHit2D[] hit = Physics2D.CircleCastAll(transform.position, SenseDepth, Vector2.up, SenseDepth, mask);
    7.         for (int i = 0;i < hit.Length; i++)
    8.         {
    9.             if (hit[i].distance < closestDistance)
    10.             {
    11.                 // If you do switch to OverlapCircleAll then you can add a raycast here to make sure the food is actually visible and isn't hidden behind a wall.
    12.                 closestDistance = hit[i].distance;
    13.                 target=hit[i].transform; // better to return a transform so then your AI can track the target if it's moving. And if the returned transform==null then you know there's no target.
    14.             }
    15.         }
    16.         return target;
    17.     }