Using waveform of a sound clip to animate objects in the scene
In this recipe, we will use a waveform of a sound effect to create motion based on the average volume of the fragment of the waveform.
Getting ready
To follow this recipe, we need a sound effect. You can use the provided example Unity project and go to the Chapter 07 Special effects\Recipe 09 Using waveform of a sound clip to animate objects in the scene
directory. If you open the Example.unity
scene there and play the game, you will be able to see a few cubes react to the playing audio clip.
How to do it...
To use sound waveforms for animation, follow these steps:
- Import the sound into Unity.
- Create an empty game object and name it
AudioSource
. - Add an Audio Source component to it, and make the sound
Loop
andPlay On Awake
. Drag and drop your sound into theAudioClip
field. - Create a
Cube
game object (go toGame Object
|3D Object
|Cube
). - Create a new script and call it
ScaleWithWaveForm.cs
. In this script, we have onepublic AudioSource...