My player sprite get stuck to walls while moving along


I am trying to achieve basics 2D movement in a top-down game with Unity built-in Physics. I didn’t write any physics code.


My player sprite is moving along the wall (not even diagonal, just straight down) and get randomly blocked in the corner of some walls. By blocked, I mean that the Y movement is stopped. If I move him away from the wall, he might not be blocked again by the same wall and get blocked at some other walls, always at a corner.

The walls are all the same sprite and are of the same size.

I use 2D Box Colliders for the walls. The player sprite has a Box Collider 2D and a RigidBody 2D.

I move the player by adjusting the velocity of the rigidbody according to Input:

RigidBody2D rb = GetComponent<Rigidbody2D>();
float horizontal = Input.GetAxisRaw ("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector2 movement = new Vector2(horizontal, vertical).normalized *speed * Time.deltaTime;
this.rb.velocity = movement;

The player is set like this:


Do you have any idea why it behaves like this and how I could fix it?

Thanks for your help.

The way physics works is there’s a little buffer around the actual object, so the corner of your player is hitting the corner of a wall piece. It’s probably random due to update frequency of the physics, sometimes it’s able to pass over the corner.

If you’re able to use a Circle collider on the player, it should fix the problem by letting it slide over the corners.