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
in Engine by (197 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
by (197 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.