- Home /
The question is answered, right answer was accepted
A way to check for gameobjects within a radius like Physics.OverlapSphere when there's no collider?
I'm trying to find a way to check if certain gameobjects (with tag) are within a radius but they don't have a collider so using overlapsphere isn't the way. Also I know that I could check the distances between all the gameobjects and determine if they are within the radius, but in this case there will be such a huge number of those gameobjects that I'm worried it will lag my game. Any ideas?
Answer by Llama_w_2Ls · Aug 18, 2020 at 07:27 PM
You could just add a collider and set it to IsTrigger. This way, having a box collider or not wouldnt affect the gameobject in any way from a physics perspective, but will allow you to use the OnTriggerEnter(Collision collider)
function. Or you could do a raycast as well. You dont need a collider, as you can check the hit.transform instead of hit.collider. Also Physics.Checksphere can check for gameobjects with a certain layer or layermask. For example:
public Transform GroundCheck; //Empty gameobject that is the center of the sphere
public float GroundDistance = 0.4f; //radius
public LayerMask groundmask; //layer to check for
void Update()
{
bool IsGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, groundmask);
}
Thanks! Didn't think about using them as triggers. (If you want you can post your comment as answer so I can check it as correct)
Follow this Question
Related Questions
How to Destroy gameObjects and deal damage within a radius except player ? 2 Answers
How do I reference a GameObject after finding its collider with Physics.OverlapSphere()? 1 Answer
Generating lines between procedurally instantiated gameobjects, within a certain radius 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers