Hi, for a racing game I’m working on, I had the idea of making a game mode in which you have to press as fast as you can a button to make the car go faster and win. But how could I detect the speed in which the player is pressing a button?
2 Answers
2Hi, try to save the current time with
float startT =time.time;
then count the amout of button presses by increasing an integer for each time the buttion is pressed
if (Input.getKeyDown(...)){
count++;
}
then when you want to evaluate the frequency store the current time
float endT = time.time;
then subtract the start time from the end time and divide the number of button pushes through the result
float frequency = count / (endT-startT);
Regards, BPR
To solve this problem, you don’t need to calculate button frequency. Instead make each button press add just a bit of force/speed So if you are using a Rigidbody, you could do something like:
if (Input.GetKeyDown(KeyCode.Space))
rigidbody.AddForce(transform.forward * small_force * Time.deltaTime);
If you are moving your car through Transform.translate(), use a speed variable. Each time they press the button, increase the speed variable just a bit.
if (Input.GetKeyDown(KeyCode.Space))
speed += small_value;
transform.Translate(transform.forward * speed * Time.deltaTime);
Thanks, it is working, but now I would like that the speed decrease s if you don't press the button, any ideas?
– Orloffyeah
Hi, thanks for the quick response, but how could I write it fot java? Float = Var?
– OrloffyeahYes, in javascript it's automatically chosen so float = var
– chrisall76