到目前为止,我有一个立方体,Y刻度对正在播放的音乐作出反应,但它的运动是非常零星的,一秒钟它将非常高,而下一个非常低。我怎样才能使它只取最高值并按比例缩放,然后慢慢向下移动到初始比例,除非调用高于其当前比例的另一个值。
public class _FFT_Obstacles : MonoBehaviour {
public GameObject cube01;
public GameObject cube02;
public GameObject cube03;
public GameObject cube04;
public GameObject cube05;
private float juice = 40f;
private float speed = 5f;
public float[] spec;
public float specMag01;
public float specMag02;
public float specMag03;
public float specMag04;
public float specMag05;
private Vector3 localScale;
private Vector3 newScale;
//The velocity that the cubes will drop
private Vector3 gravity = new Vector3(0.0f, 0.25f, 0.0f);
// Use this for initialization
void Start()
{
localScale = cube01.gameObject.transform.localScale;
}
// Update is called once per frame
void Update()
{
spec = AudioListener.GetSpectrumData(64,0,FFTWindow.Hamming); // this works on audio source
// spec = AudioListener.GetOutputData(64, 0); // this gives much better values.
specMag01 = spec[2] + spec[4];
specMag02 = spec[12] + spec[14];
specMag03 = spec[22] + spec[24];
specMag04 = spec[32] + spec[34];
specMag05 = spec[57] + spec[60];
lerpScale();
cube01.gameObject.transform.localScale = new Vector3(1f, 1f + (specMag01 * juice), 1f);
//cube02.gameObject.transform.localScale = new Vector3(1f, 1f + (specMag01 * juice), 1f);
//cube03.gameObject.transform.localScale = new Vector3(1f, 1f + (specMag01 * juice), 1f);
//cube04.gameObject.transform.localScale = new Vector3(1f, 1f + (specMag01 * juice), 1f);
//cube05.gameObject.transform.localScale = new Vector3(1f, specMag05 * juice, 1f);
}
void lerpScale()
{
newScale = cube01.gameObject.transform.localScale = new Vector3(1f, 1f + (specMag01 * juice), 1f);
// cube01.gameObject.transform.localScale = Vector3.Lerp(originalScale, newScale, Time.deltaTime * speed);
// newScale.Set(1f, cube01.gameObject.transform.localScale.y, 1f);
if (newScale.y >= localScale.y)
{
localScale.y = newScale.y;
}
else
{
localScale.y -= gravity.y;
}
}
}
答案 0 :(得分:1)
希望这有帮助。
public class _FFT_Obstacles : MonoBehaviour
{
public FFTWindow window = FFTWindow.Hamming;
public GameObject cube01;
public GameObject cube02;
public GameObject cube03;
public GameObject cube04;
public GameObject cube05;
public float falloff = 3f;
public float juice = 40f;
public float[] spec;
public float specMag01;
public float specMag02;
public float specMag03;
public float specMag04;
public float specMag05;
private void Update()
{
spec = AudioListener.GetSpectrumData(64, 0, window); // this works on audio source
// spec = AudioListener.GetOutputData(64, 0); // this gives much better values.
specMag01 = spec[2] + spec[4];
specMag02 = spec[12] + spec[14];
specMag03 = spec[22] + spec[24];
specMag04 = spec[32] + spec[34];
specMag05 = spec[57] + spec[60];
UpdateCubeScale(cube01, specMag01);
UpdateCubeScale(cube02, specMag02);
UpdateCubeScale(cube03, specMag03);
UpdateCubeScale(cube04, specMag04);
UpdateCubeScale(cube05, specMag05);
}
private void UpdateCubeScale(GameObject cube, float specMag)
{
var currentScale = cube.transform.localScale;
var newScale = new Vector3(1f, 1f + (specMag * juice), 1f);
if (newScale.y < currentScale.y)
{
newScale.y = currentScale.y - falloff * Time.deltaTime;
}
if (newScale.y < 0f)
{
newScale.y = 0f;
}
cube.transform.localScale = newScale;
}
}