Hello!
I'm newer to godot, so I can't for the life of me figure out how to flip an enemy so its always facing the player.
Here's the script:
extends KinematicBody2D
export(int) var speed = 25
var velocity: Vector2 = Vector2.ZERO
var path: Array = [] # Contains destination positions
var levelNavigation: Navigation2D = null
var player = null
var playerSpotted: bool = false
onready var los = $LineOfSight
func _ready():
yield(get_tree(), "idle_frame")
var tree = get_tree()
if tree.has_group("LevelNavigation"):
levelNavigation = tree.get_nodes_in_group("LevelNavigation")[0]
if tree.has_group("Player"):
player = tree.get_nodes_in_group("Player")[0]
func _physics_process(_delta):
if player:
los.look_at(player.global_position)
check_player_in_detection()
if playerSpotted:
generate_path()
navigate()
move()
func check_player_in_detection() -> bool:
var collider = los.get_collider()
if collider and collider.is_in_group("Player"):
playerSpotted = true
return true
else:
return false
func navigate(): # Define the next position to go to
if path.size() > 0:
velocity = global_position.direction_to(path[1]) * speed
# If reached the destination, remove this point from path array
if global_position == path[0]:
path.pop_front()
func generate_path(): # It's obvious
if levelNavigation != null and player != null:
path = levelNavigation.get_simple_path(global_position, player.global_position, false)
func move():
velocity = move_and_slide(velocity)
$AnimationPlayer.play("Chase")
Thanks for the help!