How enemy face player?

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

How the enemy face the player when enemy chasing/attacking player?
X axis only

extends KinematicBody2D

const GRAVITY = 10
const SPEED = 30
const FLOOR = Vector2(0, -1)

var velocity = Vector2()
var player = null
var direction = 1

func _physics_process(delta):
velocity.x = SPEED * direction

if player:
	velocity = position.direction_to(player.position) * SPEED

$AnimationPlayer.play("walk")
velocity.y += GRAVITY

velocity = move_and_slide(velocity, FLOOR)

if is_on_wall():
	direction = direction * -1
	$DownRay.position.x *= -1
	
if $DownRay.is_colliding() == false:
	direction = direction * -1
	$DownRay.position.x *= -1

	

func _on_Area2D_body_entered(body):
player = body

func _on_Area2D_body_exited(body):
player = null

:bust_in_silhouette: Reply From: alexp

You probably want to use the Sprite’s flip_h, e.g.

onready var my_sprite = $Sprite

func _process(delta):
  my_sprite.set_flip_h(velocity.x < 0)