Find all GameObjects within a certain radius?

Want to start making a Boid flocking system but having a hard time finding a way to check for other objects (in my case enemies of the same class of the one that is checking) in a certain radius around another object. I found the Physics.OverlapSphere method but that returns colliders, not gameobjects and their transforms. Is that what i want to use or is there another way?

Physics.OverlapSphere is the way to go. You can then access Collider.gameObject or Collider.transfrom.

The other methods are

  • Check every single object for distance
  • Implement your own spatial partitioning structure
2 Likes

Thanks for clearing that up!

If you need to know how many GameObjects are around at any time, you should maybe use a trigger collider following the object that need to know how many GameObjects are around.

Add a new script to it and in a OnTriggerEnter & OnTriggerExit, handle a list of the GameObjects arounds it.

This will have a smoother impact on your CPU instead of a one shot Physics.OverlapSphere.

Don’t forget to clear the list when you reset.

3 Likes

This works too.

I reccomend bench marking both if performance is critical. Both will behave different in different set ups.

2 Likes

Thanks. This will be on a lot of object at once so anything to help with performance is welcome.

Will the list of gameobjects keep track of their positions? The objects i want to keep track of will be moving.

Spatial Partitioning (i.e. OctTrees) might be worth some consideration if you are comparing things like hundreds of boids versus a number of attractors and repellants. making a repellant only test distance against boids in a sector (vs all boids in the scene) that encapsulates the repellants range should significantly help with any performance issues.

2 Likes

Of course, that’s the best way to do that so you end up almost not comparing anything, but i can be much less accurate depending on how much you are partitioning your world isn’t it ?
Unless it’s used as a first filter then you still compare distances under a certain amount of partition around your object.

You can create a list of anything including GameObjects or Transforms:

using System.Collections;
using System.Collections.Generic; // need this to use list
using UnityEngine;

public class Example : MonoBehaviour
{
    private List<GameObject> list = new List<GameObject>();

    private void OnDisable()
    {
        list.Clear();
    }

    void OnTriggerEnter(Collider other)
    {
        // Filter by using specific layers for this object and "others" instead of using tags
        list.Add(other.gameObject);
    }

    void OnTriggerExit(Collider other)
    {
        // Filter by using specific layers for this object and "others" instead of using tags
        list.Remove(other.gameObject);
    }
}

I’ve never heard of OctTrees but i’ll check it out, sounds interesting. Here’s a video that demonstrates what kind of behavior i’d like, and it has a link to the creators websites where you can download a demo of it, if you’re interested.

https://www.youtube.com/watch?v=g0LwS4ysGbE

Well you should use compute shader for that… It will be way more effective to process all of that GPU side.

I know it’s been three years since last comment, but I would like to know, is there any tutorial on how to write such a shader?