Issue with flip_h after enemy collides with the wall (2D)

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

I am using Godot 3.2 for a 2D project, and running a script for an enemy to change the direction (flip_h) when is colliding with the walls, however, when the enemy collides with the wall or another enemy the enemy change also the direction.

The code I am using is working only when collides for the first time (ONCE), after that isn’t flipping the direction any more, any ideas?

thanks

extends "res://Actors/Actors.gd"

func _ready() -> void:
    set_physics_process(false)
    _velocity.x = -speed.x
    $enemy1.flip_h = true
	
func _physics_process(delta) -> void:
   _velocity.y += gravity * delta
   $enemy1.play("walk")

   if is_on_wall():
	_velocity.x *= -1.0
	$enemy1.flip_h = false

_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y
:bust_in_silhouette: Reply From: Jorge

Someone pointed to my answer to use :

$enemy1.flip_h = !$enemy1.flip_h 

because I was calling the “false” all the time.

However, he gives me another solution to be out of:

is_on_wall()

and suggested:

if _velocity.x > 0:
   $enemy1.flip_h = false
elif _velocity.x < 0:
   $enemy1.flip_h = true