- Home /
My RigidBody2D+BoxCollider2D is getting stuck on corners
Hello!
I'm loving using Unity, just getting started, but I've run into a really strange problem. I am using a RigidBody2D + Box Collider for a 2D platform movement. I'm using a Box Collider so the player doesn't 'slip' off of the corners of platforms.
So I do what you'd expect, set horizontal velocity based on input, and add vertical force when the player hits the jump button.
To prevent jumping, I use a horizontal linecast along the bottom of the collider to detect the ground and set an isGrounded boolean, like so:
// Get the extents (edges) of the player
extents = GetComponent<BoxCollider2D> ().size * 0.5f;
// Use a linecast to check if player is on the ground
if (Physics2D.Linecast (new Vector2 (transform.position.x - extents.x, transform.position.y - extents.y - 0.02f),new Vector2 (transform.position.x + extents.x, transform.position.y - extents.y - 0.02f), 1 << LayerMask.NameToLayer ("Obstacle")))
isGrounded = true;
else
isGrounded = false;
I don't allow the player to jump if isGrounded is false, it's near perfect. The problem is, if the player walks right to the very, very corner of a platform, the player can't jump. I've made isGrounded public, and I see the checkbox uncheck in the inspector, so the linecast, although it goes from one edge to the other, is obviously no longer colliding, and yet the physics engine seems to think it still is.
I tried to deal with it by altering this part:
isGrounded = true;
else if (rigidbody2D.velocity.y != 0)
isGrounded = false;
My reasoning there is that it wouldn't set isGrounded to false again until the physics engine has decided it's time to fall off, or the player jumps. The problem here is that if you stand on that very corner, and then perform a single jump, then the rigidbody will 'land' but the linecast won't collide, so isGrounded will remain false and then the player can't jump a second time if they don't move.
The other thing I thought to try was to enlarge the raycast slightly so it goes slightly beyond the edges, but then jumping whilst touching a wall will still count as grounded, and then I thought to do that and check for the y velocity to be 0, but I believe that still leaves a possibility (albeit a small one) of the player performing a double jump when touching a wall.
I've attached a screenshot of what's happening in the scene view if I zoom in on the corner, with a red Debug.Drawline to show where the linecast is:
Does anybody have any idea what's going on here?
P.S. The GetComponent I mentioned above is called in the Start() function to save it happening every frame, I just included it above to make it clear what extents is.
Your answer
Follow this Question
Related Questions
Rigidbody2D is kind of flying instead of falling when the child box is colliding 1 Answer
Problem with 2 Rigidbodies having respective colliders (2d) 1 Answer
Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :( 1 Answer
2D Pong Ball Not Reflecting Off Walls "Exactly" 1 Answer
Ball bounces at different heights when it shouldn't 0 Answers