Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Bincredible · Mar 21, 2017 at 06:17 PM · rigidbody2djumpphysics2daddforce

Jump problem

Hi, I am having trouble making a jump which allows the player to either hold the screen to jump continuously or tap the screen once and just jump once. My script below is what I am using but since GetMouseButton(0) is being called more than once while the player is on the ground the rigid body is getting the force added more than once too which means the player flies way up into the air. Is there a way which I can call the rigidbody.AddForce() just once using this method or will I need to use a different method? thanks

     var jump : boolean;
     var grounded : boolean = false;
     var groundCheck : Transform;
     var groundRadius : float = 0.2f;
     var whatIsGround : LayerMask;
 var myRigidbody : Rigidbody2D;
     
     function FixedUpdate(){
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
     }
     
     function Update(){
         if(Input.GetMouseButton(0)){
             if(grounded){
                 jump = true;
             }    
         }
         if(jump){
             myRigidbody.AddForce(new Vector2(0, 1500f * Time.fixedDeltaTime));
             jump = 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

1 Reply

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

Answer by Hanoble · Mar 21, 2017 at 06:54 PM

You should debug your code (either through logs or breakpoints), but what I would assume is happening is that for several frames your grounded check is still true and thus the AddForce is being called several times.

The code you have is an okay starting point and one solution would be to implement something like a "minimum jump timer", which kicks off a timer and manages a "canJump" bool for you. This will need to be tweaked until it feels right with your grounded check, but something like this would work.

 //Duration is a public class variable you pass in
 private IEnumerator DisableJump(float duration)
 {
       float timer = duration;
       canJump = false;
 
       while(timer > 0.0f)
      {
            timer -= time.deltaTime;
            yield return null; 
      }
 
      canJump = true; 
 }

You would then call this using:

 StartCoroutine(DisableJump(jumpTimer));

Lastly, you would want to remove the setting of the jump bool from the update logic and check to make sure you can jump, something like this:

 function Update()
 {
       if(Input.GetMouseButton(0))
       {
              if(grounded && canJump)
              {
                    myRigidbody.AddForce(new Vector2(0, 1500f * Time.fixedDeltaTime));;            
                   StartCoroutine(DisableJump(jumpTimer));       
              }    
        }
 }

Unity also has a free character controller, and there are several others you can find out there for free, where they tackle similar issues. Looking at how they handle grounded checks and jump logic would be a good training exercise and something I would advise if you have the time for 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

66 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

Related Questions

player jumps too high when clicking with two fingers. 2 Answers

Use addForce to Jump 1 Answer

Velocity not updating object position 1 Answer

How can I increase or decrease jump speed while keeping the jump arc the same? 1 Answer

Character doesn't fall back down when jumping 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