Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Cbjfan1 · Aug 06, 2012 at 06:08 PM · animating

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Aug 06, 2012 at 06:11 PM 1
Share

Look into animationevents.

avatar image Ingen · Aug 06, 2012 at 07:18 PM 2
Share

http://docs.unity3d.com/Documentation/Components/animeditor-AnimationEvents.html

5 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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

alt text

  • Right click on enabled and select Both Tangents > Constant

  • You should end up with a line like this:

alt text

  • Test It!


c.png (2.0 kB)
e.png (6.8 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cbjfan1 · Oct 01, 2012 at 11:34 PM 1
Share

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?

avatar image Ben-Stoneman ♦♦ · Oct 23, 2012 at 12:47 PM 0
Share

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.

avatar image sushi7 · May 19, 2013 at 06:10 PM 0
Share

Thanks for your support, I tried setting enabled back to 0 but the audio will still only play once. Any suggestions?

Thanks

avatar image apostro · May 21, 2015 at 12:33 PM 0
Share

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.

avatar image
8

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.


  1. Add the script to the object with the animation

  2. Open the animation window

  3. make an "animation Event" by right clicking near the top of the timeline alt text

4 A window will pop up called "Edit Animation Event" with a list of functions to choose from. alt text

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 ;-)


screen shot 2013-10-10 at 13.27.21.png (31.1 kB)
screen shot 2013-10-10 at 13.30.05.png (16.7 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rendermouse · Jul 14, 2016 at 03:20 PM 1
Share

In my opinion, this is the best, most flexible solution.

avatar image Me_hungry · Sep 23, 2016 at 09:16 AM 1
Share

You should add public method to see your method in the list of functions

avatar image rawansadek · Apr 28, 2018 at 01:23 PM 0
Share

Can someone show me how to add this script?

avatar image Ammon_Satterfield · Feb 27, 2019 at 03:07 PM 0
Share

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. Here my function in my script was `PlayStartSound()`

animation-event-label.png (6.4 kB)
avatar image umiata · Jan 05, 2020 at 05:56 AM 0
Share

+1. Best answer that worked for me.

avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

  1. 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 }

  2. 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.

alt text

  1. Go to the animation window.

  2. Find the animation you want to add a sound to, or create a new one.

  3. Click the Add Property button, expand the script and add the Sound To Play property

  4. 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.

  5. 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.

alt text

And now your done.


script-sound-to-play-and-audio-clip-array.png (19.6 kB)
sound-animation-curves.png (2.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eppz · Jan 13, 2017 at 01:29 AM 0
Share

$$anonymous$$ake sure to leave "Play on Awake" turned on (as it is by default).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a 2D fighter 1 Answer

Idle Animation that does not change position of object 0 Answers

Does Anyone Know What Animation System Unity3d Technologies Uses? 1 Answer

Animating an absolute position? 1 Answer

Animation Update for the TPS controller 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges