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 /
This question was closed Mar 07, 2015 at 04:17 PM by FatalVitality for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by FatalVitality · Feb 18, 2014 at 07:28 PM · c#2djumpdouble jump

(Solved) Double jumping

Hi, I'm simply trying to make my player able to double jump. So far I've been able to make my player able to jump once until it's grounded again; then I tried to add a jump counter. However Unity gives me an error message on jumpCount +1; Does anyone know what I've done wrong and how to fix this?

 public float moveSpeed = 10f;
 public float jumpForce = 50f;
 int jumpCount = 1;
 
 bool grounded = false;
 
 void Update () 
 {
 //Jumping
 if(jumpCount > 3 || grounded && Input.GetKeyDown(KeyCode.UpArrow))
     {
 rigidbody2D.AddForce(new Vector2(0,jumpForce));
 grounded = false;
 jumpCount + 1;
     }
 }
Comment
Add comment · Show 2
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 NeoMakina · Sep 27, 2019 at 04:00 PM 0
Share

if((count <3 || grounded ) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)) jumpCount less the 3 then don't forget about operators precedence, without parentheses the && will be evaluated before the ||

avatar image jonicraft23 · May 03, 2020 at 10:48 AM 0
Share

Here is my simple solution : int jumpCount = 1;

 if(Input.GetButtonDown("Jump") && jumpCount < 2)
         {
             rb.AddForce(Vector2.up *150f);
             jumpCount++;
         }
         if(rb.velocity.y ==0){
             jumpCount = 1;
             }

so when the player collision with the ground (rigid body velocity y = 0 ) the jump count will reset to 1, and you can change the jumpCount in if condition to measure the jump count like jumpCount <3 for 3 jump and more and more..

3 Replies

  • Sort: 
avatar image
19
Best Answer

Answer by Komak57 · Feb 18, 2014 at 07:45 PM

Usually, when jumping, you would want to zero out vertical velocity before adding force to counter any gravity. Next, you have 2 scenarios you want to watch for. Grounded & jumping, Airborn & jumping & candoublejump. So you want something like

 if (jumpkeydown) {
   if (grounded) {
     rigidbody2D.velocity.y = 0;
     rigidbody2D.AddForce(new Vector2(0, jumpForce));
     candobulejump = true;
   } else {
     if (candoublejump) {
       candoublejump = false;
       rigidbody2D.velocity.y = 0;
       rigidbody2D.AddForce(new Vector2(0, jumpForce));
   }
 }

values and descriptions may vary.

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 FatalVitality · Feb 18, 2014 at 08:12 PM 0
Share

Thanks man, it works :D

avatar image Lord_Ford · Jun 25, 2015 at 05:03 AM 0
Share

Thank you very much. This is very simple and works great -Cheers :D

avatar image _DkC · Jul 17, 2015 at 07:18 AM 2
Share

Thank you so very much man! :D I was having a problem with my character jumping WAY too high when you double tap the jump button and setting the y-axis velocity to 0 solved that.

For Unity 5.1 users, ins$$anonymous$$d of:

rigidbody2d.velocity.y = 0;

Do this:

rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, 0);

Thanks!

avatar image
1

Answer by Josh707 · Feb 18, 2014 at 07:42 PM

 jumpCount > 3 || grounded

I think you mean less than, every time you jump the counter goes up, once it's over 3 you can jump continuously. The error is also because you aren't assigning that calculation to a variable, unlike Python you can't just write '1+1' and it will print the result. It will have to be 'jumpCount = jumpCount + 1;', or just += 1 or better yet jumpCount++.

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 FatalVitality · Feb 18, 2014 at 07:51 PM 0
Share

Ok, you were right about my > mistake and the +=1 seems to work. Now my problem is to somehow reset the jumping counter, do you know how?

avatar image MineBoss47 FatalVitality · Dec 22, 2020 at 05:50 PM 0
Share

to reset jump counter just put

jumpCount = 1;

avatar image Karios · Oct 27, 2015 at 09:42 AM 0
Share

can you give me a code to limit it two jump rate in a time span ,i have been trying but no success

avatar image
0

Answer by JayOhhh · Feb 18, 2014 at 07:43 PM

You should try assigning jump count the +1 value. As you have it right now your variable is not being assigned your +1 value.

 jumpCount = jumpCount + 1;
Comment
Add comment · Show 2 · 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 Kidroflz · Mar 26, 2017 at 03:11 AM 0
Share

Top comment didnt work for me he just would jump extremely high ins$$anonymous$$d of jumping twice so in the else statement i just wrote a different condition adding the space bar being pressed again... if this helps ive only been program$$anonymous$$g now for a week so if my code is stupid, my bad

 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && OnGround && Jumpcount < 2) {
             jumpForce = 360f;
             rigid.AddRelativeForce (new Vector2 (0, jumpForce));
             candoublejump = true;
 
         } 
         else 
         {
             if (candoublejump == true && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) 
             {
                 jumpForce = 300f;
                 rigid.AddRelativeForce (new Vector2 (0, jumpForce));
                 candoublejump = false;
             
             }
         }
 
avatar image JeanLaurore101 · Nov 26, 2018 at 09:22 PM 0
Share

That code work for me ins$$anonymous$$d of the other one above. Thanks for sharing!

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Character doesn't jump repeatedly 1 Answer

2d Beat Em Up Jump Functionality 0 Answers

Jumping from a floor to another floor (2D) 2 Answers

Accelerating speed 2 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