# Detect if a spatial is in the radius of another

Hello mates, I am trying to figure out how to detect if a spatial is in a certain radius to another spatial. An example would be a turret; If a spatial of a type (ie a bear) comes within a certain radius, then shoot it. I looked through creating a custom control for the turret, but I cannot figure out how to detect if a spatial is within its shooting radius. Thanks for your help!

EDIT: I just realized I wrote “radius” in the original post. Please substitute that for “range”!

compare both positions to each other (there is even a .distance() method)
take that value and compare it to whatever you need.

Is there any other way to detect if another spatial is near, without using the distance method on every spatial attached to the scene until the distance is within a range. I am looking into the use of a bounding sphere, and executing code if another spatial intersects with it, but I have no idea if that would work.

@IdreesInc said: Is there any other way to detect if another spatial is near, without using the distance method on every spatial attached to the scene until the distance is within a range. I am looking into the use of a bounding sphere, and executing code if another spatial intersects with it, but I have no idea if that would work.

Why? The given solution is faster and gives the same result.

@normen said: Why? The given solution is faster and gives the same result.
Really? I was under the impression that applying the distance method to all the spatials in the scene constantly would cause the game to lag a bit. Thanks for your help!
@IdreesInc said: Really? I was under the impression that applying the distance method to all the spatials in the scene constantly would cause the game to lag a bit. Thanks for your help!

Well, you can do it more intelligently than that, of course. No reason to check towers against towers, for example.

If the game is designed properly, then you must have some game objects representing the towers and the mobs. You only need to check those. The scene graph is for visualization really… though even then you could organize it into towers and mobs.

2 Likes