Running in a 2D platformer (increase speed while holding down a button)

Hey guys! I’m very new to unity (just finished the “Create with Code course”) and now I’m learning some things with youtube tutorials. I want to do the typical “run” like in Mario games in which you gain speed while you press “x” button and I have something like this until now.


void Update()
    {
        float input = Input.GetAxis("Horizontal");
        if (Input.GetButton("Run"))
        {
            rb.velocity = new Vector2(input * runSpeed, rb.velocity.y);
        }
        else
        {
            rb.velocity = new Vector2(input * walkSpeed, rb.velocity.y);
        }

The problem with this is that the player goes from let’s say “5” (walkSpeed) of speed immediately to “10” (runSpeed), but what I want to accomplish is that the player goes 5, 6 ,7 ,8 ,9 up to 10 which will be the max speed.

I tried to look for the answer somewhere else, but I couldn’t find one that work with my project.

Thanks in advance!

I believe this template can help you.


if(speedVar <  speedVarMax)//increase only the max value hasn't been reached
     speedVar += Time.deltaTime;
rb.velocity = new Vector2(input * speedVar, rb.velocity.y);

Where speedVar is the speed variable that you want to use, and speedVarMax is the max value that speedVar can reach.

Note: It will only increase while the user is holding down the button, so this may result in the variable’s value to not be at its original value when holding the button again.


If you need more help let me know. :slight_smile:

@unity_KyRi1TJbI0K3vA

So the code looks something like this now:


void Update()
    {
        float input = Input.GetAxis("Horizontal");
  

        if (Input.GetButton("Run") && walkSpeed < runSpeed)
        {
            walkSpeed += Time.deltaTime;
            rb.velocity = new Vector2(input * walkSpeed, rb.velocity.y);
        }
        else
        {
            rb.velocity = new Vector2(input * walkSpeed, rb.velocity.y);
        }
        if (input > 0 && facingRight == false)
        {
            Flip();
        } 
        else if (input < 0 && facingRight == true)
        {
            Flip();
        }

First of all, thanks! Every time I press the button the movement speed increases until max BUT now the problem is that is doesn’t return to the normal. I tried with the “else” but is not working.

So let’s say I’m halfway through I read about something called linear drag to slow down the player but I don’t know how to apply it yet.

I will be trying some different things these days, any help highly appreciated

You can use something like this:

void Update()
{
    float input = Input.GetAxis("Horizontal");
   
    if (Input.GetKey(KeyCode.X))
    {
        if (runSpeed < maxSpeed)
        {
            runSpeed += Time.deltaTime;
        }
        rb.velocity = new Vector2(input * runSpeed, rb.velocity.y);
    }
    else if (Input.GetKeyUp(KeyCode.X))
    {
        runSpeed = 5; // reset the runSpeed to default
    }
    else
    {
        rb.velocity = new Vector2(input * walkSpeed, rb.velocity.y);
    }

}