How to do a thing if user does something with a certain time limit?

87 Views Asked by At

I'm trying to make a 3D game but right now just jumping, I have got the jumping working, but I want to do a thing that (for example if speed=10) but if space is pressed 2x within (for example 0.5 seconds) the speed will go up by (for example 2) and if the player doesn't jump 2x times within the 0.5 seconds the speed stays/resets to normal.

So to simplify character speed becomes faster if you press space in certain time and if you don't for it stays 10, for example if I press space/jump and after 1 second do the same, the speed won't go up, but if I press space/jump within seconds <= 0.5 seconds the speed will go up, until I don't press it within the time, so if I stop pressing it within seconds <= 0.5 seconds the jump resets to 10 and if I jump --> 0.6 seconds =< seconds the speed stays the same.

And to people who don't know ursina, its a python based game engine and you modify the player using this type ----> player=FirstPersonController(gravity = (1), speed=10)

(Not sure but I think the engine is fully based on Python. Just so you know.

The engine I'm learning in is "Ursina Engine"

1

There are 1 best solutions below

2
On

Sounds like you need to save the timestamp whenever the space is pressed. Then, every time it is pressed, before updating the timestamp, compare now against the previous timestamp... if it is less than 500ms, then speed up!

Reacting to the user doing nothing is harder. I have not used ursula, and a quick look at the documentation, I do not see much in the Ursina Engine to help you.

But I have an idea you could try? When the character is sped up, create an invisible wall to hit. Set the wall up exactly where the character will be in 500ms time, if the user does nothing, they will "collide" with the wall and it will trigger your routine to reduce the speed. If the user does do something, remove the wall. Just an idea. Not sure if that will work. Enjoy!