UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Music visualization linerenderer"
using UnityEngine; using System.Collections; [RequireComponent(typeof(AudioSource))] public class MusicVisualizer : MonoBehaviour { public int numberOfSamples = 1024; public float lineWidth = 0.1f; public float radius = 5f; public float sensitivity = 100f; private LineRenderer lineRenderer; private AudioSource audioSource; private float[] samples; void Start() { lineRenderer = GetComponent<LineRenderer>(); audioSource = GetComponent<AudioSource>(); samples = new float[numberOfSamples]; lineRenderer.positionCount = numberOfSamples; lineRenderer.startWidth = lineWidth; lineRenderer.endWidth = lineWidth; } void Update() { audioSource.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris); for (int i = 0; i < numberOfSamples; i++) { float angle = i * 360f / numberOfSamples; float sampleHeight = samples[i] * sensitivity; Vector3 position = Quaternion.Euler(0, angle, 0) * new Vector3(radius + sampleHeight, 0, 0); lineRenderer.SetPosition(i, position); } } }