Created Enemy to track player's last known location
This commit is contained in:
26
Assets/scripts/Enemy.gd
Normal file
26
Assets/scripts/Enemy.gd
Normal 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)
|
||||
Reference in New Issue
Block a user