34 lines
955 B
GDScript
34 lines
955 B
GDScript
extends CharacterBody3D
|
|
|
|
@export var min_speed = 10
|
|
@export var max_speed = 18
|
|
|
|
signal squashed
|
|
|
|
func _physics_process(delta):
|
|
move_and_slide()
|
|
|
|
func initialize(start_position, player_position):
|
|
# Look to the player
|
|
look_at_horizontal(start_position, player_position)
|
|
# But rotate a little bit
|
|
rotate_y(randf_range(-PI / 4, PI / 4))
|
|
# Set random speed
|
|
var random_speed = randi_range(min_speed, max_speed)
|
|
# Apply the velocity
|
|
velocity = Vector3.FORWARD * random_speed
|
|
velocity = velocity.rotated(Vector3.UP, rotation.y)
|
|
|
|
$AnimationPlayer.speed_scale = random_speed / min_speed
|
|
|
|
func look_at_horizontal(start_position: Vector3, target_position: Vector3):
|
|
var target_horizontal_position = Vector3(target_position.x, start_position.y, target_position.z)
|
|
look_at_from_position(start_position, target_horizontal_position, Vector3.UP)
|
|
|
|
func squash():
|
|
squashed.emit()
|
|
queue_free()
|
|
|
|
func _on_visibility_notifier_screen_exited():
|
|
queue_free()
|