extends CharacterBody3D ##玩家 class_name Player @onready var people: Node3D = $people @onready var state_machine: StateMachine = $StateMachine ##导航 @onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D var speed:float = 4 var stop_distance:float = 1 signal ArriveTargetPos ##设置目标位(寻路方式运动) func setTargetPos(targetPos:Vector3,stopDistance = 1)-> void: stop_distance = stopDistance navigation_agent_3d.target_position = targetPos navigation_agent_3d.target_position =navigation_agent_3d.get_final_position() ##去目标位 func goToTarget()-> void: $NavigationTimer.start() ##停止移动 func stopToTarget()-> void: $NavigationTimer.stop() state_machine.change_state("Idle") func _on_navigation_timer_timeout() -> void: var direction_3d = (navigation_agent_3d.get_next_path_position() - global_position).normalized() velocity = direction_3d*speed look_at_from_position(global_position, navigation_agent_3d.get_next_path_position()) if navigation_agent_3d.distance_to_target() > stop_distance : move_and_slide() state_machine.change_state("Walk") else : stopToTarget() ArriveTargetPos.emit() ##区域碰撞检测相关 signal AreaEnter(area: Area3D) signal AreaExit(area: Area3D) func _on_area_3d_area_entered(area: Area3D) -> void: AreaEnter.emit(area) func _on_area_3d_area_exited(area: Area3D) -> void: AreaExit.emit(area)