Checking gameobject in Collider range only on method call

Hi, the problem looks like this:

  1. We have some Collider, lets say a box collider with is detecting range of enemies, and its placed on our AI allay.
  2. Our Allay just defeated enemie that he was focused on, and now he wants to check who is with his range [collider]. I don’t want to use OnTriggerStay, because it is called every frame so It’s not optymalized. I just need to Check on 1 frame, what gameObjects are in collider range. Its like calling OnTriggerStay but only once.
    So guys do you know how I do that?
    Cheers

Do any of these solutions work? Your question isn’t perfectly clear.

  • Try Physics.OverlapBox() or Physics.OverlapSphere() to get all the objects in a certain range. Unity - Scripting API: Physics

  • On your trigger, create a list of objects inside the trigger. Add/Remove the objects OnTriggerEnter/OnTriggerExit. Now you can access all the objects in that trigger easily.

  • Get all the enemies in the scene and use Vector3.Distance() to determine if they’re in range. You can also use this to determine which enemy is the closest if you’re trying to decide which enemy to focus on.