Character sliding down when landing on slopes

Hi everyone!

I’m having an annoying problem with my character controller. The character can perfectly stand, walk and jump on slopes, the problem is that when the player lands on a slope after jumping, he slides a tiny bit down, just as following:

I’m using the finite state machine pattern, so my character goes from a fall state to an idle state when landing. Tried setting a different physics material 2D when in idle, setting the gravity to 0, but the tiny slide still happens!

Any ideas on what could it be?

It’s just inertia. You could try setting the Y-velocity to 0 when you switch states to idle.

Check for the player both when standing on the ground and not moving. Then turn off gravity

Tried this on the exit method of the fall state:

      public override void OnExit()
    {
        player.playerRigidbody.velocity = Vector2.zero;
        base.OnExit();
    }

The idle state entering method is as following:

     public override void OnEnter(PlayerStateMachine _playerStateMachine)
    {
        base.OnEnter(_playerStateMachine);
        player.playerRigidbody.gravityScale = 0f;
        player.SetPlayerFriction(true);
    }

The sliding still happens. I believe it’s something related to friction, since when I set it to 1 during the fall state, the sliding is drastically reduced (to acceptable levels). The problem is that I can’t set friction to1 during fall, it would cause the player to get stuck into walls. the Tilemap I’m using as ground also has a physics material with friction set to 1.

any guess?

I still wonder why you’re using physics when it seems at every step you don’t want physics.

It honestly sounds like you should be using Unity’s CharacterController component.

Isn’t Unity’s CharacterController only for 3D? I’m working on a 2D game.

Also, creating a custom physics and collision system would be overcomplicated and unnecessary, since everything is responsive and working fine with rigidbody physics, except for this tiny problem…

Touche.

Double touche. Been working with custom gravity myself and have been sticking with rigidbodies for movement for that very reason (though have been refraining from manually driving the rigidbody’s velocity value as much as possible).

Almost feels like you have situations where you kind of ‘dont’ want physics, and situations where you do. Might be worth introducing a means to switch states with your controller.

Reviving this thread because I found the cause of the problem, but still don’t know how to solve it.

I’m using an Physics2D.OverlapBox casted below the character to find the ground while falling, when the box finds the ground, it turns the fricction on.The problem is that due to the speed of the fall, in one frame the character is still in the air without friction, and in the next frame the friction is turned on, but he has already slid down the slope.

This can be resolved by increasing the size of the OverlapBox Y, but this would cause the character to be grounded earlier than desired. So the only thing that came in mind is to somehow manipulate the position of the character directly in some way, but no idea how.

One possible way to handle this is to set a “slopeDrag” to your rigidbody. If your payer is on a slope, apply a high enough slope drag to reduce sliding downhill. If the player is not on a slope, then you could set a groundDrag to prevent such a high drag in every instance other than on slopes.