Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ElPerro54 · Apr 20, 2017 at 05:24 PM · jumping2d platformerdouble jump

2d Platformer double jump wont work

Hi guys, Im trying to set up the double jump code for my 2d platformer but it doesnt seem to work.

 if (Input.GetKey(KeyCode.Space) && onGround)
         {
                 rb.velocity = new Vector2(rb.velocity.x, jumpheight);
                 canDoubleJump = true;
         }
 if (Input.GetKey(KeyCode.Space) && canDoubleJump && !onGround)
         {
                 rb.velocity = new Vector2(rb.velocity.x, jumpheight);
                 canDoubleJump = false;
         }

So first im checking if im on the ground so I cant jump forever then setting canDoubleJump bool to true, so the next time I press the jump key while im not on the ground I can jump again. But the problem is as soon as I jump the canDoubleJump bool goes true then instantly false so I can only jump once. Any help? I also tried this code from a youtube video but it has the same effect:

  if (Input.GetKey(KeyCode.Space))
         {
             if (onGround) { 
                 rb.velocity = new Vector2(rb.velocity.x,jumpheight);
                 canDoubleJump = true;
             }
             else
             {
                 if (canDoubleJump)
                 {
                     
                     //rb.velocity = new Vector2(rb.velocity.x, 0);
                     rb.velocity = new Vector2(rb.velocity.x, jumpheight);
                     //rb.AddForce(Vector2.up * jumpheight);
                     canDoubleJump = false;
                 }
             }
         }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Commoble · Apr 20, 2017 at 05:25 PM

The fundamental problem here is that Input.GetKey doesn't "clear" the keypress; if the key is being held during that frame, it returns true. However, since framerates tend to be much faster than human fingers, a single press of the spacebar will hold it down for many frames.

With this in mind, both blocks of code don't appear to work for the same reason: When the user presses the spacebar, the player object will start jumping. In the next frame, the player object will have left the ground, but the user will still be holding the spacebar, so the double-jump code triggers immediately.

A simple way to fix this is to use Input.GetKeyDown() instead of Input.GetKey(). GetKeyDown only returns true on the very first frame of the keypress; it will return false in subsequent frames until the user releases the key and presses it again.

I also suggest using your second code block with this; the code is cleaner and will cause fewer additional problems down the line.

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 ElPerro54 · Apr 20, 2017 at 05:53 PM

Thank you so much, this fixed it!

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

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

99 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

Related Questions

2D Top-Down Platforming like Mario and Luigi: Super Star Saga 1 Answer

2D platformer Touch button? 1 Answer

My player is double jumping when it should not be able to. I am not sure what is allowing it to do so. 0 Answers

Player not double jumping. 0 Answers

player not able to double jump 0 Answers


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