44 lines
1.4 KiB
GDScript
44 lines
1.4 KiB
GDScript
extends CharacterBody3D
|
|
|
|
var movement_speed: float = 3.0
|
|
|
|
var target: CharacterBody3D
|
|
var movement_target_position: Vector3
|
|
|
|
var skip: bool = false
|
|
|
|
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
|
|
|
|
func _ready() -> void:
|
|
first_frame.call_deferred()
|
|
|
|
func first_frame() -> void:
|
|
actor_setup(999999999)
|
|
|
|
# determines the closest player and navigate.
|
|
func actor_setup(lowest_distance: float) -> void:
|
|
if !skip:
|
|
for child: Node in get_tree().root.get_node("/root/Testmap").get_children():
|
|
if child is Node3D:
|
|
if child.has_method("playerstub") and position.distance_to((child as Node3D).position) < lowest_distance:
|
|
target = child
|
|
skip = true
|
|
|
|
var dist: float = target.position.distance_to(movement_target_position)
|
|
if movement_target_position != target.position and (dist > 5 or position.distance_to(target.position) <= 5 or position.distance_to(movement_target_position) <= 5):
|
|
movement_target_position = target.position
|
|
navigation_agent.set_target_position(movement_target_position)
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
actor_setup(position.distance_to(target.position))
|
|
|
|
if navigation_agent.is_navigation_finished():
|
|
return
|
|
|
|
var current_agent_position: Vector3 = global_position
|
|
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
|
|
|
|
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
|
|
|
|
if move_and_slide():
|
|
pass
|