Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using Unity3d to develop a RTS/TD hybrid prototype game. What is the best approach for "awareness" between units and their enemies? Is it sane to have every unit check the distance to every enemy and engage if within range?

The approach I'm going for right now is to have a trigger sphere on every unit. If an enemy enters the trigger, the unit becomes aware of the enemy and starts distance checking. Does this save some unnecessary checks?

What's the best practice here?

share|improve this question
    
I should add that the unit/enemy count is going to be in the scale of Supreme Commander –  Phil Dec 28 '10 at 20:41
add comment

2 Answers

up vote 10 down vote accepted

Look into bounding volume hierarchies (BVH). They're most commonly used in collision detection to reduce the number of checks needed when calculating collisions or in rendering to perform frustum culling on objects. Since you're already using spheres, I'd suggest a sphere-tree though other volumes such as AABBs may be more efficient. I'm not sure what sort of support Unity has for such things as I've never used it, but there is probably something for this already within the collision detection or rendering parts of the engine.

Basically, you would want to group enemies that are near each other together into multiple parent spheres. When a unit is moved, you would check it's trigger sphere against the parent sphere instead of checking each enemy. If the trigger sphere intersects with the parent sphere, you would check each enemy within. If it doesn't, you can discard all of the enemies that are within it. You would want to set up multiple levels of spheres based on a maximum sphere size or enemy count for each sphere and perform the check based off of a top-level sphere. Then it's just a matter of walking down the tree in order to check each enemy without needing to perform the distance check for each one.

Steps required each frame:

  1. Move enemies
  2. Rebuild/update the BVH for new enemy positions
  3. Move units and check against sphere-tree.

This can reduce the checks needed when there are lots of enemies but the overhead of updating and storing the tree may not be worth it when there aren't that many. I'm not familiar with Supreme Commander to know what you're after, so I'm just assuming 'hundreds'. You'll need to profile in different situations to find out whether the overhead will be worth it to you.

share|improve this answer
3  
+1 this approach will be simplest although it will have some errors. OP may want to implement this and then implement something like an octree. Also, in most RTSs, the units react with some delay. In fact, most AI in games is calculated 10-30 frames per second where the game may be running at 30-60 frames per second (the numbers are, of course, approximations). –  Samaursa Dec 29 '10 at 5:33
    
Thank you, this was exactly what I needed to take me one step further. I'll look up BVH and how to implement it in Unity. Very pedagogical and informative post! +1 –  Phil Dec 29 '10 at 14:42
    
Oh and check out Supreme Commander if you're in to RTS. It's a very nice game indeed, as are all Chris Taylor games really. –  Phil Dec 29 '10 at 14:43
add comment

There is no need to implement BVH's, since Unity's collision engine essentially does that for you already.

Simply attach a large bounding-sphere trigger to each unit (representing its range) and handle the OnCollisionEnter() and OnCollisionExit() callbacks to keep track of which enemies are within range of each unit.

Note that the case you're interested in is when another unit collides with the sphere, not when another unit's sphere collides with the sphere.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.