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: [Trap] - Pegboard Nerds x MisterWives - Coffins [Monstercat FREE Release] - YouTube

I tested this song in my game’s visualizer, and [it looks terrible and jittery][1] (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:  
		this.aSource = GetComponent<AudioSource>();  
		this.lRenderer = GetComponent<LineRenderer>();  
		this.goTransform = GetComponent<Transform>();  

	void Start()  
		//The line should have the same number of points as the number of samples  
		//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 *= tempCube.GetComponent<Transform>();* 
  •  	//Make the cube a child of this game object* 

_ cubesTransform*.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 .position.x, (Mathf.Sqrt (samples * 20000)) * 3 + 2, cubesTransform .position.z);

* //If the new cubePos.y is greater than the current cube position*

_ cubesTransform .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”);*

* }*

* }*


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.