Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_KyRi1TJbI0K3vA · Oct 02, 2020 at 03:19 AM · 2d-platformerrunrunning

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityToMakeMoney · Oct 03, 2020 at 05:06 PM 0
Share

So did you manage to solve all your problems?

avatar image unity_KyRi1TJbI0K3vA · Oct 06, 2020 at 06:50 AM 0
Share

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!

avatar image enestelli unity_KyRi1TJbI0K3vA · Oct 06, 2020 at 12:58 PM 0
Share

Really happy to solved your problem! Think twice code once! I just tried to think simple.

avatar image
0

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. :)

@unity_KyRi1TJbI0K3vA

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityToMakeMoney · Oct 03, 2020 at 06:14 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Running straight 2 Answers

What's wrong with my sprinting logic? 1 Answer

idle > walk > run animation 1 Answer

Can someone help me add running to my script? 1 Answer

Character Jumping while Sprinting Code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges