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 Tyler 2 · Jan 25, 2011 at 06:07 AM · buttontime

Measuring time?

I want to make a super mario bros. style jump, where the longer the button is held, the higher the player moves. How can I measure the amount of time that a button is held down?

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

2 Replies

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

Answer by tertle · Jan 25, 2011 at 06:56 AM

I think the best approach would be to just keep jumping, up to a maximum height, while the button is held down. No need to record how long the button was held.

That said, to answer your actual question I believe you could just use GetKeyDown and GetKeyUp to record the time a button is held.

ie.

private float _timeDown = 0.0f; private bool _isDown = false;

public class Jump : MonoBehaviour { void update() { if (Input.GetKeyDown (KeyCode.Space)) _isDown = true; else if (Input.GetKeyUp (KeyCode.Space)) _isDown = false;

      if (_isDown)
           _timeDown += Time.deltaTime;
 } 

}

Obviosuly you'll want to reset _isDown sometime or whatever.

Comment
Add comment · Show 2 · 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 Tyler 2 · Jan 26, 2011 at 12:37 AM 0
Share

Actually, your idea sounds better than $$anonymous$$e. How would I measure the y axis distance from the "ground" (I would have to attach an object to the player's feet and get the distance from that, since I would be jumping from stairs, platforms, etc. and not just a constant starting point)?

avatar image tertle · Jan 27, 2011 at 12:48 AM 0
Share

If you actually look in the character controller.unity package it comes with some really nice scripts which you can use as reference. The Character$$anonymous$$otor script has pretty much exactly what you want (though supports 3D). It's all in js but if you use c# it's still easy enough to read (didn't take me that long to port the whole file to c# when I first started)

avatar image
1

Answer by bearcano · Jan 25, 2011 at 09:04 AM

If you simply calculate the time the button is pressed to determine the jump height, you won't be able to start the jump until after the button is done being pressed. This is (probably) not the behavior you want.

Perhaps what you should do is have a value for your jump speed, and then a max jump height. Then while the button is pressed, have the player jump for as long as they haven't reached the max jump height.

variables:

private float jumpSpeed = 2.0f;
private float maxJumpHeight = 10.0f;
private float currentJumpHeight = 0.0f;
private bool canJump;

and then wherever your update function is where you're handling the jumping...

float yMovementModifier = 0.0f; if (canJump) { if (Input.GetKey(KeyCode.Space)) { //if our jump hasn't hit the max jump height yet... if (currentJumpHeight < (maxJumpHeight - (jumpSpeed * Time.deltaTime))) { //add our jumpspeed to our current jump height currentJumpHeight += jumpSpeed * Time.deltaTime; yMovementModifier = jumpSpeed * Time.deltaTime;

         }
         else
         {
             //we're at max height, so only allow the last bit of jump
             currentJumpHeight = maxJumpHeight;
             yMovementModifier = maxJumpHeight - (jumpSpeed * Time.deltaTime);

             //also, since we're at max jump height, set a flag saying we can't jump anymore
             canJump = false;
         }
     }
 }
 else
 {
     //your code here should check when the player hits the ground
     //and then set the following flag:
     canJump = true;
     currentJumpHeight = 0.0f;
 }

 //update your player's position
 transform.position.y += yMovementModifier;

This is far from the most efficient way to do it (and it may be a little buggy) but hopefully you get the idea.

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

No one has followed this question yet.

Related Questions

Checking to see how many times a button was pressed in a certain amount of time? 2 Answers

how to find time of a button pressed 2 Answers

Problem with finding the right syntax for time. 1 Answer

multiple GUI.Buttons at the same time 1 Answer

Game slows down after restart 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