Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Camera), typeof(AudioSource))]
- public class AudioVisualizer : MonoBehaviour
- {
- /* ... */
- void Update()
- {
- /* ... */
- // Find minimum and maximum value.
- // Use a second array of floats to smooth the animation.
- int sampleLen = samples.Length;
- float minSample = 0.0f;
- float maxSample = 0.0f;
- for(int i = 0; i < sampleLen; ++i)
- {
- // SmoothStep is a variant easing function; could also use Lerp.
- smoothed[i] = Mathf.SmoothStep(smoothed[i],
- samples[i],
- smoothingFactor);
- minSample = Mathf.Min(minSample, smoothed[i]);
- maxSample = Mathf.Max(maxSample, smoothed[i]);
- }
- int barLen = bars.Length;
- float toPercent = 1.0f / (barLen - 1);
- for(int i = 0; i < barLen; ++i)
- {
- // Convert progress through bars array to samples array index.
- float iPercent = i * toPercent;
- int sampleIndex = (int)(iPercent * (sampleLen - 1));
- // Get sample, convert to bar height.
- float sample = smoothed[sampleIndex];
- float barHeight = P5.Map(sample,
- minSample, maxSample,
- barMinHeight, barMaxHeight);
- Vector3 scale = originalBarScale;
- scale.y = barHeight;
- // Get transform, set properties.
- GameObject bar = bars[i];
- Transform tr = bar.GetComponent<Transform>();
- tr.localScale = scale;
- }
- }
- }
Add Comment
Please, Sign In to add comment