Non slippery movement

Ive tried making non slippery movement. I’m new to unity, and there are a few problems with the code I am using.

public class PlayerControl : MonoBehaviour
{
    public new Rigidbody2D rigidbody2D;
    public float mSpeed = 5;
    private bool moving;
   
    void Start()
    {
        
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    Vector2 moveKeys = Vector2.zero;

   

    void Update()
    {
        float moveX = Input.GetAxis("Horizontal");
        
        if (moveX == 1 || moveX == -1)
        {
            moving = true;
            
        }
        else moving = false;


        moveKeys = new Vector2(moveX, 0).normalized;

        
    }

    public void FixedUpdate()
    {
        if (moving == true)
        { rigidbody2D.velocity = new Vector3(moveKeys.x * mSpeed, 0);

            Debug.Log("Function Called");

        }
    }
}
  1. There is a delay for before I start moving and before I stop moving.
  2. When I reach the ground, my character cannot move, only when in the air.
  3. When moving in the air my vertical movement stops. This is most likely because in the line that moves my character’s vector3’s y is equal to zero, possible stopping vertical movement? I do not know a solution to this.

Please Help me

  1. Use Input.GetAxisRaw or round your axis value to get it to reach 1 faster.

  1. Your set velocity should include the current y velocity, like this:

    rigidbody2D.velocity = new Vector3(moveKeys.x * mSpeed, rigidbody2D.velocity.y);
    

This will maintain any vertical velocity your rigidbody has.


And 3. There’s no need to only set your velocity if you’re moving. Just set it always to moveKeys.x and it will work the same.

@CamphorStudios