sticky and blocking colliders that stop my character from running around.

Hi, everybody !

In the many plaformers that i’ve tried to devellop, there is a problem with the collider walls my rigidbody player (he is not a character controller) walks in. When touching a collider , my player cannot jump, he stays blocked near this collider until you release the move arrows, or the jump key. I would be more specific, but my problem couldn’t be fixed with a code i think. Maybe there’s a Rigidbody variable I mistreated.

Thanks for reading !

I have had this problem numerous times also within project developed inside of Unity. The problem is not due to the physics material, it is because when you are holding the arrow button, the engine is physically trying to push your object through the wall and the physics engine is trying to correct it by pushing it back away from the wall at the same time. This ultimately leads to the problem that you’ve described and is something that really should be avoided using a collision detection system.

The easiest and most optimised way to do this would be to cast a ver short ray directly infront and behind of your character. If the ray infront collides with something, disable forward movement, if the ray behind collides with something, disable backwards movement.

You could even take this one step further and use the above method to trigger animations to simulate bumping into a collider.

I solved this “sticky” ridgidbody issue using the solution suggested by @donnysobonny.

The easiest and most optimised way to do this would be to cast a ver short ray directly infront and behind of your character.

Here’s some code that I put in FixedUpdate() to solve it. Keep in mind that my game is 2.5D using the Unity 2D view on the x and y axis.

rigidbody.velocity = new Vector2(
    this.MoveDirection * this.MaxSpeed, rigidbody.velocity.y);

RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.right, out hit, 0.1f)
    || Physics.Raycast(transform.position, Vector3.left, out hit, 0.1f)) {
	    Debug.Log("Hit " + hit.transform.gameObject.name);
	    if (hit.transform.tag == "Stoppable"){
	        rigidbody.velocity = new Vector2(0, rigidbody.velocity.y);
	    }
}

In this example, I’ve tagged the objects I’m colliding with using a custom “Stoppable” tag. This can easily be changed to hit.transform.name, layer or some other identifier. I’m also using my own “MaxSpeed” and “MoveDirection” variables to handle the initial ridgidbody velocity.