Enemy range shooting

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

I’m trying to make the enemy keep shooting the projectiles when the player still in the area 2d collision shape, but they shoot only one time, and i need to enter again to they shoot the player, how do i make they keep shooting while the player stay in the area?

func _on_Range_body_entered(body):
if “player” in body.name and fireCooldown.is_stopped():
fireCooldown.start(cooldown)
fire()

:bust_in_silhouette: Reply From: timothybrentwood

body_entered only fires once per body that enters the area so you’ll need to set a boolean and use _physics_process() to cause it to shoot multiple times then use the body_exited signal to revert your boolean.

var player_in_range = false

func _physics_process():
    if not player_in_range:
        pass
    elif fireCooldown.is_stopped():
        fireCooldown.start(cooldown)
        fire()

func _on_Range_body_entered(body):
    if "player" in body.name:
        player_in_range = true

func _on_Range_body_exited(body):
    if "player" in body.name:
        player_in_range = false

Ty so much dude, you helped me a lot. It works

YPestis | 2021-08-07 19:52