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

Collision Depending On Z Axis For 2D?

Discussion in '2D' started by ExcitedMormon, Oct 15, 2014.

  1. ExcitedMormon

    ExcitedMormon

    Joined:
    Aug 3, 2014
    Posts:
    7
    Hello. I have a question that I find interesting. Is it possible for 2D Colliders and Rigidbody2D to have 3D like collisions, depending on where the Player is on the Z axis? So for an example let's say my position was 1 on the Z axis, and the thing I want to collide with is the same. Then let's say on the Z axis of 3, there was another thing that I wanted to collide with. Would it be possible to only collide with the first thing when I'm on the same Z axis as it, and then if I switched my position to 3 Z axis, then I wouldn't collide with the first thing, instead I would collide with the second one? Hope someone can answer this.
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    You could just set the Collision Layer whenever the Z-value changes. Otherwise use appropriately constrained cylinders in 3D physics.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The 2D physics engine is purely 2D (it's the Box2D engine) and has no concept of the Z axis.

    --Eric
     
  4. ExcitedMormon

    ExcitedMormon

    Joined:
    Aug 3, 2014
    Posts:
    7
    So I'll have to resort to changing all of my Box Collider 2Ds to 3D, all my rigidbody2Ds to 3D, all my scripts to 3D, and so on? If so, is there a quick way to do it?
     
  5. ExcitedMormon

    ExcitedMormon

    Joined:
    Aug 3, 2014
    Posts:
    7
    I'm not sure what you mean but I thought of an idea that I think goes with what your saying. Like if I was on the 1 on the Z axis, and then changed my position to 3, the colliders on the 1 Z Axis would disable, and the ones on the 3 will enable. It could work as I was already trying it, the only thing I'm worried about is how much it will affect the performance.
     
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    You can set up Collision Layers which determine which colliders can collide with each other. See here and here for 2D-specific.
     
    ExcitedMormon likes this.
  7. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    have a look at sorting layers with regards to rendering 2D scenes. that way you can swap players sorting layer, and have your player have a pseudo 3D feel.

    works a bit like old school parallax backgrounds if thats the kind of feel your after. then like Pyrian says have a wee look at collision layers.
     
    ExcitedMormon likes this.