Add scripts to make the character follow a path

This commit is contained in:
Matthew Welch 2024-09-27 13:39:45 -07:00
parent 22c4bd2ecf
commit c30454e7b8
4 changed files with 137 additions and 34 deletions

View File

@ -1,29 +1,51 @@
extends CharacterBody3D @tool
extends Area3D
const SPEED = 5.0 @export var path: Path3D:
const JUMP_VELOCITY = 4.5 set = set_path
@export var path: PathFollow3D; @export var speed: float = 5.0
var follow: PathFollow3D
var marker: Marker3D
var total_distance: float = 0.0
func _ready() -> void:
if path == null or path is not Path3D:
printerr("path is not set to a Path3D")
return
follow = path.get_node("Follow")
marker = follow.get_node("Marker")
func _physics_process(delta): func _process(delta):
# Add the gravity. if path == null:
#if not is_on_floor(): return
#velocity += get_gravity() * delta global_position = marker.global_position
# if follow.progress_ratio >= 1:
## Handle jump. return
#if Input.is_action_just_pressed("ui_accept") and is_on_floor(): var travel = speed * delta
#velocity.y = JUMP_VELOCITY follow.progress += travel
total_distance += travel
#if is_equal_approx(follow.progress, change_distance):
#path = $Path3D2
#follow = path.get_node(("Follow"))
#change_distance = path.curve.get_baked_length()
#marker.reparent(follow)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide() func set_path(new_path: Path3D):
if new_path is not Path3D:
printerr("path must be a Path3D")
return
path = new_path
follow = path.get_node("Follow")
marker = follow.get_node("Marker")
update_configuration_warnings()
func _get_configuration_warnings() -> PackedStringArray:
var warnings = []
if path == null or path is not Path3D:
warnings.append("path must be set to a Path3D.")
return warnings

View File

@ -1,19 +1,22 @@
[gd_scene load_steps=4 format=3 uid="uid://vamte52rvfhy"] [gd_scene load_steps=4 format=3 uid="uid://vamte52rvfhy"]
[ext_resource type="Script" path="res://character/character.gd" id="1_xk8jv"] [ext_resource type="Script" path="res://character/character.gd" id="1_evtr0"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_gnhao"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_gnhao"]
[sub_resource type="CapsuleMesh" id="CapsuleMesh_br0ui"] [sub_resource type="CapsuleMesh" id="CapsuleMesh_br0ui"]
[node name="CharacterBody3D" type="CharacterBody3D"] [node name="Character" type="Area3D"]
script = ExtResource("1_xk8jv") script = ExtResource("1_evtr0")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("CapsuleShape3D_gnhao") shape = SubResource("CapsuleShape3D_gnhao")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
mesh = SubResource("CapsuleMesh_br0ui") mesh = SubResource("CapsuleMesh_br0ui")
skeleton = NodePath("")
[node name="Camera3D" type="Camera3D" parent="."] [node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, -4.52534e-08, 1.68888e-07, -1.77636e-15, 0.965926, 0.258819, -1.74846e-07, -0.258819, 0.965926, 0, 1, 2.55814) transform = Transform3D(1, -4.52535e-08, 1.68888e-07, -1.77636e-15, 0.965926, 0.258819, -1.74846e-07, -0.258819, 0.965926, 0, 2, 2.558)

32
world.gd Normal file
View File

@ -0,0 +1,32 @@
extends Node3D
@export var speed: float = 10
@onready var path_3d_2: Path3D = $Path3D2
@onready var path: Path3D = $Path
@onready var follow: PathFollow3D = $Path/Follow
@onready var marker: Marker3D = $Path/Follow/Marker
var points_added = false
var point_removed = false
var change_distance: float
var total_distance: float = 0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
change_distance = path.curve.get_baked_length()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
%Character.global_position = marker.global_position
if follow.progress_ratio >= 1:
return
var travel = speed * delta
follow.progress += travel
total_distance += travel
if is_equal_approx(follow.progress, change_distance):
path = $Path3D2
follow = path.get_node(("Follow"))
change_distance = path.curve.get_baked_length()
marker.reparent(follow)

View File

