- Home /
Animation play audio?
I'm kinda new to animating. I know how to move the object around, and add keyframes, but is there a way to add an audio play at a certain keyframe in the animation? Also is there a way to add a particle effect?
Answer by Ben-Stoneman · Aug 06, 2012 at 08:44 PM
On the object you are animating:
Add a audio.Play(); script.
Add an Audio Source and enable Play on Awake.
Attach your Audio Clip to the Audio source.
Disable the Script component.
Open the Animation window (Ctrl+6) and find the new Script component.
Click on the grey dot to the right of Enabled, and Add Curves.
Now go to the key frame you want the sound to play on and change enabled to 1
Right click on enabled and select Both Tangents > Constant
You should end up with a line like this:
Test It!
I used this technique for a reloading animation for my game, but the sound only plays the first time the person reloads, and all the other times it's silent. Is there any way to fix this?
Try setting the enabled back to 0 just after the sound is played, this should reset the script to inactive, allowing you to reactivate when the animation plays again.
Let me know how you get on.
Thanks for your support, I tried setting enabled back to 0 but the audio will still only play once. Any suggestions?
Thanks
I realize this is an old question, but if anyone's having the same issue, try function OnEnable() ins$$anonymous$$d of function Start() in the audio.Play script.
Answer by henriettejenssen · Oct 10, 2013 at 12:09 PM
I have the same problem. I do not try to play a sound, only to write a message to the console, but the solution should be the same. I chose to make an Animation Event instead, create a function (that in your case would play a sound) and call the function from the Animation Event. Here is the code I am using (Javascript):
function TellConsole(){ // creates a function.
print("Works!"); // paste the audio playing code instead.
}
TellConsole(); // calls the function every time the Animation Event gets triggered.
Add the script to the object with the animation
Open the animation window
make an "animation Event" by right clicking near the top of the timeline
4 A window will pop up called "Edit Animation Event" with a list of functions to choose from.
Your desired function should be found in this list. Every time the animation plays, the desired function will run. In your case, the audio.Play function that I have been too lazy to write down here ;-)
In my opinion, this is the best, most flexible solution.
You should add public method to see your method in the list of functions
This answer is the simplest and most robust. You can use this to call any manner of function within an animation. As a bonus the events on your animation will be labeled with your function name which makes them very clear.
Answer by blueLED · Nov 13, 2014 at 01:49 AM
If you don't want to write an animation event to play every sound, there's another way to do this right in the animation timeline window.
First make sure the audio source is disabled (click the checkbox beside "Audio Source" to toggle it off in the inspector). And also turn on "Play on Awake".
Now, in your timeline you can simply click "Add Curve" and select enabled for the AudioSource and make keyframes where you can toggle the sound on and then off again at the end.
Answer by TheGreatS · Nov 04, 2015 at 11:05 AM
This is an old question but it still comes up in google search and found a pretty good solution. This works if you want an animation object to be able to play multiple sounds, and play them over each other.
Add script to object with animation and audio source and include these things in the designated places
public float soundToPlay = -1.0f; //this with designate which sound to play. -1 means don't play any sound. This is a float because then the animation window can access it. public AudioClip[] audioClip; //this holds the sounds
AudioSource audio;//for holding the audio source
void Start () { audio = GetComponent ();//put this in start. This gets the audio source. }
void Update () { //put the following in update if (soundToPlay > -1.0f) {//if the sound is greater than the value for not playing a sound PlaySound((int) soundToPlay, 1);//play the sound, casting the float to an int so that the audio source can use it soundToPlay = -1.0f;//set it back to zero to keep this from looping back around and playing the sound again. } }
void PlaySound(int clip, float volumeScale){ audio.PlayOneShot (audioClip [clip], volumeScale);//play the sound with the designated clip and volume scale }
Click the object with the script inside it and navigate to the script in the inspector. You should see the Sound To Play and Audio Clip properties. Set the audio clip array size and then drag and drop all the audio clips you want to play into their respective boxes.
Go to the animation window.
Find the animation you want to add a sound to, or create a new one.
Click the Add Property button, expand the script and add the Sound To Play property
Position three keyframes one frame apart to set the value of Sound To Play. Make the ones on the edge -1 and the middle one the designated value in the Audio Clip array for the sound you want to play. Be careful when applying the sound to the very first frame of an animation, I have had problems with the sound glitching out when there isn't a buffer frame to set the value to -1.
Go into the animation curves and by right clicking set the two end keyframes to audio, and the middle keyframe to broken. Drag the two smoothing handles all the way down.
And now your done.
Answer by eppz · Jan 13, 2017 at 01:28 AM
Just drag the sound to a GameObject, and Enable it when you want the sound to play.
$$anonymous$$ake sure to leave "Play on Awake" turned on (as it is by default).
Your answer
![](https://koobas.hobune.stream/wayback/20220613075253im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clothing and weapon animation 1 Answer
More Animations = More Screwed Up? 0 Answers
Partilcle effect help needed 2 Answers