How do big RTS games like strategma and The Conquerors 3 or TD games handle unit ranges?

I feel like raycasts would probably be too expensive. I don’t know if subtracting the positions and then using .Magnitude would be better. Is there any other ways/more optimal ways I can implement figuring out if an enemy unit is in range?

3 Likes

Subtracting the positions and using .Magnitude is the correct way to check if something is in-range (i.e. within an x stud radius). If you also have cover (e.g. walls or rocks or something that would block an attacker from hitting a target), then you would need to do a raycast to see if there is a clear path. You could check the distance first (via magnitude) and then do the raycast only if the target is in range. But honestly, I wouldn’t worry too much, raycasts are much less expensive than you’d think, especially if you are using RaycastParams.FilterDescendantsInstances to limit which instances need to be considered.

1 Like

Ah alright, thank you so much lol. Seems like I had the right idea years ago when I just started making stealth NPCs.

1 Like

I’m the developer of TC3. If you have a bunch of units in several teams, you’ll probably want to avoid using a mass scan of .Magnitude and go for something like a Quadtree. Checking hundreds of thousands of magnitudes a second is not very optimal, as I found out in 2016. You might be able to get away with it today though.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.