Files
music-spinner/cow.gd
2026-02-10 06:09:33 +01:00

30 lines
964 B
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
extends Node3D
@export var base_spin_speed := 0.5 # Constant slow spin speed
@export var beat_boost := 2.0 # How much faster it spins on the beat
@export var smoothness := 0.1 # Lower = smoother response
@export var music_bus := "MusicBus" # Bus name where analyzer is added
@onready var analyzer = AudioServer.get_bus_effect_instance(
AudioServer.get_bus_index(music_bus), 0
)
var smoothed_bass := 0.0
func _process(delta: float) -> void:
if not analyzer:
return
# Analyze low frequencies (20200Hz) for beat detection
var bass = analyzer.get_magnitude_for_frequency_range(20, 200)
var bass_strength = bass.length() * 10.0
# Smooth the value to avoid jitter
smoothed_bass = lerp(smoothed_bass, bass_strength, smoothness)
# Spin speed: base + a small boost from the beat
var spin_speed = base_spin_speed + smoothed_bass * beat_boost
# Apply rotation (e.g. around Y-axis)
rotation.y += spin_speed * delta