Forum rules - please read before posting.

¿How I can play a sound when the animation play one specific frame?

Hi Gurus!!

I need play a sound in a specific frame of one character's animation... How I can make this?? I'm searching in the manual and the forum and I find no answer or solution.

Can anybody help me?

Thanks a lot!!

Comments

  • edited June 2016
    Not AC related question :D
    But here is the way
    Save this script:
    using UnityEngine;
    using System.Collections;

    public class SoundPlayEventAnim : MonoBehaviour {


        public AudioClip SoundToPlay;
        public AudioSource source;
       
       
        void Awake () {
           
            source = GetComponent<AudioSource>();
        }
       
        void playSound() {
           
            source.PlayOneShot(SoundToPlay);
        }
       
        void Update () {
           
           
           
           
           
        }

    Add the script to your object that has animation component.
    Add Audio Source component to that object too.
    Open your animation window and right click on a frame you want your audio start playing, add animation event, right click on it again and edit, from the drop down list find playSound() and select it.

    That's all, just don't forget to set what audio to play in the inspector window of your object in Sound Play Event Anim component in Sound To Play feild and Audio Source that you added to your object.
  • Thank you very much!!

    I'm testing but have not yet managed to make it work. But I have some more questions about this:

    1. Where I have to add the component of the script? In the "sprite child" character (this element has the "animation controller"), or in the animation file (file.anim)?

    2. Do I have to change something in the script? The name of the sound file is "tijeras.ogg" and the AudioSource object is "tijerassound". Do I have to include these names in the script?.

    3. The animation is part of the collection of animations of the main character. There are other character animations that need different sounds. For example, when the character hits an object.
    How I can manage all these sounds?

    Thank you very much again.
  • I have a problem with this ...

    I add a script component to "sprite child" of the main character and copy your script into it. They appear in the "inspector box" two tables to add a sound file and AudioSource. Then I add a component audioSource the same "sprite child". I indicated what the AudioSource next to the other sounds in the folder /sounds in the "Hierarchy box".

    But when I go to the box animation, and try to add a new property to the animation, the script's option don't appear, while the AudioSource option if it appears ...

    What am I doing wrong?

    Thank you

    (This answers the previous question who had to add the script and audiosource).
  • edited June 2016
    Nothing wrong, you just didn't specify which type of animation you were referring to... If I'm not mistaken the method explained above works for Mecanim animation; where you can add events to specific frames in a 3D animation, which in turn can call scripts; but as you are using 2D, you are just using the Animation Panel, where you have all your sprites laid out, I presume. I must be honest and say I work mostly with 3D, but I do have used the animation panel before, so, instead, why don't you see if you can just set the audio source to play automatically (in the audio source settings), make it disabled by default (remove the components check), and whenever your animation reaches the frame you want, just go ahead and "enable" the audio source within the animation panel? then at the last frame of the animation you can just disable it again? the idea could use some tweaking, but you could give it a try.

    PS: just make sure the audio source is in the sprite which holds the animation and then make sure to set the audio file in the audio source. Then depending on the kind of sound you want (you haven't told us...), then change between looping sound or one time sounds.  
  • edited June 2016
    Yes this is for Mecanim Animation. But it might work for other animations as long as you can have access to frame panel and call for the playSound function. Haven't tested for 3d but works fine for 2d.
    And since Alverik said the animation panel event works with 3d too I suppose it works fine for both.

    1.You just click on the script file and drag it onto the object that has animation component (the animation where you want your sound play on your specific frame) in you hierarchy.

    2.No need to change the script.

    3.This script is designed for one instance sound play per object. As you can see it has only 2 fields for the sound to play and the audio source. Also the void playSound () function that you define in your edit animation event drop list.
     You would need to add more void playSound () functions  just copy/paste and change names of  void playSound ()  and more public AudioClip SoubdToPlay to void playSound 02() and
    public AudioClip SoubdToPlay02. So you could be able to define other sounds and call respectivly correct playSounds to play in animation event drop-down list.

    4. That is correct you add audio source to the same object that has animation component and already your script. And in the script tables you select audio to play and in the source table you drag that audio Source you've just added to that table.

    5. It's regardless, once you added the script to your object that has animation component, you should see in the animation box, in the dropdown list of animation event playSound() function.
     You didn't read well, it doesn't add new property we are not modyfing the Unity Source API here :D
     In animation box. Just below the timescale numbers line there is grey space line right click on it and add animation event, than right click on the event again and edit it select from the droplist your playSound function.
    Might need to search how to Add animation event in unity's documentary.
  • edited June 2016
    Oh, so you're basically making an animation for that specific component? (which shows as the function?) Anyway, sorry if I spoke too soon,  I just don't have much experience working with 2D and haven't ever needed to use the animation panel to play sounds at specific frames. Anyways, if I'm ever in a similar situation I'll make sure to try it out.
  • edited June 2016
    The script derives the component you added in this case audiosource and calls for sound play. :)
     Also I just edited my last post. If it worked for you in 3d and for me in 2d then it works fine, as long as we can have access to the animation panel and call/trigger our functions be it sounds or anything we might incorporate with AC or anything else.
  • Yes, I use a similar script to play sounds for my doors and other objects. Although, for 3D, if you need animation events, AFAIK, you usually do it right in the animation file + mecanim editor, there's usually no need to use the Animation panel.
  • edited June 2016
    Hi guys!!

    Thank you very much to all people!! Finally I got it working!

    Now I have a question arising from this. I can do that an object (eg, a door), open or closed (ie, hide or show an scene object) when the animation reaches a certain frame?

    Thank you again!!
  • edited June 2016
    Well, I'm guessing you could do something similar to what you just did. Add a script in your character which runs/plays an animation from another game object, then call that script in during that character's animation. 

    Just declare the game object and then in a function call the animation.play of the object. The code would go something like this:

    using UnityEngine;

    public GameObject MyObject;

    void PlayMyAnim()
    {
    MyObject.animation.Play("MyAnimation");
    }

    Just remember to check the animation properties (in the inspector), to make sure it's not looping, unless you want the animation to play again and again...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.