0 votes

For some reason after i shoot a couple times, the audio keeps looping. Also sometimes the bullets spawn before they should. Anyone know why?

func _physics_process(delta):
        if Input.is_action_pressed("Shoot") and can_fire:
            fire()
 func fire():
        SoundPlayer.play_sound(SoundPlayer.shoot)
        var bullet_instance = bullet.instance()
        bullet_instance.position = $Position2D.get_global_position()
        bullet_instance.rotation_degrees = rotation_degrees
        bullet_instance.apply_impulse(Vector2(), Vector2(bullet_speed, 0).rotated(rotation))
        get_tree().get_root().add_child(bullet_instance)
        can_fire = false
        yield(get_tree().create_timer(fire_rate), "timeout")
        can_fire = true
Godot version 3.5.1.stable
in Engine by (20 points)

Are you saying that bullets are sometimes being spawned before the player presses fire? I fire() being called elsewhere?

No, while im fireing the bullet sometimes shoots too quick, and the sound it makes will repeat forever even after shooting one bullet

Do you change can_fire elsewhere ?

By the way you can change your code to remove the yield.

yield(get_tree().create_timer(fire_rate), "timeout")

Create a timer named cooldown with oneshot set to true and just start it from your fire function with your fire_rate parameters and check for timer end to allow shooting again.

if Input.is_action_pressed("Shoot") and $cooldown.time_left == 0:

Maybe it will solve your problem

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.