Character keep sliding 2d,Character keeps sliding,how to stop character sliding in unity 2D

My player keeps sliding when i don’t want him to. I want him to walk normally and not slide when i simply tap the move button. Here is my code:

public float speed = 10f;

public float sprintSpeed = 0.5f;



public Rigidbody2D rb2d;

// Use this for initialization
void Start () {

    rb2d = GetComponent<Rigidbody2D>();

}

private void FixedUpdate()
{
    if (Input.GetAxisRaw("Horizontal") < 0)
    {

        rb2d.velocity = new Vector2(-speed, rb2d.velocity.y);

        if (Input.GetKey(KeyCode.LeftShift))
        {
            rb2d.velocity = new Vector2(-sprintSpeed, rb2d.velocity.y);
        }
       
    }

    if (Input.GetAxisRaw("Horizontal") > 0)
    {
        rb2d.velocity = new Vector2(speed, rb2d.velocity.y);

        if (Input.GetKey(KeyCode.LeftShift))
        {
            rb2d.velocity = new Vector2(sprintSpeed, rb2d.velocity.y);
        }
    }
}

Hey there,

first up its enough to post your post once… please check your posts and edit them to look right before you post them.
The problem you have in your code is that you never tell the rigidbody to stop. You never reset the velocity to 0. Thus it always keeps it’s velocity.

Try the following:

 private void FixedUpdate()
 {
     if (Input.GetAxisRaw("Horizontal") < 0)
     {
         rb2d.velocity = new Vector2(-speed, rb2d.velocity.y);
         if (Input.GetKey(KeyCode.LeftShift))
         {
             rb2d.velocity = new Vector2(-sprintSpeed, rb2d.velocity.y);
         }
        
     }
   
     else if (Input.GetAxisRaw("Horizontal") > 0)
     {
         rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
         if (Input.GetKey(KeyCode.LeftShift))
         {
             rb2d.velocity = new Vector2(sprintSpeed, rb2d.velocity.y);
         }
     }
     else {rb2d.velocity = new Vector2(0,rb2d.velocity.y);}
 }

Hope this works/helps.

Edit: If this does NOT work try changing the GetRawAxis to some other way like GetKey(KeyCode.A).