Attention | Topic was automatically imported from the old Question2Answer platform. | |
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