Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ignoring collisions in an RTS / Shooter

Discussion in 'Physics' started by Elum224, Oct 12, 2017.

  1. Elum224

    Elum224

    Joined:
    Nov 23, 2013
    Posts:
    36
    I'm wondering what approaches people take to dealing with allow / disallowing collisions between projectiles and units in a Top-down-shooter or real time strategy game with lots of teams or special weapons.

    My use case:
    I have a team A I want to allow them to shoot through each other to hit team C.
    Team A and C pass through each other.
    Units on the same team collide with each other.
    Some weapons can friendly fire. Some only heal allies.

    Layers:
    TeamA - Units of team A, friendly fire only bullets (healing weapons that don't hit enemies)
    TeamABulletsFoe - Bullets that hit enemies
    TeamC - Units of team C, friendly fire only bullets
    TeamCBulletsFoe - Bullets that hit enemies
    BulletsFriendFoe - Bullets that do friendly fire.

    This approach is a bit annoying to set up & I'd ideally like to increase the number of team arbitrarily. There is a 31 layer limit (and the layers are shared for more than just collisions) so I don't really like this approach.

    Another approach could be to have a single layer for bullets instead of the 3 (n+1 where n is team number) above set them all to triggers and write my own "layer" system to manage when hits occur. It would give me more flexibility and use fewer layers, but I feel like it would be much less efficient receiving lots of events when bullets pass through allies.


    What approach do you take for manager multiple collision groups?
    Does anyone make extensive use of Physics.IgnoreCollision?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Use of layers should be very specific, like you've already found it will not scale very far and has limited use.

    You'll probably want to assign a team ID to units so that you can filter them from queries such as bullet hits, collisions and other things like switches for impacts that might heal allies but simultaneously damage enemies in AoE.

    Something like this

    Code (csharp):
    1.  
    2. public interface IUseTeams
    3.     {
    4.         int TeamId { get; set; }
    5.     }
    6.  
    Code (csharp):
    1. public void OnTriggerEnter(Collider other)
    2. {
    3.     var otherDude = other.GetComponent<IUseTeams>();
    4.     if (otherDude != null && !Helpers.CheckForPassthrough(MyTeamId, otherDude.TeamId) DoDamage();
    5. }
    CheckForPassthrough would be your mapping matrix to see what teams id's can hit what team id's.

    Physics isn't as straightforward with passthroughs. You can ignorecollision but it seems to process after the collision takes effect (last I checked) so it's somewhat useless in a lot of cases. If you're not using RB's then you don't have to worry about that. The list of ignores clears if you disable/enable the object too, so that has pros/cons.