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 /
avatar image
0
Question by bman85 · Aug 26, 2013 at 03:37 AM · javascriptiosjump

Jumping Problem?

I am using GetMouseButtonDown to allow a sphere in my game to jump. The problem is, you are able to jump multiple times right in a row and pretty much fly. What is the simplest way to only allow the player to only jump once at a time?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 26, 2013 at 03:42 AM

The typical way is to only allow a character to jump if it is grounded. How you detect ground will depend on your game. You can:

  • You can pick a certain height and call grounded anything that has a 'y' value less than the specified height.

  • You can do a Physics.Raycast() down and detect the distance to the surface. Under some threshold, and you are grounded.

  • You can detect a collision and either based on the type of collision or based on the contact points say something is grounded. See OnCollisionEnter().

Assuming your jump runs in a fixed amount time, another solution is to lock the user out from jumping for the time of the jump.

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 Ozzyel-PT · Aug 26, 2013 at 04:14 AM

If you're using a Character Controller, make a condition if it's grounded.

 private var ableToJump = true;
 function Start () {
     controller = GetComponent(CharacterController);
 }
 
 function Update (){
         if (controller.isGrounded)
         ableToJump = true;
                 //move y dierction command....
         }else{
                 ableToJump = false;
     }
 }

Hope it helps.

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 pickle chips · Aug 26, 2013 at 03:48 AM

If there is a character controller attached to the sphere, you could use the CharacterController.IsGrounded property to check whether the sphere is back on the ground.

Otherwise, you could use a float variable, to time how long it takes until you can jump again, which is very simple - but not the best way, Using code such as this:

 float timeAfterClicked; //How long has it been since they last clicked?
 float timeToJump //The cooldown of how long you want it to be until they can jump again
     
 void Update() {
    if (Input.GetMouseButtonDown(0) && timeAfterClicked >= 0) {
       Jump(); //Whatever code you use to jump
       timeAfterClicked += timeToJump;
    } 
    if (timeAfterClicked > 0) {
        timeAfterClicked -= Time.deltatime;
    }
 }

This will time how long it's taken since your last jump, and after the cooldown time (timeToJump) has passed, you will be able to jump again.

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

17 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

Related Questions

Bullets sometimes flying through enemies in Unity2D 2 Answers

Allow users to input a list? 0 Answers

If Grounded 2 Answers

AddForce to sphere 1 Answer

unity2d javascript jump help 1 Answer


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