- Home /
How can I detect the time someone uses input?
I have a jump function that's using addforce to create the jump - There is also a double jump, that adds jumpSpeed to the first jump's velocity.y
Problem is that if the player presses the space bar really fast, the character jumps really high, because at the start of the jump velocity.y is much higher than it is a fraction of a second later
I want a jump and double jump function that works consistently - My idea is to divide the jump into seconds - if the player presses the spacebar twice within .1 seconds, then no force is added, etc etc
How can I record when the spacebar is pressed? If I know that, I can simply do (inputA - inputB) < 0.1f do nothing
Here's my jump function
void Jump()
{
rigidbody.AddForce(0,jumpSpeed,0);
jumpCount++;
isGrounded = false;
}
and how I call it
if((jumpCount < 2 && Input.GetButtonDown("Jump")) && !bossArea) // ******* jump
{
if(Random.Range(0,1) == 0 && jumpCount < 2)
audio.PlayOneShot(jump_a);
else
audio.PlayOneShot(jump_b);
}
Answer by robertbu · May 18, 2014 at 03:18 AM
You can do it this way. Record by:
var timestamp = Time.time;
Test by:
if (Time.time > timestamp + waitTime) {
// Do something
}
Or
var timestamp = Time.time + waitTime;
And test:
if (Time.time > timestamp) {
// Do something
}