How do you limit the number of projectiles or add a cooldown to shooting? Here's the shooting code.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sabir_The_Trubbish

func shoot():
var projectile = load(“res://Bullets/UnchargedProjectile.tscn”)
var bullet = projectile.instance()
bullet.position = get_global_position()

:bust_in_silhouette: Reply From: annemarietannengrund

im also not that far, but i watched a ton of videos the last days to get a feeling.

i remember this one from yesterday, he is showcasing and at one point talking about the projectile velocity.

this should give a an idea for a working approach i guess.

hope this helps in finding a solution :slight_smile:

Thanks, this helped :slight_smile:

Sabir_The_Trubbish | 2021-05-23 21:14

:bust_in_silhouette: Reply From: frosty

One way to achieve this is using a Timer, which you could add as a node in the editor, or via code.

  • Add a new timer node to your player, and set the wait time to however long you want it to be between shots – say, 0.1 seconds.
  • Also set the timer to “One Shot”
  • In your code, when you check whether the user is firing (presumably to call your shoot method), also check that the timer is stopped. This will prevent multiple shots being fired while the timer is running.
  • Finally, at the end of your shoot method, start the timer so that another shot can’t be fired straight away.

Your code may end up looking something like tihs:

func _process(delta):
    if Input.is_action_pressed("fire") and $CooldownTimer.is_stopped():

func shoot():
    # your existing code

I also recorded a video of this setup and posted it here, in case it’s useful!