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:
//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 *= 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”);*
* }*
* }*
}
_*[1]: http://i.imgur.com/Hv21Gjp.png*_