Spawning Objects is too fast

Hi,

I’m making an action game with some friends and I’m used to java and its mechanics (but I’m still a student and learning most of it by myself).

We have a character that is an archer and who is shooting arrows. We want him to shoot arrows when we press a certain key. The problem is that in a split second it can spawn 4 of them. How to control them? I used deltaTime to move them accordingly to the frameRate but is there a way to control this with any input?

Thank you!

A good option is probably to change GetKey/GetButton (which triggers every frame while it’s being pressed) to GetKeyDown/GetButtonDown (which triggers only once, on the frame that it’s first pressed, and then not again until the button is released and pressed again).

You could also keep it as GetKey/GetButton but put in a delay by logging the Time.time (the current time) to a float class member, called for instance lastShot, whenever a shot is fired. Then, you’d check if the current Time.time is greater than lastShot + some delay like 2f (2 seconds) every frame before allowing to fire again. That way, you could hold the button down and continuously fire, but it wouldn’t fire them all at once.

This is likely to be a little over your head, but the way that I’d probably do it if I were writing this myself is to trigger a “FiringArrow” coroutine when the button is pressed, which would then handle the arrow firing and the wait timer all internally. While the coroutine is running, some bool like “isFiring” would be true, and you can only start the coroutine again when isFiring is false (which it’ll switch it to when it’s finishing up).

The GetKeyDown! Should have thought about it! I used it in Java! And I also thought of the Time.time thing but I was wondering if there was a better way of doing it.

Thanks!