- Home /
make visualizer smoother
I need some help making the visualizer on my game smoother. I have an example of what it's supposed to look like: https://www.youtube.com/watch?v=gRr9E9cVqHE
I tested this song in my game's visualizer, and it looks terrible and jittery (it's the jagged pink line in the background). I'm thinking I need to make the visualizer value to be interpolated from 5 frames or so. is there a quick way or doing that, or a better way to fix it?
Here's my current code (not originally mine):
using UnityEngine;
using System.Collections;
public class AudioVisualizer : MonoBehaviour
{
//The velocity that the cubes will drop
public Vector3 gravity = new Vector3(0.0f,0.25f,0.0f);
//An AudioSource object so the music can be played
private AudioSource aSource;
//A float array that stores the audio samples
public float[] samples = new float[64];
//A renderer that will draw a line at the screen
private LineRenderer lRenderer;
//A reference to the cube prefab
public GameObject cube;
//The transform attached to this game object
private Transform goTransform;
//The position of the current cube. Will also be the position of each point of the line.
private Vector3 cubePos;
//An array that stores the Transforms of all instantiated cubes
private Transform[] cubesTransform;
void Awake ()
{
//Get and store a reference to the following attached components:
//AudioSource
this.aSource = GetComponent<AudioSource>();
//LineRenderer
this.lRenderer = GetComponent<LineRenderer>();
//Transform
this.goTransform = GetComponent<Transform>();
}
void Start()
{
//The line should have the same number of points as the number of samples
lRenderer.SetVertexCount(samples.Length);
//The cubesTransform array should be initialized with the same length as the samples array
cubesTransform = new Transform[samples.Length];
//Center the audio visualization line at the X axis, according to the samples array length
//goTransform.position = new Vector3 ((-samples.Length / 2) * cube.transform.localScale.x, goTransform.position.y, goTransform.position.z);
//Create a temporary GameObject, that will serve as a reference to the most recent cloned cube
GameObject tempCube;
//For each sample
for(int i=0; i<samples.Length;i++)
{
//Instantiate a cube placing it at the right side of the previous one
tempCube = (GameObject) Instantiate(cube, new Vector3(goTransform.position.x + i, goTransform.position.y, goTransform.position.z),Quaternion.identity);
//Get the recently instantiated cube Transform component
cubesTransform[i] = tempCube.GetComponent<Transform>();
//Make the cube a child of this game object
cubesTransform[i].parent = goTransform;
}
}
void Update ()
{
if (PlayerPrefs.GetInt ("UseMusic") == 1) {
if (PlayerPrefs.GetInt ("UseVisualizerBackground") == 1) {
lRenderer.enabled = true;
}
//Obtain the samples from the frequency bands of the attached AudioSource
aSource.GetSpectrumData (this.samples, 0, FFTWindow.Hamming);
//For each sample
for (int i = 0; i < samples.Length; i++) {
/*Set the cubePos Vector3 to the same value as the position of the corresponding
* cube. However, set it's Y element according to the current sample.*/
cubePos.Set (cubesTransform [i].position.x, (Mathf.Sqrt (samples [i] * 20000)) * 3 + 2, cubesTransform [i].position.z);
//If the new cubePos.y is greater than the current cube position
cubesTransform [i].position = cubePos;
/*Set the position of each vertex of the line based on the cube position.
* Since this method only takes absolute World space positions, it has
* been subtracted by the current game object position.*/
if (lRenderer.enabled == true) {
lRenderer.SetPosition (i, cubePos);
}
}
} else {
if (lRenderer.enabled == true) {
lRenderer.enabled = false;
}
}
if (PlayerPrefs.GetInt("UseVisualizerBackground") == 1) {
lRenderer.enabled = true;
Debug.Log ("using visualizer");
}
}
}
Answer by SniperEvan · Oct 24, 2015 at 05:56 AM
Unity has built in functions Lerp and Slerp (spherical lerp, gives smoother end points). I would experiment with those first.
If that doesn't suit your needs I would try storing the last few values in an array and taking the average.
Let me know if that helps / you need more information.
How would I go about using the lerp and slerp functions?
@SniperEvan How can I use Lerp and Slerp in this situation?