Get colliding bodies every frame

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

I’m having trouble with a an enemy trying to follow the player only on the x-axis. The problem is that i have set up collisons on a platform so that the enemy doesnt fall off the edge and n collison changes direction and movement to stimulate a “patroling” effect. When i call the Area2d signal “body_entered” the enemy changes its direction to the player, but when it hits the wall it changes its direction again and stops following the player as he is still in the area2d range, so the body_entered signal isnt called again.
Here’s my enemy code:

func _physics_process(delta):
if direction == "left" :
	velocity.x = -SPEED*delta
elif direction == "right":	
	velocity.x = SPEED*delta
velocity.y += GRAVITY*delta
if is_on_floor():
	velocity.y = 0;
velocity = move_and_slide(velocity, FLOOR)

func ChangeDirection():
if direction == "right":
	#get_node( "Sprite" ).set_flip_h( false )
	sprite.set_scale(scaling)
	direction = "right"
elif direction == "left":
	#get_node( "Spr1ite" ).set_flip_h( true )
	sprite.set_scale(Vector2(scaling.x*-1,scaling.y))
	direction = "left"



#Damage Calculation
func take_damage(damage):
HEALTH -= damage
print(HEALTH)

#Reverse direction and movement
func _on_LeftReverse_body_entered(body):
ChangeDirection()
direction = “right”
pass

func _on_RightReverse_body_entered(body):
ChangeDirection()
direction = "left"
pass


func _on_DetectPlayer_body_entered(body):

if body.is_in_group("Player"):
	if player.get_position().x < get_position().x:
		direction = "right"
	elif player.get_position().x > get_position().x:
		direction = "left"
:bust_in_silhouette: Reply From: UnRealCloud 1

Hi, with your code a quick solution would be to set a bool variable like

func _on_DetectPlayer_body_entered(body):
chasing = true
if body.is_in_group("Player"):
    if player.get_position().x < get_position().x:
        direction = "right"
    elif player.get_position().x > get_position().x:
        direction = "left"


func _on_DetectPlayer_body_exited(body):
chasing = false

and place

 if chasing  == true:
 return

at the begining of your function detecting collision with wall. So you avoid your object to change dierction when hitting a wall