3D场景中物体的自动寻路

场景搭建

上述BASE节点下分别为相机、灯光、与地板
在地板中添加材质, 设置路径Mesh属性 -> Material
[gd_resource type="StandardMaterial3D" load_steps=2 format=2]
[ext_resource path="res://.../grid.png" type="Texture2D" id=1]
[resource]
albedo_texture = ExtResource( 1 )
uv1_scale = Vector3( 10, 10, 1 )
然后创建Path3D
以及PathFollow3D
以及角色模型(一个简单的柱体和球体)
3D角色

材质编写
分别在Body、Head中加入材质,设置Geometry属性,创建tres文件, 这里将物体控制为红色
[gd_resource type="StandardMaterial3D" format=2]
[resource]
albedo_color = Color( 1, 0, 0, 1 )
运动控制
在Path3D
的属性面板中,新建Curve3D,然后在主窗口上方的菜单栏选择路径节点创建按钮,绘制运动路径

最后在character上创建运动脚本,根据父节点中的运动路径创建一个位置列表,然后定时的将人物移动到该节点位置
extends Node
@export var waypoint_check_distance: float = 0.1
@export var total_loop_time: float = 5.0
var _path_follow: PathFollow3D
var _waypoint_timer: Timer
var _waypoint_positions: PackedVector3Array
var _current_path_time: float
var _current_waypoint_index: int
var _next_waypoint_index: int
var _moving: bool
func _ready() -> void:
_path_follow = get_parent() as PathFollow3D
_waypoint_timer = get_node("WaypointTimer") as Timer
var path_curve: Curve3D = _path_follow.get_parent().curve
var n_waypoints: int = path_curve.get_point_count()
_waypoint_positions = PackedVector3Array()
for i in range(n_waypoints - 1):
_waypoint_positions.append(path_curve.get_point_position(i))
_current_waypoint_index = 0
_next_waypoint_index = 1
_moving = true
func _process(delta: float) -> void:
if _moving:
_current_path_time += delta
_path_follow.progress_ratio = _current_path_time / total_loop_time
var d = (_waypoint_positions[_next_waypoint_index] - _path_follow.position).length()
if d < waypoint_check_distance:
_current_waypoint_index = _next_waypoint_index
_next_waypoint_index = (_current_waypoint_index + 1) % _waypoint_positions.size()
_waypoint_timer.start()
_moving = false
func _on_waypoint_timer_timeout() -> void:
_moving = true