Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Can 3D Physics interact with 2D Physics?

Discussion in '2D' started by Ilingis, Nov 14, 2013.

  1. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    Hello,
    let me first tell you that Unity is such a great software! I installed this new version Unity 4.3 and I played with it a bit and I'm not sure if it's possible so I came here to ask you if it's possible to have sprites with 2D physics that can interact with my 3D objects that have Rigidbodies.

    Can 2D physics and 3D physics interact with each other? Thanks!
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    345
    Yes, they can absolutely interact with each other. Just be sure to use OnCollisionEnter for 3D collisions and OnCollisionEnter2D for 2D collisions.
     
  3. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Mine didnt work.

    I had 3 sprites with 2D box collider and 2D rigidbody

    I had 1 3D cube with a 3D box collider and 3D rigidbody tagged "paddock"

    This code attached to the sprites did not work:
    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider col)
    3. {
    4. if(col.gameObject.tag == "paddock")
    5.             {
    6.                 // some code was here
    7.             }
    8. }
    9.  
    But when I changed the sprites to have 3D box colliders and 3D rigidbodies it worked as it should.

    But it looks like unitylover has provided the solution but I dont know how I would use it.
     
    Last edited: Nov 14, 2013
  4. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    345
    Did you use OnCollisionEnter or OnCollisionEnter2D?

    *EDIT*

    I had it working on my end I thought, but now it's not working. I will play around with it for a bit and report back what I find.
     
  5. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    As I far as I know 2D and 3D are completely sepparate systems (Box2D and PhysX) and they can't interact.
     
  6. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I would have tought that was the case.

    Good plan
     
    Last edited: Nov 14, 2013
  7. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    345
    He's right. But there is a simple way to get the two to play nicely, at least a little bit.

    Try making an empty game object with a Box Collider 2D attached to it and a Rigidbody 2D. Child your 3D object inside this empty game object and you'll be able to generate collisions.
     
  8. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Something I will also try is to remove the 3D collider and 3D rigidbody from the 3D object and replace them with 2D instead of parenting to an empty game object and see if that works.
     
  9. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    So those two physics work separate and there are ways to get them work together with a trick that you give 3d objects a 2D collider, I see. Hopefully someone from Unity team could comment on this? I'm making a 2.5D platform game and I'd love to have my player (which has a box collider) walk on a polygon collider floor.
     
  10. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    looking at this video
    http://www.youtube.com/watch?v=_qS7DD5Tz_A

    at around the 47 minute mark he tries putting a 2D collider and 2D rigidbody on a 3D cube but it doesnt work BUT he forgot to put a collider on his sprite (realises this at the 55 minute mark). It worked.

    So the solution seems to be replace the 3D collider and 3D rigidbody on 3D objects with their 2D counterparts or put 3D collider and 3D rigidbody on the sprites.

    The reason obviously is as Lukas H points out that 2D and 3D physics are completly different physics engines so will not interact with each other.

    And the answer to:
    Can 2D physics and 3D physics interact with each other?

    is no.
     
    Last edited: Nov 14, 2013
  11. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    Thanks for all the replies, guys! I appreciate it.