- Home /
Find All objects in range (no physics)
Hi I'm building a system where an object effects all objects in its range (spherical), for objects that are rigidbodys I can use a trigger sphere collider and apply the effect on trigger stay etc, however I also have a number of gameobjects I need to also apply the effect to that don't use rigidbodys for performance reasons and I'm trying to figure out the fastest way to effect them.
The obvious solution would be to loop through all objects and see if they are in range by comparing the distance vectors square magnitude to the square of the range, but I'm hoping there's a faster solution like physics.overlapsphere but for everything?
If not the next step would be optimising the loop in which case I assume tags or layers are the fastest way to cull the list down to the objects that I require?
Answer by Phles · Feb 10, 2014 at 11:42 AM
If you really want to avoid using physics completely (no colliders) as your questions title implies you can tag all the objects you are interested in and use GameObject.FindGameObjectsWithTag(tag) and loop through the array of tagged objects checking distances.
Adding colliders and using the Physics.OverlapSphere approach is probably a better way to go though, be sure to use a layer on your objects and use Physics.OverlapSphere with a layermask or with a large radius and lots of objects it gets to be somewhat inefficient.
Answer by wibble82 · Feb 10, 2014 at 11:21 AM
Hi There
It sounds like what you need is to have the objects you're searching for have a 'collider' attached (not a rigid body). This means they exist in the physics engines view of the world, but don't move or bounce around. In addition, in unity you can specify what layers collide with other layers, so you could keep these in a special layer to avoid them hitting other objects. These would have a memory cost, but very little extra performance cost (as they don't move, and don't collide with any of your rigid bodies).
Once that's done, Physics.OverlapSphere is what you're after!
Check out this answer for a different problem with a similar solution:
http://answers.unity3d.com/questions/231987/find-all-objects-currently-colliding-with-trigger.html
-Chris
Answer by vuwij · Oct 30, 2016 at 07:52 AM
You can use Physics2D.CircleCastAll if you are using 2D, in 3D there is Physics.CapsuleCastAll or SphereCastAll. This sends a spherical raycast looking or any objects with Colliders. there is no way you can avoid colliders
public void Inspect()
{
RaycastHit2D[] castStar = Physics2D.CircleCastAll(transform.position, inspectRadius, Vector2.zero);
foreach (RaycastHit2D raycastHit in castStar)
{
if (InspectionIsInvalid(raycastHit)) continue;
IInspectable[] mObj = raycastHit.collider.GetComponentsInChildren<IInspectable>();
if (mObj.Length != 0)
{
Debug.Log(raycastHit.collider.name);
foreach (IInspectable obj in mObj)
obj.InspectionAction(raycastHit);
return;
}
}
}
Answer by TheReEvolutions · Jul 10, 2017 at 01:56 PM
For anyone wanting to use vector loop distance method. Hope it helps .
var colorR : Color;
function FindInRange () : GameObject {
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Target Objects Tag Here");
var radiusDistance = 50; // the range of distance
var position = transform.position;
// Iterate through them and find the objects within range
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < radiusDistance) {
go.GetComponent.<Renderer>().material.color = colorR; // effect on object
}
}
}
Your answer
