UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Music visualization"
using UnityEngine; using System.Collections; [RequireComponent(typeof(AudioSource))] public class MusicVisualizer : MonoBehaviour { public GameObject prefab; public int numberOfObjects = 64; public float radius = 5f; public float scale = 1f; private GameObject[] objects; private AudioSource audioSource; private float[] samples = new float[512]; // Start is called before the first frame update void Start() { audioSource = GetComponent<AudioSource>(); objects = new GameObject[numberOfObjects]; for (int i = 0; i < numberOfObjects; i++) { float angle = i * 360f / numberOfObjects; Vector3 pos = Quaternion.Euler(0, angle, 0) * Vector3.forward * radius; objects[i] = Instantiate(prefab, pos, Quaternion.identity); objects[i].transform.SetParent(transform); } } // Update is called once per frame void Update() { audioSource.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris); for (int i = 0; i < numberOfObjects; i++) { float sampleValue = samples[i] * scale; Vector3 newScale = new Vector3(1, sampleValue, 1); objects[i].transform.localScale = newScale; } } }