@ -1,33 +1,79 @@
[gd_scene load_steps=4 format=3 uid="uid://blupjt2ulo2g1"] [gd_scene load_steps=9 format=3 uid="uid://blupjt2ulo2g1"]
[ext_resource type="PackedScene" uid="uid://vamte52rvfhy" path="res://character/character.tscn" id="1_nfwx6"] [ext_resource type="PackedScene" uid="uid://vamte52rvfhy" path="res://character/character.tscn" id="1_nfwx6"]
[ext_resource type="Script" path="res://world.gd" id="1_oloil"]
[sub_resource type="PlaneMesh" id="PlaneMesh_okds2"] [sub_resource type="PlaneMesh" id="PlaneMesh_okds2"]
size = Vector2(10, 10) size = Vector2(10, 10)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gqvdt"]
cull_mode = 2
albedo_color = Color(1, 0, 0, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2ipub"]
cull_mode = 2
albedo_color = Color(0, 1, 0, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_k12sw"]
cull_mode = 2
albedo_color = Color(0, 0, 1, 1)
[sub_resource type="Curve3D" id="Curve3D_5ttj7"] [sub_resource type="Curve3D" id="Curve3D_5ttj7"]
_data = { _data = {
"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0.425, 4.6, 0, 0, 0, 0, 0, 0, 0, 0.47, -4.745, 0, 0, 0, 0, 0, 0, 0, 2.68, -11.185), "points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0.425, 4.6, 0, 0, 0, 0, 0, 0, 0, 0.47, -4.745, 0, 0, 0, 0, 0, 0, 0, 2.84942, -14.0262),
"tilts": PackedFloat32Array(0, 0, 0)
}
point_count = 3
[sub_resource type="Curve3D" id="Curve3D_3mog2"]
_data = {
"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 2.8487, -14.2054, 0, 0, 0, 0, 0, 0, 0, 2.81399, -24.5989, 0, 0, 0, 0, 0, 0, -0.0390603, 2.79813, -35.8309),
"tilts": PackedFloat32Array(0, 0, 0) "tilts": PackedFloat32Array(0, 0, 0)
} }
point_count = 3 point_count = 3
[node name="World" type="Node3D"] [node name="World" type="Node3D"]
script = ExtResource("1_oloil")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("PlaneMesh_okds2") mesh = SubResource("PlaneMesh_okds2")
[node name="MeshInstance3D2" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.978896, -0.204359, 0, 0.204359, 0.978896, 0, 0.696645, -10.0891)
material_override = SubResource("StandardMaterial3D_gqvdt")
mesh = SubResource("PlaneMesh_okds2")
[node name="MeshInstance3D3" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5446, -20.8633)
material_override = SubResource("StandardMaterial3D_2ipub")
mesh = SubResource("PlaneMesh_okds2")
[node name="MeshInstance3D4" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5446, -31.7451)
material_override = SubResource("StandardMaterial3D_k12sw")
mesh = SubResource("PlaneMesh_okds2")
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-0.827728, -0.0513662, 0.558773, 0.136568, 0.947417, 0.289396, -0.544256, 0.315852, -0.777189, 3.45982, 2.03445, -6.78341) transform = Transform3D(-0.827728, -0.0513662, 0.558773, 0.136568, 0.947417, 0.289396, -0.544256, 0.315852, -0.777189, 3.45982, 2.03445, -6.78341)
[node name="Path3D" type="Path3D" parent="."] [node name="Path" type="Path3D" parent="."]
curve = SubResource("Curve3D_5ttj7") curve = SubResource("Curve3D_5ttj7")
[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] [node name="Follow" type="PathFollow3D" parent="Path"]
transform = Transform3D(1, 0, 0, 0, 0.999875, 0, 0, 0, 0.999875, 0, 0.445395, 0.364689) transform = Transform3D(1, 0, 0, 0, 0.999858, 0, 0, 0, 0.999858, 0, 0.425, 4.6)
progress = 4.23536
rotation_mode = 0 rotation_mode = 0
loop = false loop = false
tilt_enabled = false tilt_enabled = false
[node name="CharacterBody3D" parent="." instance=ExtResource("1_nfwx6")] [node name="Marker" type="Marker3D" parent="Path/Follow"]
[node name="Path3D2" type="Path3D" parent="."]
curve = SubResource("Curve3D_3mog2")
[node name="Follow" type="PathFollow3D" parent="Path3D2"]
transform = Transform3D(1, 0, 0, 0, 0.999858, 0, 0, 0, 0.999858, 0, 2.8487, -14.2054)
rotation_mode = 0
loop = false
tilt_enabled = false
[node name="Character" parent="." instance=ExtResource("1_nfwx6")]