How to smoothly scale with audio spectrum data

Hi, I’m new to programming and currently trying to write up a simple audio visualiser where I just grab some spectrum data and scale the cubes with that. The problem is while it’s doing this it’s very jittery and I want it to scale smoothly. I’ve been looking around and found Lerp but I’m not sure how to use it with my code could someone point me in the right direction?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class audioSpecDazzle : MonoBehaviour {

	public GameObject cube01;
	public GameObject cube02;
	public GameObject cube03;
	public GameObject cube04;
	public GameObject cube05;
	public GameObject cube06;
	public GameObject cube07;
	public GameObject cube08;
	public GameObject cube09;
	public GameObject cube10;
	public Vector3 initialPosition;
	public float juice = 150f;
	public float[] spec;
	public float specMag01;
	public float specMag02;
	public float specMag03;
	public float specMag04;
	public float specMag05;
	public float specMag06;
	public float specMag07;
	public float specMag08;
	public float specMag09;
	public float specMag10;
	// Use this for initialization
	void Start () 
		Application.targetFrameRate = 999;
	// Update is called once per frame
	void Update () 
		spec = AudioListener.GetSpectrumData(1024,0,FFTWindow.BlackmanHarris); // this works on audio source
		//spec = AudioListener.GetOutputData (64,0);  // this gives much  better values.
		specMag01 = spec[2] + spec[4] + spec[8];
		specMag02 = spec [12] + spec [16] + spec [24];
		specMag03 = spec[32] + spec[36] + spec[40];
		specMag04 = spec[44] + spec[48] + spec[50];
		specMag05 = spec[52] + spec[54] + spec[56];
		specMag06 = spec[58] + spec[60] + spec[62];
		specMag07 = spec[70] + spec[72];
		specMag08 = spec [74] + spec [76];
		specMag09 = spec [78] + spec [80];
		specMag10 = spec [82] + spec [84];

		cube01.gameObject.transform.localScale = new Vector3(1f,specMag01*juice,1f);
		cube02.gameObject.transform.localScale = new Vector3(1f,specMag02*juice,1f);
		cube03.gameObject.transform.localScale = new Vector3(1f,specMag03*juice,1f);
		cube04.gameObject.transform.localScale = new Vector3(1f,specMag04*juice,1f);
		cube05.gameObject.transform.localScale = new Vector3(1f,specMag05*juice,1f);
		cube06.gameObject.transform.localScale = new Vector3(1f,specMag06*juice,1f);
		cube07.gameObject.transform.localScale = new Vector3(1f,specMag07*juice,1f);
		cube08.gameObject.transform.localScale = new Vector3(1f,specMag08*juice,1f);
		cube09.gameObject.transform.localScale = new Vector3(1f,specMag09*juice,1f);
		cube10.gameObject.transform.localScale = new Vector3(1f,specMag10*juice,1f);


Linear Interpolation, Lerp(A, B, C) just takes values A and B and returns you a value that’s C percent between them. If C==0 it returns A, if C==1 it returns B. There’s no magic in it so it’s hard to say if it suits your purpose.

You would do something like

cube01.transform.localScale = Vector3.Lerp(cube01.transform.localScale, new Vector3(1f,specMag01*juice,1f), 0.5f);

In simple terms that just makes the bar height change half of what it’s supposed to every frame, making the transitions seemingly smoother but also “less responsive”.

Sometimes putting Time.deltaTime * someMagicConstant as the 3rd parameter gives you a nice effect in Lerp, but it’s not the “right way” to use Lerp since then the effect will greatly depend on your framerate etc.

If this doesn’t give you the desired results, then I’d say it’s a design problem, not a programming problem. Lerp is better suited for situations where you have 2 values and you want to smoothly get from A to B over a longer time. In your case the values change every frame so there is not really time to do any Lerping.