- Home /
Aggro behavior for RTS game
Hello, I'm working on a RTS game using Unity - and I was wondering regarding how to implement the following:
In my game I plan to have up to around 60 friendly units and about 150 enemies. All enemies/friendly units that a unit from the opposite team comes in range, and has a clear line of sight with - automatically attacks the target, considering the following:
All units can have varying ranges of sight.
A unit can always target only 1 unit
Once a unit has left the range of another unit, it can not attack it
Now I have come up with 2 solutions, and I was wondering which is the best way (or if there is a 3rd, better way) to do it:
Put a sphere trigger collider on all units, and keep track of all units coming in and out of trigger events - every update I run through all the units inside my collider and fire ray casts to see that the unit has a clear LOS on the other unit.
For every unit every update I go through all enemy units and check if they are in range. If a unit is in range, I fire a raycast and check if it's visible.
Thanks!
BTW, forgot to mention that I put a delay on both methods so they don't really happen every update, but every 500ms.
Answer by Mortoc · Jul 05, 2012 at 10:29 PM
Two things I'd do to modify the approach there:
1) Use Physics.OverlapSphere (http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html) instead of sphere colliders to do your range checking.
2) Make sure the delay is offset somewhat. Making them only update every 500ms is great, but if they all trigger on the same moment you're still going to have a framerate drop. (Chances are you're already doing this, I just wanted to include it in case)
Thanks for the advice, I'll try this approach tomorrow.
Right now I've tried the 2nd approach, all those colliders really lagged down my FPS.
Well, I've tried using the OverlapSphere method - but right now the distance checking method seems to be the most efficient. Testing with 500 enemies and 100 friendlies, I got about 38FPS~ using the distance method and around 34FPS~ using OverlapSphere.
Your answer
Follow this Question
Related Questions
Raycasting crashing Unity? 3 Answers
How to control order of executiing scripts? 1 Answer
WayPoints mixed with Raycast 1 Answer
Jitter on Slerp - Raycast 0 Answers
AI bug issue 0 Answers