Box Collider Collision Catching on Edges

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.

//Apply horizontal movement
		rigidbody2D.AddForce(Vector2.right * hMove);
		//Apply vertical movement
		rigidbody2D.AddForce(Vector2.up * vMove);

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.

void CheckCollision(Collision2D c)
	{
		foreach(ContactPoint2D contact in c.contacts)
		{
			//Check for floor hit
			if(vMove <= 0  contact.point.y <= transform.position.y + boxCollider2D.size.y / 2  Vector2.Angle(Vector2.up, contact.normal) <= slopeLimit)
			{
				isGrounded = true;
				vMove = Mathf.Max(0, vMove);
			}
		}
	}

Any help in this issue would be appreciated, as I cannot find the source of the issue.

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.

This is a known issue…
http://forum.unity3d.com/threads/222310-2D-collision-glitch-crossing-multiple-objects-*Project-Included*

Perhaps somebody should submit a bug report to the Unity devs.
In the meantime, can’t you just use a Circle Collider with Rotation Fixed?

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!

Same here, Unity 5.1.2. This bug drives me crazy…

It’s not a bug, it’s how Box2D works. You can read more about the issue here: Ghost vertices - Box2D tutorials - iforce2d

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.

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?

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/

1 Like

… 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) :slight_smile:

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

Wow, those new features looks amazing.

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

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.

@
I have the same problem (also in 3D).
Did you fix it? Does anyone else have an idea on how to fix it?

It’s been a while, no idea.

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…

No, it doesn’t. Tried and still got stuck. There’s apparently no fix to this problem.

CompositeCollider2D in outline mode produces continuous edges so they do not suffer from this issue.

I tried it and it didn’t solve my problem. I had to add one capsule collider to make my player not suffer with that problem.

Well your problem then wasn’t what’s been described above beause that problem is related to non-continuous edges only (multiple colliders are not a continuous edge), something the CompositeCollider2D in outline mode produces.

Now I can jump infinitely