Smooth player movement with joystick

Hello.

I would like to get some advice. How to make player control smoother? I know my animations are bad. But, I’m talking about the code now, maybe something can be improved here

gracefulunderstatedanteater

private void Move()
    {
        // Player's  inputs
        //float horizontalInput = Input.GetAxisRaw("Horizontal");
        //float verticalInput = Input.GetAxisRaw("Vertical");

        float horizontalInput = joystick.Horizontal;
        float verticalInput = joystick.Vertical;

        Vector3 inputs = new Vector3(horizontalInput, 0, verticalInput);
        Vector3 movement = inputs.normalized * Time.deltaTime * speed;

        //Reset moves
        if (horizontalInput == 0.0f && verticalInput == 0.0f)
        {
            animator.SetBool("LeftMove", false);
            animator.SetBool("RightMove", false);
            animator.SetBool("ForwardMove", false);
            animator.SetBool("BackMove", false);
        }

        //Left Move
        if (horizontalInput == -1.0f && verticalInput == 0.0f)
        {
            animator.SetBool("LeftMove", true);
            animator.SetBool("RightMove", false);
            animator.SetBool("ForwardMove", false);
            animator.SetBool("BackMove", false);
        }

        //Right Move
        if (horizontalInput == 1.0f && verticalInput == 0.0f)
        {
            animator.SetBool("RightMove", true);
            animator.SetBool("LeftMove", false);
            animator.SetBool("ForwardMove", false);
            animator.SetBool("BackMove", false);
        }

        //Forward Move
        if (horizontalInput == 0.0f && verticalInput == 1.0f)
        {
            animator.SetBool("ForwardMove", true);
            animator.SetBool("BackMove", false);
            animator.SetBool("LeftMove", false);
            animator.SetBool("RightMove", false);
        }

        //Back Move
        if (horizontalInput == 0.0f && verticalInput == -1.0f)
        {
            animator.SetBool("BackMove", true);
            animator.SetBool("ForwardMove", false);
            animator.SetBool("LeftMove", false);
            animator.SetBool("RightMove", false);
        }

        player.Move(movement);
    }

    public void Look()
    {
        transform.LookAt(enemy.transform.position, Vector3.up);
    }

You could apply this approach to the read-in input quantity, effectively time-filtering it.

Or you could apply the approach to the player’s stored velocity, changing it smoothly regardless of how abrupt the inputs were.

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub