Object loses all vertical velocity when moving againsta a wall.

Hello all, apologies for what feels like a newbie question, but my player object otherwise works exactly as i intend. However when i was playtesting i discovered something, when moving, the player object loses all vertical velocity when holding against a wall. Obviously this is not intended and not a behavior i want, however i don’t have any ideas how i should fix it. Here is the movement code:

    void PlayerMove()
    {

        if (Input.GetAxisRaw("Horizontal") != 0)
        {
            transform.localScale = new Vector3(Input.GetAxisRaw("Horizontal"), 1, 1);

            Vector2 velocityX;
            velocityX = new Vector2(Input.GetAxisRaw("Horizontal") * GroundedMoveSpeed, 0);
            //rb.velocity = new Vector2(velocityX.x, rb.velocity.y);  
            rb.AddForce(velocityX);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
    }

as you can see, the line " //rb.velocity = new Vector2(velocityX.x, rb.velocity.y); "
is commented out. This was the way the character initially handled movement, until i discovered the wall-holding bug. I then tried the line after, rb.addforce. This solves the wall holding, but now the character has a sort of slow buildup of speed, which i don’t want. I was going for quick, responsive, controls. Is there a way to fix my initial way of doing it, or is addforce really the only option if i dont want some sort of overly convoluted scheme?

You might be able to stop this from happening by adding a Physics Material 2D to your player, and make the material have a Friction of 0.

Just create the material in the IDE with Friction 0, and then drag it onto your player game object.

I have done that before to stop the player snagging on a wall.

1 Like

Yep, it’s most likely friction.

Also, adding forces in Update is generally a bad idea. Update runs on a different frequency from fixedUpdate, and a variable one at that. Sometimes you might get 0 AddForce calls per physics timestep, sometimes you might get 1, sometimes you might get 3. This will lead to inconsistent controls. It depends on the framerate and how that lines up with physics timesteps…

Perhaps i should move it into fixedUpdate then?