Search Unity

Detecting enemies : best practices question

Discussion in 'Scripting' started by Horn, Mar 26, 2009.

  1. Horn

    Horn

    Joined:
    Mar 26, 2009
    Posts:
    2
    Hi guys. I'm cutting my teeth on Unity and I have a question about whether or not I'm doing things the proper way.

    I've written an object I call a Seeker. When it spawns, it looks for the closest object tagged 'Entity' and then goes out and destroys it. My question is, how would I best implement teams? I was thinking of changing the Entity tag to EntityTeam1, EntityTeam2 but then I would have to call FindGameObjectsWithTag for each tag which likely be pretty slow and be a pain to maintain. I also thought of adding a 'Team' property to the objects so when the script is iterating over the results of FindGameObjectsWithTag it can compare Team to see if the seeker should care or not. The advantage of this method (in my mind at least) is that it will make adding new teams fairly straightforward.

    Is there other way I could do this? Is it possible to have sub-tags? For example, an Entity of another team might be something the Seeker should shoot at, run away from, or try and catch. Would adding properties be the best way of handling these cases?
     
  2. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    I'm keeping track of enemies via scripting, and using tags to identify all possible targets, which sounds like what you're doing now. I think you're right that that is the easiest way to scale the number of potential enemies or allies without having to have tons of tags. I haven't had any problems with this approach so far!