- Home /
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!
Answer by enestelli · Oct 03, 2020 at 02:18 PM
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);
}
}
I tested many lines of code from youtube tutorials, from discord servers and Unity's QA, but this is THE answer! It gives me the result I was looking for, thanks a lot.
Also thanks to the person that gave the first asnwer, even though is not the code I will ultmately use, it really helped me to understand some things.
Thank you!
Really happy to solved your problem! Think twice code once! I just tried to think simple.
Answer by UnityToMakeMoney · Oct 02, 2020 at 03:49 AM
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. :)
Answer by unity_KyRi1TJbI0K3vA · Oct 03, 2020 at 03:57 AM
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
After looking at your new code there are some improvements I will like to make:
Rigidbody2D rb = GetComponent<Rigidbody2D>();//the rigidbody component of this gameobject
if(rb.velocity.x == 0)//if this gameobject is not moving
walkSpeed = originalSpeed;//reset 'walkSpeed'
//an 'else' is not needed since we simply won't change the value if moving
if (Input.GetButton("Run") )
{
if(walkSpeed < runSpeed)//increase speed only if not at max
walkSpeed += Time.deltaTime;
rb.velocity = new Vector2(input * walkSpeed, rb.velocity.y);
}
/*similar to the original, but removed redundant code*/
As for "linear drag", it is located on the object's rigid body. What it pretty much does is increase the friction (by air or land) in any given linear direction (up, down, right, and left).
Here is a video where someone else uses it: Link. @unity_KyRi1TJbI0K3vA Let me know if you need any more help. :)
Your answer
Follow this Question
Related Questions
Running straight 2 Answers
What's wrong with my sprinting logic? 1 Answer
idle > walk > run animation 1 Answer