2D Game - Buggy Movement

Hey everyone,
I realize that this is a pretty beaten horse; there are a lot of tutorials out there on unity movement.
However, I can’t seem to find anything addressing a specific problem that I’ve been having, in which the player’s movement scheme is buttery (in the sense that moving let and right result in the player gliding, as though they’re on ice, instead of stopping when the left or right keys stop being pressed) and while jumping works fine, trying to jump while holding down the left or right keys result in an extremely small jump, and jumping while the player is “gliding” results in all forward momentum being cancelled and the character jumping straight up. Trying to move the character in mid air makes gravity basically stop affecting them, and they “float” down very slowly if the player is holding the left or right keys.

What I’m looking for is short, snappy movement, where the character only moves when the player holds down the left or right keys. Jumping while moving in a direction should allow the player to jump in that direction (instead of momentum being cancelled) and the player should be able to move in mid air (though gravity should affect them).

I’ve tried using addForce() and directly manipulating the velocity, both of which have resulted in the same, buttery movement and broken jump. transform.Translate() seemed to work for left and right movement but not for jump, but most tutorials I’ve come across say not to use that for Rigidbody2D objects.

This is my movement code, for reference:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class WASDMove : MonoBehaviour {


    const float OPPCONST = 1.0f; //used to increment or decrement speed of character.


    const float BASESPEED = 5f; //the base speed of the player.


    private float speed; //speed of the player (changes).


    private bool isGrounded; //tells the console if the player is on the ground or not.


    private int doubleJump; /*checks to see how many times the player has jumped. If isGrounded is true and doubleJump is 1, can jump twice.  if isGrounded is false and

    doubleJump is 1, then the player is falling and can jump once more. The first time the player jumps, nothing happens to doubleJump. the second time they jump it becomes 0

    and the player can longer jump until isGrounded becomes true again, at which point double jump becomes 1 again.*/


    private bool movePressed = false;


    private bool leftPressed = false;


    public globalHP health;


    public changeSize size;


    private float forceX;


    private float forceY;


    private float jumpSpeed;


    const float DRAG = .3f;


    // sets the speed and tells the console that the player is on the ground, so that they may do a double jump.

    void Start()

    {

        setSpeed();

        forceX = reset();

        forceY = reset();

        jumpSpeed = 10f;

    }


    public void OnCollisionEnter2D(Collision2D Collide)

    {

        if (Collide.gameObject.tag == "ground")

        {

            doubleJump = 2;

            isGrounded = true;

        } else {

            isGrounded = false;

        }

    }


    public void OnCollisionExit2D(Collision2D Collide)

    {

        doubleJump = 1;

    }



    //sets the speed of the player based on an equation that makes the player faster if they're smaller, and slower if they're larger.

    public void setSpeed()

    {

        if (health.getHP() == 1.0f)

        {

            speed = BASESPEED;

        }

        else

        {

            speed = BASESPEED * (OPPCONST + (OPPCONST - health.getHP()));

        }

    }

  

    //returns speed.

    public float getSpeed()

    {

        return speed;

    }


    private float reset()

    {

        return 0f;

    }


       // Update moves the player left, right and lets them jump.

       void Update () {

        leftPressed = false;

        forceX = reset();

        forceY = reset();

        movePressed = false;


        setSpeed();

        jumpSpeed = getSpeed();


        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))

        {

            //GetComponent<Rigidbody2D>().velocity = new Vector2(1, 0) * getSpeed() * -1;

            forceX = getSpeed() * -1;

            transform.localScale = new Vector3(-(size.getScale()), (size.getScale()), (size.getScale()));

            movePressed = true;

            leftPressed = true;

        }

        else

        {

            forceX = 0f;

        }


        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))

        {

            //GetComponent<Rigidbody2D>().velocity = new Vector2(1, 0) * getSpeed();

            forceX = getSpeed();

            transform.localScale = new Vector3((size.getScale()), (size.getScale()), (size.getScale()));

            movePressed = true;


        } else if (leftPressed == false)

        {

            forceX = 0f;

        }

        if (Input.GetKeyDown(KeyCode.Space) && health.getHP() > .1)

        {

            if (doubleJump == 0)

            {

                return;

            }

            else if(doubleJump == 2)

            {

                forceY = jumpSpeed;

                doubleJump = doubleJump - 1;

                movePressed = true;


            } else {


                forceY = jumpSpeed;

                doubleJump = doubleJump - 1;

                movePressed = true;

                health.setHP(health.getHP() * .95f);

                size.scaleChange(.05f);

                size.alter();


            }

        }



        if(movePressed == true) {

            GetComponent<Rigidbody2D>().velocity = new Vector2(forceX, forceY);

        }


    }


}

What am I doing wrong, or what should I be doing? Thanks!

You are using Velocity, which is going to have that “gliding” effect. It sounds like you want more stop-and-go movement. Have you checked out the move object script in the 2D rogue-like tutorial? It seems much closer to what you want. Even if it isn’t exact, the core mechanic in the script should help out: Unit Mechanics - Unity Learn