-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathbeetle_moving_platforms.gd
More file actions
74 lines (55 loc) · 2.12 KB
/
beetle_moving_platforms.gd
File metadata and controls
74 lines (55 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extends CharacterBody3D
signal platform_reached
const SPEED := 3.0
var is_on_platform := false
var target_index := 0:
set(value):
target_index = wrapi(value, 0, target_global_positions.size())
start()
var target_global_positions := []:
set(value):
target_global_positions = value
if target_global_positions.is_empty():
return
start()
var remote_transform: RemoteTransform3D = null
@onready var beetle_skin: Node3D = %BeetlebotSkin
@onready var navigation_agent: NavigationAgent3D = %NavigationAgent3D
func setup(remote_transform: RemoteTransform3D) -> void:
self.remote_transform = remote_transform
func _ready() -> void:
navigation_agent.velocity_computed.connect(move)
navigation_agent.navigation_finished.connect(stop)
navigation_agent.max_speed = SPEED
set_physics_process(false)
func _physics_process(delta: float) -> void:
var next_location := navigation_agent.get_next_path_position()
var direction := (next_location - global_position).normalized()
var new_velocity := direction * SPEED
navigation_agent.velocity = new_velocity
if is_on_platform:
stop()
func move(safe_velocity: Vector3) -> void:
velocity = safe_velocity
var current_model_transform := beetle_skin.global_transform
beetle_skin.look_at(global_position + velocity, Vector3.UP, true)
beetle_skin.global_transform = current_model_transform.interpolate_with(beetle_skin.global_transform, 10.0 * get_physics_process_delta_time())
move_and_slide()
func start() -> void:
is_on_platform = false
set_physics_process(true)
navigation_agent.avoidance_enabled = true
remote_transform.remote_path = ^""
navigation_agent.target_position = target_global_positions[target_index]
beetle_skin.walk()
func stop() -> void:
beetle_skin.idle()
set_physics_process(false)
navigation_agent.avoidance_enabled = false
if global_position.distance_to(navigation_agent.target_position) < navigation_agent.path_desired_distance:
target_index += 1
elif is_on_platform:
remote_transform.global_position = global_position
remote_transform.global_rotation = global_rotation
remote_transform.remote_path = remote_transform.get_path_to(self)
platform_reached.emit()