Search Unity

2D collisions only if same Z level?

Discussion in '2D' started by astromedia-only, Jul 25, 2015.

  1. astromedia-only

    astromedia-only

    Joined:
    May 13, 2014
    Posts:
    25
    Hi, im sorry if there is some answer to this. I would like to resolve 2d collisions only if objects have same z value, is there some easy way to do that?

    Context: For example I would like to make server/client app from space environment, each client will be in some/different ship and theese ships could be in different star systems. Now i would like from server to manage all systems simultanously, so i will give each to own group (empty object) and on different Z level. So all systems will be on same coords starting from center (0,0,x) and inner mechanics would be resolved for each system separatly (only objects with same Z position will interact).

    I hope you understand what i was trying to say, sorry for my english, its not my primary language and some things are still hard to explain :)

    PS: I must admit its first time im playing with unity net support so if there is some easier way how to handle multiple "maps" (there would not be technicaly in different maps, im planning to generate them randomly at game start, i just want them to use same coordinates and from system to system i will use some sort of automatic transition) at once im open to new solutions :)
     
  2. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    You can limit collisions between different Unity layers. These are not the Sorting Layers, but the User Layers, which can be accessed from a game object's inspector panel. They are located under the game object's name, to the right of "Tags." You can create your own layers, but only up to 24, meaning that 24 different collisions between layers can be handled. You would use
    Code (CSharp):
    1. Physics2D.IgnoreLayerCollision(layerA, layerB, true)
    to disable collisions between layers.
     
  3. astromedia-only

    astromedia-only

    Joined:
    May 13, 2014
    Posts:
    25
    Thx for answer ... i thought there will be something like that but its somehow unpractical, especialy if there will be more than 24 systems generated even if i will not need theese layers for something else too. Maybe there is some way how to simulate "z level" layers for collisions by that but im sure it would create lot of mess in my project. Maybe i will have to do that anyway but i will wait if somebody could provide easier solution :)
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    So you want them to collide if their z axis match but not if they are out by even 0.0000000001?
    If it's 2d & they normally interact I was taught that you put them on the same z coords & use layers & tags to help filter interactions & use the drawing layers to determine what appears in front or behind the other.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can use 3D physics instead of 2D. 2D physics have no concept of a Z dimension at all.

    --Eric
     
    Dieter_Dagger likes this.
  6. astromedia-only

    astromedia-only

    Joined:
    May 13, 2014
    Posts:
    25
    Oh, i see ... i hoped there would be some option how layer 2d scene using z axis. Too bad, it seems more intuitive to me than that pseudolayers unity offers :/ ... If there is no easy workaround it would be maybe better to forgot security and test collisions client side then.

    About 3d physics - its even capable of registering collisions with objects with zero thicknes?
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You'd be using 3D physics, so you wouldn't have objects with zero thickness. What you're displaying on the screen is separate from how physics are calculated. i.e., use a box (cube) collider with a sprite, no problem.

    --Eric
     
    Dieter_Dagger likes this.
  8. astromedia-only

    astromedia-only

    Joined:
    May 13, 2014
    Posts:
    25
    Ok, its not ideal but it seems like my best option ... thx, i will try that :)
     
    Last edited: Jul 26, 2015
  9. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    How would you use 3D colliders with 2D Tilemaps, any good workaround?