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 Fujitaka · Oct 09, 2016 at 11:47 PM · rigidbodyaddforcedouble jump

Player Jumps twice with double jump deactivated

Hey guys! I've got a bit of a problem that I just can't figure out how to solve. I'm making a new character controller script and I'm planing an having a double jump in my game when it is unlocked. I'm using Rigidbody.AddForce so I can make another script working (not relevant here).

Everything works so far except that the player can jump twice even though double jump is not active. Here's the relevant code:

     if(grounded == true)
     {
         maxVel = 15.0f;
         timesJumped = 0;
     }
     else
     {
         maxVel = 7.5f;
     }

     if(Input.GetKeyDown(KeyCode.W) && timesJumped < maxJumps)
     {
         Debug.Log("Check " + timesJumped);
         timesJumped++;
         rBody.AddForce(gameObject.transform.up * jump, ForceMode.Impulse);
     }

The problem seems to be, that when I hit W to fast after jumping once, that it doesn't increment "timesJumped" fast enough and because of that the game executes the code a second time... The even stranger thing about that is, that "timesJumped" increments only once even if the player jumps twice. I've even tied using a Coroutine to account for this "lag", but nothing works. The player is able to jump twice regardless.

If you need more information's, just ask and I will provide it ^^

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Geometrical · Oct 09, 2016 at 11:59 PM

The code seems logically correct to me, but perhaps if you provide more information I'll be able to help. Specifically:

  1. How are you checking whether the player is grounded or not?

  2. In which method are you executing this code?

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 Fujitaka · Oct 10, 2016 at 12:08 AM 0
Share
  1. The grounding is checked by another script that is attached to an empty child object with a Collider. It uses OnTriggerEnter to set it to true in case of a collision with the ground and if the player has enough Y-distance to the ground, then it set's it to false again. (That is literally the only thing that script does)

  2. Everything I've posted in my question is executed in the Update function.

Thank you for helping ^^

avatar image Geometrical Fujitaka · Oct 10, 2016 at 12:38 AM 0
Share

You should never put any physics related code in the Update function, use FixedUpdate ins$$anonymous$$d. Try it and report back the results.

avatar image Fujitaka Geometrical · Oct 10, 2016 at 12:50 AM 0
Share

Just tried that, but I get the same results... But thanks for the info, anyway ^^

Imma go to sleep now. It's late over here. I'll check back tomorrow.

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

90 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

Related Questions

How to remove addforce effect 0 Answers

Rigidbody.addForce is not working 10 Answers

Having a OnTriggerEnter make another gameobject Add Force in UnityScript. 1 Answer

rigidbody,addforce is affecting all clones which are clicked on through raycast 0 Answers

Ello~ I need help with transform.parent = null; When I play it and press the DropWeapon input button it goes in a random location and doesnt move . 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