Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | SuperMatCat24 |
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
Are you saying that bullets are sometimes being spawned before the player presses fire? I fire() being called elsewhere?
SteveSmith | 2023-01-28 09:42
No, while im fireing the bullet sometimes shoots too quick, and the sound it makes will repeat forever even after shooting one bullet
SuperMatCat24 | 2023-01-28 12:10
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
zeludtnecniv | 2023-01-31 12:01