I want an object to run code that detects objects around them, in a specific radius.
BUT I cannot use a trigger... or any game objects or components.
What could I do just using code?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI want an object to run code that detects objects around them, in a specific radius.
BUT I cannot use a trigger... or any game objects or components.
What could I do just using code?
The classes Physics
and Physics2D
contain several useful static methods to detect Collider
s and Collider2D
s within geometric shapes. For example, if you want to get all objects overlapping a sphere with radius 10 around the current gameObject you would do this:
var colliders = Physics.OverlapSphere(transform.position, 10f)
foreach(var collider in colliders) {
Debug.Log($"{collider.gameObject.name} is nearby");
}
Most of these methods also have "NonAlloc" variants which overwrite an existing array instead of creating a new one. These methods are faster and generate no garbage (especially if you call them a lot), but are a bit harder to use and require that you know an upper limit of how many objects you are going to detect at most.
dist() < radius
. That's checking within the given radius to me. \$\endgroup\$