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.
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!
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.
… 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.
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
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.