Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Box Collider Collision Catching on Edges

Discussion in '2D' started by DeeDeeKaKa, Jan 15, 2014.

  1. DeeDeeKaKa

    DeeDeeKaKa

    Joined:
    Sep 14, 2013
    Posts:
    5
    Hello,

    I am having an issue with collision detection. I am using Unity 4.3.2 and trying out the new 2D game features. On my main character (Deity Link), he has a Rigidbody 2D, and a Boxcollider2D, as well as a script for control. I am using some rigid body physics and applying forces to his rigidbody to move him around.

    Code (csharp):
    1. //Apply horizontal movement
    2.         rigidbody2D.AddForce(Vector2.right * hMove);
    3.         //Apply vertical movement
    4.         rigidbody2D.AddForce(Vector2.up * vMove);
    5.  
    I don't understand why, but his box collider will snag on the edges of tiles that also have a boxcollider2D, and then he will not be able to move anymore in that direction until he gets unstuck (I make him jump over that area to unstick him). If you look at this picture, clearly the boxcollider2D on my character is not actually hitting the tile's collider, but floating above. I even have a physics material on my box collider with friction and bounciness set to 0. I have a feeling this could be a calculation error on my end or a small bug in Unity. But here is my code to check if he is grounded and to stop his motion.

    Code (csharp):
    1. void CheckCollision(Collision2D c)
    2.     {
    3.         foreach(ContactPoint2D contact in c.contacts)
    4.         {
    5.             //Check for floor hit
    6.             if(vMove <= 0  contact.point.y <= transform.position.y + boxCollider2D.size.y / 2  Vector2.Angle(Vector2.up, contact.normal) <= slopeLimit)
    7.             {
    8.                 isGrounded = true;
    9.                 vMove = Mathf.Max(0, vMove);
    10.             }
    11.         }
    12.     }
    Any help in this issue would be appreciated, as I cannot find the source of the issue.

    $collider.png
     
  2. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Hey, I've been having the same issue. You can check out my thread here:
    http://forum.unity3d.com/threads/220861-Rigidbody-getting-stuck-on-tiled-wall
    The suggested fixes didn't work for me, but maybe you will have better luck with them.
    For me the only way around has been to work with circle colliders for wall collision and a seperate collider on a different layer as an attack hitbox.
     
  3. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  4. DeeDeeKaKa

    DeeDeeKaKa

    Joined:
    Sep 14, 2013
    Posts:
    5
    Hey, thanks for the replies.

    I figured since I have been reading around in the forums. I am going to report it as a bug so that something gets done about soon. For now I will use the circle collider technique for the bottom of my character. Thanks for the help though!
     
  5. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    364
    Same here, Unity 5.1.2. This bug drives me crazy...
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,014
    It's not a bug, it's how Box2D works. You can read more about the issue here: http://www.iforce2d.net/b2dtut/ghost-vertices

    Whilst you want this to be a continuous surface, it isn't and Box2D treats it as such. The recommended solution is to create a continuous surface using the EdgeCollider2D. It's also more efficient but granted when you want to work where a tile is a game-object and has its own collider then it's maybe not what you want to do, even if that approach is super inefficient.

    Solving this in a nice way is what the 2D tile-map WIP aims to achieve.
     
  7. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    364
    Another solution I came to is catch stuck moment and add 0.01 unit to the transform's position. Strange thing is rigidbody.velocity lying in this case, telling me that velocity is like object is moving, when it is on the same place. So I need to use transfrom's velocity calculations... I think, telling this is not a bug is wrong, if this is how Box2D works, there is a bug in Box2D. Just thinking out loud... Btw, is 2D tile-map an upcoming feature or what?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,014
    It can be called whatever you like but unfortunately, it isn't something that can be 'just fixed' in Box2D either. It's a discontinuous surface; something which Box2D solved by adding edge-chains as used by Unitys EdgeCollider2D.

    You can read more about the tile-map here: http://forum.unity3d.com/threads/2d-alpha-release-3.347722/
     
  9. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    ... Or you could do the same as the default 2d prefab (included with Unity): use a circle collider below the box collider. That way, the added benefit is that you can walk/run up and down slopes (would be your next issue with this approach) :)

    Note: this doesnt solve the issue if the ground collider is not perfectly aligned with each other.
     
  10. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    364
    Wow, those new features looks amazing.
     
  11. JPBotelho

    JPBotelho

    Joined:
    May 22, 2016
    Posts:
    205
    I know this is an old post, but
    @MelvMay
    I'm working with 3D Box Colliders instead of 2D, so i cannot use edge colliders.
    Is there any fix for 3D?
    I've tried adding physics materials, changing colision detection to continuous, etc...
    nothing works
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,014
    I don't work on the 3D physics stuff so I can't say for sure but I've not heard of anything that can counter this issue.
     
  13. LuckyLukert

    LuckyLukert

    Joined:
    Feb 23, 2019
    Posts:
    3
    @JPBotelho
    I have the same problem (also in 3D).
    Did you fix it? Does anyone else have an idea on how to fix it?
     
  14. JPBotelho

    JPBotelho

    Joined:
    May 22, 2016
    Posts:
    205
    It's been a while, no idea.
     
  15. Manolich

    Manolich

    Joined:
    Mar 28, 2019
    Posts:
    1
    If anybody still needs it...

    Applying a Composite Collider 2d to the tilemap and setting the rigidbody2D that comes with it to "static' solved for me.

    Sorry for my grammar, english isn't my native language...