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 fulcanmal · Oct 01, 2010 at 11:10 PM · jumplogic2d-tutorial

ApplyJumping() Logic Problem

I'm having some trouble understanding the logic of the ApplyJumping() function of the Unity 2D side scroller tutorial.

function ApplyJumping () { // UNITY: Prevent jumping too fast after each other

if (jump.lastTime + jump.repeatTime > Time.time) return;

if (controller.isGrounded) { // UNITY:Jump // UNITY: - Only when pressing the button down //UNITY: - With a timeout so you can press the button slightly before landing

 if (jump.enabled && Time.time < jump.lastButtonTime + jump.timeout) {

     movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height);
     movement.inAirVelocity = lastPlatformVelocity;
     SendMessage ("DidJump", SendMessageOptions.DontRequireReceiver);
 }

}

}

If I understand the SendMessage function correctly, this calls the DidJump( ) function:

function DidJump () {

 jump.jumping = true;
 jump.reachedApex = false;
 jump.lastTime = Time.time;
 jump.lastStartHeight = transform.position.y;
 jump.lastButtonTime = -10;

}

As you can see this function sets lastButtonTime to -10.

In the Update() function lastButtonTime is set to the current time whenever the "Jump" button is pressed:

function Update () {

if (Input.GetButtonDown ("Jump") && canControl) {

 jump.lastButtonTime = Time.time;

}

ApplyJumping();

}

jump.timeout is declared with a value of 0.15 and jump.enabled is declared as true. So lets say the game has just started. We are 10 seconds in when the player presses the spacebar.

Time.time (10) < jump.lastButtonTime (10) + jump.timeout (0.15) is true. This if statement executes. The DidJump() function is triggered. lastButtonTime is set to -10. The character jumps.

He presses spacebar again 0.10 seconds after the first jump. And here I find one of the logic problems...

ApplyJumping() and the Input.GetAxis are both in the Update() function. When the spacebar is pressed the second time will lastButtonTime equal -10, as it was set to when DidJump() was executed, or will it equal Time.time because spacebar was pressed?

Because Input.GetAxis is first in the Update() function logic would seem to suggest that this code is executed first, and therefore lastButtonTime resets to Time.time, but if this is the case then the if statement (Time.time < lastButtonTime + jump.timeout) would always evaluate to true.

The comments from the unity devs say that this code allows you to press the jump button slightly before hitting the ground, allowing you to jump again. I'm not quite seeing the logic. I know it will seem ludicrously simple once someone points out to me what I'm missing.

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
Best Answer

Answer by Ehren · Oct 01, 2010 at 11:33 PM

It's a bit convoluted, but it actually works.

  • Update stores the last time the jump button was pressed (jump.lastButtonTime).
  • ApplyJumping only jumps when:
    • jump.repeatTime seconds have elapsed since jump.lastTime (note that jump.lastTime is different from jump.last*Button*Time)
    • The character is grounded
    • Jumping is enabled
    • The jump button was pressed in the last jump.timeout seconds.
  • When a jump actually occurs
    • lastButtonTime is set back to -10
    • lastTime is set to Time.time.

Storing the button time in Update allows you to press the jump button slightly before landing. In this case, the GetButtonDown event would occur in one frame (when the character was ungrounded), but would be stored and used in a subsequent frame (as long as the character became grounded before jump.timeout seconds had elapsed).

As for your question about pressing the spacebar again 0.10 seconds after the first jump, one of the following would occur:

  • The character would not jump if:
    • jump.repeatTime <= 0.10
    • Jumping is disabled
    • isGrounded == false, and stays false until jump.timeout seconds have passed
  • Otherwise, the character would jump.

Hope this helps!

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 fulcanmal · Oct 02, 2010 at 12:41 AM 0
Share

Ahhhh! The problem was a misconception on my part that this helped me to identify. I understand it now, thanks!

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

No one has followed this question yet.

Related Questions

TransformPoint and InverseTransformPoint 2 Answers

Click a cube to move player over to that position & rotate player to face fixed point 1 Answer

How do you make a prolonged jump script in JavaScript? 1 Answer

Better jumping with "Lerpz" character controller 1 Answer

IPhone gui jump button 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