How would I detect the closest enemy to the end within a tower's range

I am trying to make a tower defense game and I can detect which enemy is closest to the end, but I still need to figure out how to detect this within a tower’s range. I have heard Region3 might be able to do something like this? Just asking for any useful resources, thanks

Cozmo

1 Like

Magnitude my friend, magnitude.

(position1 - position2).Magnitude 
-- returns a vector length you can compare to for distances
2 Likes

Then I could get the distance from the tower to the end and subtract the magnitude i got from "(position1 - position2).Magnitude "

That should work, thanks!

If you really want, you could try using WorldRoot | Roblox Creator Documentation as this will detect if the outer edges and vertices are within range, not just the center position.

However, I this is really only needed for custom use cases depending on the size of the enemy models the towers are attacking. If they are all relatively small on all axes then use @Evercreeper’s answer.

2 Likes

As you said, I think @Evercreeper’s solution will probably be better in this situation, but if it dosent work I will experiment with yours.

I’m not saying that @Evercreeper’s solution won’t work. In fact, it most certainly will. I was merely providing an alternate method if you had certain enemy npc’s in which their size along one axis was significantly extended from the center point.

1 Like