Search Unity

Is it expensive to create empty GameObjects will a collider on during runtime?

Discussion in 'Scripting' started by kevdotbadger, Jan 2, 2012.

  1. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Hay, I have a scenario where when a bullet is fired i create an empty gameObject, assign a collider then destroy it. The idea behind it is that if an enemy is within the SphereCollider's radius they'll hear the gunshot and head towards it.

    Initially I though I'll just add 3 "noise colliders" to my main character. So that depending on the weapon will depend which collider is used (a silenced gun will have a small radius, whereas a shotgun will have a medium radius, explosion large radius). Then I though it might be better to generate these on the fly. That way I can take more things into account (a shotgun's "noise colliders" might be small if we were inside a building).

    I've coded it so that if a "noise collider" exists, destroy it, then create a new one with a given radius. This ensures that only 1 "noise collider" is present at a time.

    Is this expensive to do? Would it be better to just have the 3 preset colliders and use them?
     
  2. YosemiteSam

    YosemiteSam

    Joined:
    May 5, 2011
    Posts:
    60
    Why not just use something like:

    Code (csharp):
    1. if (Vector3.Distance(gunPosition, enemyPosition) < threshold)
    2. ...
     
  3. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Seems a better idea, however there are multiple enemies, all moving independently, so I'll need to check everyone's position?
     
  4. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Can anyone help me?
     
  5. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Hey!It all depends on how many enemies you have there...If there are less than a hundred,then you should have no problems.
    You could try http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html as well...
    You gotta think how you will store the nearby enemies,so you don't have to loop trough all the entities in the gamescene if you will use the distance checking.

    You might wanna do some testing if you have tons of bad fellows,and see which method is faster.

    Good luck!
     
    Last edited: Jan 2, 2012
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Try physics.overlapsphere set to the size of your sound collider size... set it for the layer your enemies are on. This will return the colliders for all enemies in range... then send them the message they heard something.
     
  7. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    I would go with novashot's suggestion.
    If you can't make it with physics.overlapsphere, then I would try a sphereCast. But SphereCast may not work if you create the sphere already colliding with the enemies.