35 lines
989 B
GDScript
35 lines
989 B
GDScript
class_name MovingPlatform extends AutoPathFollow
|
|
|
|
@export var body: AnimatableBody3D
|
|
@export var area: Area3D
|
|
|
|
@onready var _area_bodies: Array[Character] = []
|
|
|
|
func _ready() -> void:
|
|
if area:
|
|
area.body_entered.connect(_on_area_body_entered)
|
|
area.body_exited.connect(_on_area_body_exited)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
super(delta)
|
|
if body:
|
|
body.global_position = global_position
|
|
body.constant_linear_velocity = velocity
|
|
for ch in _area_bodies:
|
|
if ch.impetus.is_zero_approx() && ch.is_grounded():
|
|
ch.state = &'idle'
|
|
var veldiff := velocity - ch.linear_velocity
|
|
veldiff -= veldiff.project(global_basis.y)
|
|
ch.apply_central_impulse(ch.mass*veldiff)
|
|
|
|
func _on_area_body_entered(pbody: Node3D) -> void:
|
|
var ch := pbody as Character
|
|
if ch && _area_bodies.count(ch) <= 0:
|
|
_area_bodies.push_back(ch)
|
|
|
|
func _on_area_body_exited(pbody: Node3D) -> void:
|
|
var ch := pbody as Character
|
|
if ch:
|
|
while _area_bodies.count(ch) > 0:
|
|
_area_bodies.erase(ch)
|