Save the Time

Hi.I want to make a CharacterController jump script which jump speed depends on how long you press jump button.

(Like every other Platform Game.)

This is the function that decides jump speed.

function SetJumpSpeed():float{

var timeKeeper:float;
var pressedTime:float=0.2;//0.2second

timeKeeper=Time.time;//save Time.time when the jump button was pushed.

if((Time.time-timeKeeper)>pressedTime&&Input.GetButton(“Jump”))

//if 0.2second has been passed since I pressed Jump Button and I am still pressing Jump Button.

jumpSpeed=8.0;//I can jump higher.

else jumpSpeed=4.0;

Debug.Log(“”+jumpSpeed);

return jumpSpeed;

}

jumpSpeed will be always 8.0.I guess this will happen because the line

timeKeeper=Time.time;

doesn’t work property in Update function.

Will you show me how I can save time when I pressed the button.
Thanks in advance,and sorry for my poor grammar.

What you need to do is check when the button is PRESSED, then when it is RELEASED. The function you’ve written just resets the timeKeeper to the current time.

Look into Input.GetButtonDown and Input.GetButtonUp instead of GetButton

You record the time when the button goes down, you then subtract it from the current time when the button goes up - that is how long the button has been pressed for.