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 /
avatar image
0
Question by JeanLaurore101 · Nov 28, 2018 at 04:24 PM · jumpingdouble jump

How can I stop my player from jumping after double jumps? My player consistently jumping.

public float jumpSpeed = 10f; public float jumpForce = 50f; int jumpCount = 1; public bool isGrounded; bool grounded = false; Rigidbody rb;

 bool canDoubleJump;

 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody>();
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown (KeyCode.Space) && isGrounded && jumpCount< 2)
     {
         jumpForce = 360f;
         rb.AddRelativeForce (new Vector3(0, jumpForce));
         canDoubleJump = true;
     }
     else
     {
         if (canDoubleJump == true && Input.GetKeyDown(KeyCode.Space))
         {
             jumpForce = 300f;
             rb.AddRelativeForce (new Vector3(0, jumpForce));
             canDoubleJump = false;
         }
     }
 }

 void EnableDoubleJump()
 {
     canDoubleJump = true;
 }

}

doublejumps.png (45.1 kB)
Comment
Add comment · Show 4
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 Vega4Life · Nov 28, 2018 at 04:35 PM 0
Share

Where is isGrounded being set? How are you incrementing jumpCount?

avatar image JeanLaurore101 Vega4Life · Nov 28, 2018 at 05:17 PM 0
Share

isGrounded is set in public class public bool isGrounded;

public float jumpSpeed = 10f; public float jumpForce = 50f; int jumpCount = 1; public bool isGrounded; bool grounded = false; Rigidbody rb;

avatar image Vega4Life JeanLaurore101 · Nov 28, 2018 at 05:26 PM 0
Share

Right, but what I mean is, what is setting them to true or false after you jump. What is increasing the jumpCount after you jump?

Show more comments

2 Replies

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

Answer by GamitusLabs · Nov 28, 2018 at 09:42 PM

Here's the logic I would use (pseudo-code)

 Update()
 {
     if(curJumps < maxJumps && !applyJump)        // allows for single/double/multi-jump
     {    
         if(KeyDown(Jump))
         {
             switch(curJumps)
             {
                 case 0:
                     if(grounded)
                     {
                         jumpForce = InitialJumpForce;
                         applyJump = true;
                         curJumps++;
                         break;
                     }
                 case 1:
                     jumpForce = AirbornJumpForce;
                     applyJump = true;
                     curJumps = 2;
                     break;
                 case 2:
                     // additional air-jumps hereafter
                 default:
                     // catchall
             }
         }
     }
 }
 
 FixedUpdate()
 {
     if(applyJump)
     {
         rb.AddForce(Up * jumpForce, Impulse);
         applyJump = false;
     }
 }
 
 OnCollisionEnter()
 {
     if(collider == ground)
     {
         curJumps = 0;
         grounded = true;
     }
 }
 
 OnCollisionExit()
 {
     if(collider == ground)
     {
         grounded = false;    
     }
 }
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 JeanLaurore101 · Nov 29, 2018 at 01:51 AM 0
Share

Thanks for your help!

avatar image GamitusLabs JeanLaurore101 · Nov 29, 2018 at 03:53 AM 0
Share

You're very welcome :)

avatar image
0

Answer by mayur7garg · Nov 28, 2018 at 06:30 PM

Make sure that isGrounded bool is set to false once the player is in the air. And I haven't got what is the use of jumpCount here.

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 JeanLaurore101 · Nov 28, 2018 at 08:13 PM 0
Share

jumpCount is used to calculate the number of jumps the player has to do, but I don't think it a good use.

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

98 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

Related Questions

Please Help me fix my code (jumping animation playing once) 0 Answers

Triple jumping instead of double 2 Answers

Double Jump Not working 2 Answers

2D double jump doesn't work. 2 Answers

Unity 2D: Variable Jump Height with a 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