Character Controller can accelerate but cant deccelerate

I’ve managed to get it so my character controller is able to accelerate when starting to move but it doesn’t deccelerate at all when I let go of my xbox joystick. I thought it would work based off of what i’ve coded but it seems to be dependant on the input. If anybody can give me some advice as to how to get it to deccelerate I would be grateful. Thanks in advance :slight_smile:

    /////// Move
    if (Player.isGrounded)
    {
        Vector3 Movement = new Vector3(HorizontalSpeed, 0, VerticalSpeed);
        Movement = transform.rotation * Movement;
        Player.Move(Movement * Time.deltaTime);

        // Horizontal

        // Right
        if (Input.GetAxis("Left Joystick Horizontal") >= 1)
        {
            HorizontalSpeed += Time.deltaTime * Acceleration;
        }
        else
        {
            while (HorizontalSpeed > 0)
            {
                HorizontalSpeed -= Time.deltaTime * Acceleration;
            }
        }

        // Left
        if (Input.GetAxis("Left Joystick Horizontal") <= -1)
        {
            HorizontalSpeed -= Time.deltaTime * Acceleration;
        }
        else
        {
            while (HorizontalSpeed < 0)
            {
                HorizontalSpeed += Time.deltaTime * Acceleration;
            }
        }

        HorizontalSpeed = Mathf.Clamp(HorizontalSpeed, -10, 10);
        if (HorizontalSpeed > -0.4 && HorizontalSpeed < 0.4)
        {
            HorizontalSpeed = 0;
        }

        // Vertical

        // Forward
        if (Input.GetAxis("Left Joystick Vertical") >= 1)
        {
            VerticalSpeed += Time.deltaTime * Acceleration;
        }
        else
        {
            while (VerticalSpeed > 0)
            {
                VerticalSpeed -= Time.deltaTime * Acceleration;
            }
        }

        // Back
        if (Input.GetAxis("Left Joystick Vertical") <= -1)
        {
            VerticalSpeed -= Time.deltaTime * Acceleration;
        }
        else
        {
            while (VerticalSpeed < 0)
            {
                VerticalSpeed += Time.deltaTime * Acceleration;
            }
        }

        if (VerticalSpeed > -0.4 && VerticalSpeed < 0.4)
        {
            VerticalSpeed = 0;
        }
    }

You might be confused about the behavior you'll get from this: while (HorizontalSpeed > 0)

@sajjad2016 I changed it. You just need to write a function yourself that gets the current scale value

1 Answer

1

Try removing the while loops which @TreyH was mentioning, if this is in the Update function it gets executed each frame, you’re using deltaTime to smooth out the in/decrease of acceleration over time anyways, the while loops will pin the code in the loops until it is greater/less than whatever logic you put in there in a single frame or in the case of FixedUpdate step.

 /////// Move
 if (Player.isGrounded)
 {
     Vector3 Movement = new Vector3(HorizontalSpeed, 0, VerticalSpeed);
     Movement = transform.rotation * Movement;
     Player.Move(Movement * Time.deltaTime);

     // Horizontal
     // Right
     if (Input.GetAxis("Left Joystick Horizontal") >= 1)
     {
         HorizontalSpeed += Time.deltaTime * Acceleration;
     }
     else if (HorizontalSpeed > 0)
     {
         HorizontalSpeed -= Time.deltaTime * Acceleration;
     }

     // Left
     if (Input.GetAxis("Left Joystick Horizontal") <= -1)
     {
         HorizontalSpeed -= Time.deltaTime * Acceleration;
     }
     else if (HorizontalSpeed < 0)
     {
         HorizontalSpeed += Time.deltaTime * Acceleration;
     }

     HorizontalSpeed = Mathf.Clamp(HorizontalSpeed, -10, 10);

     if (HorizontalSpeed > -0.4 && HorizontalSpeed < 0.4)
     {
         HorizontalSpeed = 0;
     }

     // Vertical
     // Forward
     if (Input.GetAxis("Left Joystick Vertical") >= 1)
     {
         VerticalSpeed += Time.deltaTime * Acceleration;
     }
     else if (VerticalSpeed > 0)
     {
         VerticalSpeed -= Time.deltaTime * Acceleration;
     }

     // Back
     if (Input.GetAxis("Left Joystick Vertical") <= -1)
     {
         VerticalSpeed -= Time.deltaTime * Acceleration;
     }
     else if (VerticalSpeed < 0)
     {
         VerticalSpeed += Time.deltaTime * Acceleration;
     }

     if (VerticalSpeed > -0.4 && VerticalSpeed < 0.4)
     {
         VerticalSpeed = 0;
     }
 }

Aww thats aweome! It works beautifully. Thanks man! :)