0 votes

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?


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

func _ready() -> void:
    _velocity.x = -speed.x
    $enemy1.flip_h = true

func _physics_process(delta) -> void:
   _velocity.y += gravity * delta

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

_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y
asked Jun 26 in Projects by Jorge (198 points)

1 Answer

0 votes

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:


and suggested:

if _velocity.x > 0:
   $enemy1.flip_h = false
elif _velocity.x < 0:
   $enemy1.flip_h = true
answered Jun 26 by Jorge (198 points)
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 How to use this Q&A? before posting your first questions.