- Home /
how to do a jump relative to the time the key spent down ?
So i did this :
if (Input.GetKeyDown(KeyCode.Space)) {
inputTime = Time.time ;
}
if (Input.GetKeyUp(KeyCode.Space))
{
if ( Time.time - inputTime < 0.2)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = jumpSpeed * 1.3f;
}
} but, yes... there is a but, i want the same thing happens... only when i press Down the key and not when the key come up. I can't figure out how to do it. Is this possible ?
Duplicate post: http://answers.unity3d.com/questions/300496/how-to-do-2-distinctives-jumps-relative-to-the-tim.html
please don't post the same question multiple times
Answer by aldonaletto · Aug 12, 2012 at 07:35 PM
You can start a timer when the key goes down, and do the jump when the key goes up (small jump) or when a certain time has elapsed (bigger jump):
private var timer: float = 0; ... if (Input.GetKeyDown(KeyCode.Space)){ timer = 0.2; // start the timer when the key goes down } if (timer > 0){ // if timer is running... if (Input.GetKeyUp(KeyCode.Space)){ // and key goes up... timer = 0; // deactivate the timer... moveDirection.y = jumpSpeed; // and do a regular jump } else { // but if key still pressed... timer -= Time.deltaTime; // decrement the timer if (timer <= 0){ // if time elapsed... // jump anyway with increased velocity: moveDirection.y = jumpSpeed * 1.3f; } } } ...
Oops! UA ate part of the last if due to the character < - it "thought" this was a tag. Answer fixed.
Very cool friend this worked like a charm :) thanks aldonaletto.
Answer by AlwaysSunny · Aug 12, 2012 at 07:28 PM
You could execute your jump function OnKeyDown and begin checking the elapsed time while the key is down. Continue adding force or velocity or whatever as time goes by until a certain threshold is reached.
Press key
Start timer
Start jump
if timer expires, stop adding force
or
if key released, stop adding force
Your answer
Follow this Question
Related Questions
How to do 2 distinctives jumps relative to the time the key spent down? 0 Answers
Jump same amount, no matter how long jump key is held 1 Answer
Problems with jump script 1 Answer
Time Delay Animation 1 Answer
Rigid Body Wait 1 Answer