Created Enemy to track player's last known location

This commit is contained in:
Willow Behar
2024-05-10 11:25:15 -05:00
parent cbb3760380
commit 300a90af9c
2 changed files with 49 additions and 0 deletions

26
Assets/scripts/Enemy.gd Normal file
View File

@ -0,0 +1,26 @@
extends CharacterBody3D
@onready var nav_agent = $NavigationAgent3D
@export var SPEED:float = 3.0
var prev_known_location = Vector3(0,0,0)
func _ready():
update_target_location(global_transform.origin)
func _process(delta):
if prev_known_location != GlobalSettings.Player_Last_Location:
prev_known_location = GlobalSettings.Player_Last_Location
update_target_location(GlobalSettings.Player_Last_Location)
func _physics_process(delta):
var current_location = global_transform.origin
var next_location = nav_agent.get_next_path_position()
var new_velocity = (next_location - current_location).normalized() * SPEED * delta
velocity = new_velocity
move_and_slide()
func update_target_location(target_location):
nav_agent.set_target_position(target_location)