How could I accurately make a circle-range system?

Hey, so yeah I’ve just been thinking about a possible way to make circle ranges for say, a Tower Defense type of game.

Basically, say we have this tower, and when you click on it, a circle appears around the tower with the radius of the tower being the tower’s attack distance.

The tricky part is figuring out how to detect if enemies are accurately inside of the circle.

I figured using magnitude would be the best bet, although as I experimented with different sized circles it yielded much more inaccurate results.

Primarily, this is a question for 2D space.

Thanks!

3 Likes

There are 2 approaches that come to mind.

The first being the BasePart:GetTouchingParts() method. Do note this may require you to constantly loop, and may have a slight impact on performance.

The second method I can think of is using a Touched event to figure out when an object enters the radius.

You can try to ignore the Y attribute and turn tower and target’s position into Vector 2. And then calculate their distance.

.Magnitude is basically just a 3d distance. But for a RTS, it’s ideal to use 2d distance. In my RTS, I find distance on the X and Z grid which is what you’re trying to do. Instead of using .magnitude try this. e is position 1 (Your towers position.) h is position 2 (enemies position)

function Distance(e, h)
    return math.sqrt(((e.X - h.X)*(e.X - h.X))+((e.Z - h.Z)*(e.Z - h.Z))) --Does not use the Y, just x-y     distance
end

This will return the distance of the X and Z grid.

10 Likes