28 lines
778 B
GDScript

class_name RotationComponent
extends CharacterComponent
@export var root: Node3D
@export var rotation_speed: float = 15.0
var direction: Vector3
var angle: float
func _ready():
assert(root, "rotation root needs to be set")
assert(rotation_speed > 0, "rotation speed must be greater than zero")
character.controlled.connect(on_controlled)
func _process(delta):
if absf(angle_difference(root.rotation.y, angle)) < 0.001:
return
root.rotation.y = Math.damp_angle(root.rotation.y, angle, rotation_speed * delta)
func on_controlled(controller: Controller):
controller.direction_changed.connect(on_direction_changed)
func on_direction_changed(new_direction: Vector3):
direction = new_direction
if direction != Vector3.ZERO:
angle = atan2(direction.x, direction.z)