Search Unity

Question Character sliding down when landing on slopes

Discussion in 'Scripting' started by Phoenix248, Oct 27, 2022.

  1. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    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:

    Example4.PNG

    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?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    It's just inertia. You could try setting the Y-velocity to 0 when you switch states to idle.
     
  3. Unitycod4

    Unitycod4

    Joined:
    Apr 15, 2021
    Posts:
    5
    Check for the player both when standing on the ground and not moving. Then turn off gravity
     
  4. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    Tried this on the exit method of the fall state:
    Code (CSharp):
    1.       public override void OnExit()
    2.     {
    3.         player.playerRigidbody.velocity = Vector2.zero;
    4.         base.OnExit();
    5.     }
    The idle state entering method is as following:

    Code (CSharp):
    1.      public override void OnEnter(PlayerStateMachine _playerStateMachine)
    2.     {
    3.         base.OnEnter(_playerStateMachine);
    4.         player.playerRigidbody.gravityScale = 0f;
    5.         player.SetPlayerFriction(true);
    6.     }
    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?
     
    Last edited: Oct 27, 2022
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    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.
     
  6. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    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...
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    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.
     
  8. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    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.
     
  9. TazProductions

    TazProductions

    Joined:
    Feb 5, 2020
    Posts:
    2
    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.