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
1
Question by Dr_GeoFry · Apr 20, 2012 at 01:14 AM · androidjumpingconstrain

Constrain Total Number of Jumps

Hi,

I am curious how you can limit the total number of jumps in a game. I currently only want one jump, but later might implement a double jump. More to the point I would like to base jumping on collisions. That is to say, if the hero jumps and hits a wall, the jumpCounter should reset back to zero allowing the hero to start jumping again.

Here is a snippet:

 var isFalling : boolean=true;  
 
 function Update () {
     var fingerCount = 0;
     for (var touch : Touch in Input.touches) {
          if (touch.phase == TouchPhase.Began)
             fingerCount ++;
     }
     if (fingerCount > 0)
         rigidbody.velocity.y=7;
     }
         
 function OnCollisionStay(collisionInfo : Collision) {
     isFalling=false;
 
 }

Thank you

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 aldonaletto · Apr 20, 2012 at 01:37 AM

I would have a jumping flag: only jump when it's false and nJump < maxJumps. When jumping, set the jumping flag. In OnCollisionStay, clear jumping and count the jump only when was jumping and landed on the ground:

var jumping = true; var maxJumps = 1; var nJump = 0;

function Update () { var fingerCount = 0; for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) fingerCount ++; } // only jumps if not jumping yet and if maxJumps not exceeded if (fingerCount > 0 && !jumping && nJump < maxJumps){ rigidbody.velocity.y = 7; jumping = true; // is jumping now } }

function OnCollisionStay(collisionInfo : Collision) { // if was jumping and landed in some Ground object... if (jumping && collisionInfo.gameObject.CompareTag("Ground")){ jumping = false; // jump ended nJump++; // count the jump } }

Comment
Add comment · Show 4 · 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 Dr_GeoFry · Apr 20, 2012 at 02:14 AM 0
Share

I'll try this out when I get home, but doesn't there need to be some sort of touchCount restriction like:

touchCount < 2; since every touch triggers a jump?

avatar image aldonaletto · Apr 20, 2012 at 02:48 AM 0
Share

I don't know much about touches, but it seems that touchCount only tells how many fingers are touching the screen. In the script above, touches are ignored when jumping is true. When you do the first touch, the character is allowed to jump because jumping is false. This sets jumping to true, and it's only reset to false when the character lands on some Ground tagged object - and then the jump is counted, thus no more jumps will occur until the counter is zeroed.

avatar image Dr_GeoFry · Apr 20, 2012 at 03:10 AM 0
Share

That all makes sense. I will be home soon to take a look. I just was curious what this means in javascript:

!jumping I know what == and != means etc, but just curious how !jumping is being used...

As always, thank you for the assistance.

avatar image aldonaletto · Apr 20, 2012 at 03:49 AM 0
Share

!jumping means not jumping, or jumping == false. The ! operator is the logical NOT, and works in javascript, C#, Boo - basically all languages created after the old and good C

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Moving forward whilst jumping 3 Answers

Physics on a Ball 1 Answer

Make a character jump when user taps the screen of an android device 1 Answer

How To Create Android 3d Game Jump Button 2 Answers

Jumping irregularities 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