Search Unity

Physics2D Ignore Collision between Collider2D

Discussion in '2D' started by Keshav, Nov 22, 2013.

  1. Keshav

    Keshav

    Joined:
    Jan 17, 2013
    Posts:
    11
    I am migrating a project from 3D space to 2D. And have realised that ignoring collider2d collisions is not available in unity4.3.

    I require this functionality in 2D as we have used it frequently in 3d.

    Would help if some one could help out, give an alternative.

    Will this functionality be made available for 2D physics in near future?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Last edited: Nov 22, 2013
  3. Alexrose12345

    Alexrose12345

    Joined:
    Jul 15, 2012
    Posts:
    6
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Yep, same here. It seems like a pretty obvious oversight. Maybe there are technical reasons... Layers are nice, but they can't replace Physics.IgnoreCollision for things like ignoring collisions between a shooter and his bullets, assuming you have other like units on the same team and you do want friendly fire to affect his allies but not himself.
     
  5. Stone-Legion

    Stone-Legion

    Joined:
    Aug 16, 2013
    Posts:
    112
    Just ran into the same problem, hopefully this is implemented ASAP (within coming days). Our project requires that our players weapons do not hit him and its not feasable to put everything else on a different layer.
     
  6. PotatoInsertion

    PotatoInsertion

    Joined:
    Mar 11, 2013
    Posts:
    23
    Has anyone come up with some sort of work around for this? Could be waiting on Unity for a while.

    Working on game now with two characters, and right now they can't move past each other.
     
  7. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    I don't think there is any possible workaround but to use many collision layers. Or use 3D colliders with z-axis movement locked instead.
     
    Last edited: Dec 4, 2013
  8. PotatoInsertion

    PotatoInsertion

    Joined:
    Mar 11, 2013
    Posts:
    23
    Well, that's annoying. Thanks for the hint anyway, I'll look into layering things. Hopefully Unity will come up with something for this soon, it seems like a pretty important thing to omit.
     
  9. PotatoInsertion

    PotatoInsertion

    Joined:
    Mar 11, 2013
    Posts:
    23
    Just to update on this, fixing my issue was pretty easy with collision layers; I have two players both on the player layer, if I turn off the collisions between them then they work fine. It may be more complicated for other projects.

    Just note that you go to Edit>Project Settings> Physics2D, not the Physics3D option. Obvious enough, but easy to miss.
     
    JoRouss likes this.
  10. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Right, but imagine you have an enemy with a gun that fires a 2D physics collider. This gun is within the hit box of the enemy so the bullet will naturally hit the shooters own hit box. Solution: Set up a layer for hitbox, layer for bullet, ignore collisions between the two. Enemy fires gun, bullet ignores his hitbox, great. What happens if you have 2 enemies and you want them to be able to shoot each other? Now all enemy bullets go through enemy hit boxes. If you have enemy teams where allies on the same team can't shoot each other, its easy enough to set up hit box layers and bullet layers for each team, but if you just want all enemies to be able to shoot all other enemies, you're pretty much out of luck. In some scenarios you might be able to get around this by making the bullet spawn outside the shooter's hit box, but that doesn't cover every scenario.

    The same issue comes up when making a 1 on 1 fighter. Each player ends up having to have his own layers for hit boxes and hurt boxes which have to be assigned at runtime because you never know which character will be P1 or P2. How about 4 players? In short, it can easily become a big mess depending on your use case.
     
    NinjaISV likes this.
  11. FreyaM

    FreyaM

    Joined:
    Jan 6, 2014
    Posts:
    2
    May not help the situations guavaman lists but sure helped me. thks I missed this.
     
  12. Khalanar_Dev

    Khalanar_Dev

    Joined:
    Mar 25, 2013
    Posts:
    34
  13. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    I don't see any new function in Physics2D so I'd wager nothing has been changed. https://docs.unity3d.com/Documentation/ScriptReference/Physics2D.html
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Both "Physics2D.IgnoreCollision()" and "Physics2D.GetIgnoreCollision()" has been added to an internal beta release. You'll see these and many more features and fixes when it gets released.
     
  15. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Excellent, great to hear that! Thank you guys for giving this quick attention!
     
  16. mu-kow

    mu-kow

    Joined:
    May 15, 2012
    Posts:
    106
    I'm assuming that this will work as it did in 3D and you will only prevent future collisions from occuring (ie. you try and use this in OnCollisionEnter2D and it will not ignore the current collision but only subsequent ones).

    Is getting (pre)collision callbacks and being able to ignore them (while still getting the valuable contact info) among the "many more features" ?
     
  17. hauzer

    hauzer

    Joined:
    May 30, 2013
    Posts:
    1
    Any news on this? All I found regarding this issue are this thread and this feedback idea. Nothing on the issue tracker. An update would be nice.
     
  18. locrian05

    locrian05

    Joined:
    Feb 22, 2014
    Posts:
    2
    #pragma strict
    var jumpKey : KeyCode;
    var downKey : KeyCode;
    var leftKey : KeyCode;
    var rightKey : KeyCode;


    var jumpPower : float = 10;
    var jumping : boolean;

    function Start ()
    {

    }

    function FixedUpdate ()
    {
    Debug.Log(gameObject.layer);
    //Jump
    if(Input.GetKeyDown(jumpKey))
    {
    rigidbody2D.velocity.y = jumpPower;
    }

    //Down
    if(Input.GetKeyDown(downKey))
    {
    //rigidbody2D.velocity.y = 3;
    //Physics2D.IgnoreLayerCollision(0,8,true);
    }
    else
    {
    //Physics2D.IgnoreLayerCollision(0,8,false);
    }

    if(Input.GetKeyDown(leftKey))
    {
    rigidbody2D.velocity.x -= 3;

    }
    if(Input.GetKeyDown(rightKey))
    {
    rigidbody2D.velocity.x = 3;

    }

    }

    function OnTriggerStay2D (hitInfo: Collider2D)
    {
    if(hitInfo.name == "Platform" Input.GetKeyDown(downKey))
    {
    Debug.Log("stay");
    Physics2D.IgnoreLayerCollision(0,8,true);
    }
    }

    function OnTriggerEnter2D (hitInfo: Collider2D)
    {
    if(hitInfo.name == "Platform")
    {
    Debug.Log("up");
    Physics2D.IgnoreLayerCollision(0,8,true);
    }
    }


    function OnTriggerExit2D (hitInfo: Collider2D)
    {
    if(hitInfo.name == "Platform")
    {
    Debug.Log("down");
    Physics2D.IgnoreLayerCollision(0,8,false);
    }
    }
     
  19. SphericalIce

    SphericalIce

    Joined:
    Feb 5, 2014
    Posts:
    2
    Is this still not implemented? It's a game breaker to not have and I really do need it
     
  20. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    There's nothing in the docs about it http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.html so no its not implemented. And yes, its a game breaker. My system, Sprite Factory really needs this to ignore internal collisions among all the colliders for 2D. At this point, 2D physics is unusable for a console style game like a fighter.
     
  21. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Unity hasn't had a real update in over a month, so it's not in yet. But it will be in the next update apparently, whenever that is....
     
  22. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Did you get this information from Unity? I hope you're right.
     
  23. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Read all the posts in the thread... A developer "MelvMay" replied in this thread and directly said so. You even quoted the post where it was said.
     
  24. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    I did read all the posts. He did not say it was going to be added to the next update, just that it was added to an internal beta but there is no word on when that will make it through to final.
     
  25. Khalanar_Dev

    Khalanar_Dev

    Joined:
    Mar 25, 2013
    Posts:
    34
    Not implemented yet, I'm waiting to set one way platforms in Super Magma Tsunami . We'll have to wait a little more, hopefully it'll be soon =)
     
  26. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    It has been implemented (a long time ago) but isn't yet in a public release. There's a whole cycle from alpha to beta to RC that goes on before it's made public so it's been a long time coming unfortunately.

    There are a huge set of changes for 2D physics in the pipe but unfortunately cannot provide dates for releases.
     
  27. SphericalIce

    SphericalIce

    Joined:
    Feb 5, 2014
    Posts:
    2
    okay, that's fair enough, could I get a rough estimate for when they tend to come out? like, within the next few months, weeks, years? a rough time frame would be nice
     
  28. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Thanks for bringing us up to date! I really appreciate it.
     
  29. Khalanar_Dev

    Khalanar_Dev

    Joined:
    Mar 25, 2013
    Posts:
    34
    Thank for the info MelvMay! Also as Sphericallce said, if it were possible could you tell us appx when will this launch in public versions? If its gonna take more than a month or so I'll think of using a temporary workaround. :D
     
  30. PcMaster24

    PcMaster24

    Joined:
    Mar 1, 2014
    Posts:
    3
    i think a good alternative would be to create a condition inside the Update() function ,
    and disable the object collider once he collide with "ignored object" and then renable it once the collision is done .
     
  31. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    1.) We have no OnPreCollision event exposed, so you'll loose your velocity.
    2.) If you have enemies or other things that move around on a surface... they'll end up falling through.

    Using a raycast above your head, or by your Y velocity, to set an IgnoreLayerCollision() is the easiest way at the moment, but it's still a bit hacky.
     
  32. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    I would make your character have a bottom motive collider for standing on floors, and a top trigger for detecting platforms above the head (or use raycasting). When you're in a jump and the top trigger/raycast hits something, check if its a 1 way floor, if it is disable the bottom motive collider for a set amount of time (assuming your jump height isn't variable), then re-enable it. Or instead of time if you need more accuracy, when you detect the collision with the 1-way platform, start another raycast from the center of the player down a certain distance below his motive collider, check for a minimum distance to contact (so it doesn't re-enable until the player is far enough above the platform), then re-enable the player's motive collider.

    If you don't like raycasting you could use 2 triggers, one on top and one on bottom for detection. The one on the bottom should stretch a bit below the characters feet so you can anticipate the floor collision before it happens and re-enable the motive collider. (Edit: Though the trigger route has the issue that you can't detect where the collision took place, so you can't really know that the player has cleared the platform... raycasting probably works better here.)
     
    Last edited: Mar 7, 2014
  33. CoffinNail

    CoffinNail

    Joined:
    Mar 27, 2014
    Posts:
    2
    So we're encountering a similar issue with our 2 player game. I can jump through 1 way platforms in our current implementation, using IgnoreLayerCollision, until a second player joins, and then both our y's need to be > 0 to ignore collision, one player will periodically fall through platforms, and all other kinds of messes. My current idea is to put each on a dynamically assigned separate layer, but as you said in a previous post, this creates issues once we start shooting at each other, if we extend to 2+ players, etc. I tried what you suggested, but did it OnCollision, instead of with a raycast, and when y > 0, the problem was once the character hit his tiny little head on the collider, they then had y < 0 and would not pass through the collision box. Despite that being a bad idea, I was setting the character's collider IsTrigger to true. Is setting the IsTrigger to ignore collisions a good approach, or is there a more elegant way to avoid the collision, once the ray(line)cast intersects above their head?

    P.S. Unity - please fast track this in beta.
     
  34. Khalanar_Dev

    Khalanar_Dev

    Joined:
    Mar 25, 2013
    Posts:
    34
    That greatly depends on your purpose. Just bear in mind that if you set their collider to isTrigger then no other collision events will be fired until the collider is set to collision again. For example, if there is an enemy that must hit you in that time, or if player2 has to interact with player 1 you will have a few frames in which you won't get that to happen. It is high dependant on what your game is about. Per se, it is not a bad idea to change the isTrigger in order to avoid collisions, just be careful and be sure of what your needs are in every case scenario.

    I finally implemented the IgnoreLayerCollision with a layer for each player in my game. Sure it's dirty but it works for now until I get to ignore collision between to specific objects =)
     
  35. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    y < 0 being y movement speed?

    If you say he hits his head on the box then you used a collider. My suggestion was to use a trigger above the head, not just a regular collider. A trigger won't stop the character from going through the platform, but it can still detect when his head hits something, get the collider the trigger collided with, then get a component on the 2-way collider (or check a tag, etc.) to determine if its a 2-way floor, then deactivate the motive collider. Of course, you need to have a rigidbody on the floor or the head trigger in order for it to register collisions.

    I generally don't switch to isTrigger for that purpose because it may still collide with other things in the world. If all your layers are set up so nothing else out there will collide with it in trigger mode, then its fine. Otherwise, you could disable the motive collider, or you could move it to a layer and then ignore it.

    It really depends on how your characters are set up. I generally use different colliders for different purposes. For example, a character may have a motive collider and one or more hit detection colliders. That way your hit detectors can stay on while your motive collider goes off in this case. The character could get hit even while he's jumping through the platform. Of course you'd want to make sure you plan for the case that he gets hit mid jump and can't reach the top of the platform and re-enable his collider at the right time or he could become solid inside the platform if it goes on too soon or fall through the rest of the level if it goes on too late.

    As Pablo said, your game's needs will determine what works best.
     
    Last edited: Mar 27, 2014
  36. CoffinNail

    CoffinNail

    Joined:
    Mar 27, 2014
    Posts:
    2
    Awesome, thanks a lot for the insight, I have it working now. So what I did, for anyone who's interested, is created a trigger at the player's head and feet and also repurposed the general 2D box collider I had to a motive collider. On trigger for the head, if the tag of the other collider is "Ground" then the motive collider is set to IsTrigger. If the trigger at the feet hits a collider with the "Ground" tag then it sets the players motive collider IsTrigger to false. This might be an issue in the future if we want players who collide to interact in some way physically, but as of now it's working just fine. I'm writing a section for this in our devlog (still WIP) for anyone who's interested.
     
  37. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Glad you got it working. Hopefully they'll get Physics2D.IgnoreCollision in eventually so you can cover all use cases. Anything at present is just a temporary workaround.
     
  38. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625