45 lines
1.1 KiB
GDScript
45 lines
1.1 KiB
GDScript
extends NavigationAgent3D
|
|
class_name EnemyMoverFollow
|
|
|
|
@export var follow_distance : float = 1
|
|
@export var target : Node3D
|
|
@export var MAX_SPEED := 2.0
|
|
|
|
var parent : CharacterBody3D
|
|
var disabled := false
|
|
|
|
func disable():
|
|
disabled = true
|
|
|
|
func _ready():
|
|
parent = get_parent()
|
|
|
|
func set_target(new_target: Node3D):
|
|
target = new_target
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
if is_instance_valid(target) && target && !disabled:
|
|
set_target_position(target.global_position)
|
|
var next_path_position = get_next_path_position()
|
|
#next_path_position.y = global_position.y
|
|
var dir = parent.global_position.direction_to(next_path_position)
|
|
var new_velocity = dir*MAX_SPEED
|
|
parent.look_at(next_path_position)
|
|
parent.velocity = new_velocity
|
|
parent.move_and_slide()
|
|
|
|
#set_velocity(new_velocity)
|
|
|
|
#if global_position.distance_to(target.global_position) < 2:
|
|
#queue_free()
|
|
|
|
else:
|
|
parent.velocity = lerp(parent.velocity, Vector3.ZERO, 1)
|
|
parent.move_and_slide()
|
|
|
|
|
|
func _on_navigation_agent_3d_velocity_computed(safe_velocity: Vector3) -> void:
|
|
parent.velocity = parent.velocity.move_toward(safe_velocity, .25)
|
|
parent.move_and_slide()
|