- Home /
This question was
closed Mar 08, 2015 at 11:48 PM by
KnightRiderGuy for the following reason:
Found a solution
Question by
KnightRiderGuy · Feb 26, 2015 at 06:31 PM ·
audiosourcelinerenderereffectsvisualization
Line Renderer Music Visualizer Not Working
OK This is probably something simple that I am missing but I am just not seeing please help if you can. My audio clip plays fine when activated but my line renderer audio visualizer only seems to work if I assign a sound clip directly to the audio source, it seems to be ignoring my audio clips in my array??? Please help :) Here is my entire script. I shave also attached a screen capture to also help with understanding :)
using UnityEngine;
using System.Collections;
public class VBtestAudioVisualizer : MonoBehaviour
{
//Audio Clip Selections
public AudioClip VoiceClip01;
public AudioClip VoiceClip02;
public AudioClip VoiceClip03;
public AudioClip VoiceClip04;
//An AudioSource object so the music can be played
public 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
public LineRenderer lRenderer;
//A reference to the cube prefab
public GameObject cube;
//The transform attached to this game object
public 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;
//The velocity that the cubes will drop
private Vector3 gravity = new Vector3(0.0f,5.0f,0.0f);
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,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-1;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 ()
{
//Obtain the samples from the frequency bands of the attached AudioSource
aSource.GetSpectrumData(this.samples,0,FFTWindow.BlackmanHarris);
// For each sample
for(int i=0; i<samples.Length-1;i++)
{
cubePos.Set(cubesTransform[i].position.x, Mathf.Clamp(samples[i]*(i*i),0,50), cubesTransform[i].position.z);
//If the new cubePos.y is greater than the current cube position
if(cubePos.y >= cubesTransform[i].position.y)
{
//Set the cube to the new Y position
cubesTransform[i].position = cubePos;
}
else
{
//The spectrum line is below the cube, make it fall
// cubesTransform[i].position -= gravity;
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.*/
lRenderer.SetPosition(i, cubePos - goTransform.position);
}
}
//Audio Play Buttons
public void GoVoiceClip01(){
audio.PlayOneShot(VoiceClip01);
}
}
screen-shot-2015-02-26-at-122650-pm.png
(186.6 kB)
Comment
I would have thought there was a simple answer to this but so far no luck?!?
Follow this Question
Related Questions
disable / enable mixer audio effect in C#? 1 Answer
does unity 5 pitch shifter audio effect change playback speed? 1 Answer
Audio Distortion effects? 0 Answers
multi Coloured Audio Waveform? 1 Answer