Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Blink · Oct 05, 2012 at 09:07 PM · animationaudiosoundevent

Play sound on animation event?

Ive been trying to add audio to my game and have heard there's a way of adding sound to animation events, Im not to sure how to script this or how to set an event properly. I have started a script and I have my animations, If someone could point me in the right direction I would be greatfull.

 var footsteps : AudioClip;
 
 function Update () {
 
 if(AnimationEvent); //when the animation event happens in animation "gun walk" do something
 audio.clip = footsteps;
 audio.Play();
 
 }
Comment
Add comment
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

3 Replies

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

Answer by Griffo · Oct 06, 2012 at 04:21 PM

Try this.

 var footsteps : AudioClip;
 
 function footstepsEvent(){
 
     var footstepsEvent = new AnimationEvent();
     footstepsEvent.functionName = "footstepsSound";        
     footstepsEvent.time = 0.0;
 
     animation{"gun walk"].layer = 1;
     animation["gun walk"].clip.AddEvent(footstepsEvent); // Add the event to an AnimationClip
     animation["gun walk"].wrapMode = WrapMode.Once;
     animation.Play("gun walk");
 }
 
 function footstepsSound(){

     audio.volume = 1.5;
     audio.clip = footsteps;
     audio.Play();
 }
Comment
Add comment · Show 9 · 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 Blink · Oct 06, 2012 at 04:36 PM 0
Share

I get an error saying

The best overload for the method 'UnityEngine.AnimationClip.AddEvent(UnityEngine.AnimationEvent)' is not compatible with the argument list '(function(): void)'.

Can you help?

avatar image Griffo · Oct 06, 2012 at 04:45 PM 0
Share

Try it again there was a "s" missing in .. var footstepsEvent = new AnimationEvent();

avatar image Blink · Oct 06, 2012 at 04:48 PM 0
Share

Ive attached it to where my animations are but my sound doesn't play, How should I have set the animation events up?

avatar image Griffo · Oct 06, 2012 at 04:51 PM 0
Share

Have you added Component, Audio, Audio Source to you game object ?

avatar image Blink · Oct 06, 2012 at 04:52 PM 0
Share

yes i have

Show more comments
avatar image
0

Answer by Graham-Dunnett · Oct 05, 2012 at 09:14 PM

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

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 Blink · Oct 05, 2012 at 09:26 PM 0
Share

Ive looked at this and done it but It doesn't really help me scripting it, unless im just being dumb.

avatar image
0

Answer by Griffo · Oct 06, 2012 at 08:54 AM

This is an old one I used, you could alter it to your requirements .. hope it helps you out.

 var fireSoundEnd : AudioClip;    // Set end gun sound for the weapon
 
 // Create and set up the Reload AnimationEvent so the var reloadsound will play with the animation
 function reload1(){
 
     var reloadEvent = new AnimationEvent();
         reloadEvent.functionName = "reload2";            // Add the sound below - function reload2();
             reloadEvent.time = 0.0;
         
         animation["Reload"].layer = 1;                    // Place the Reload animation on a higher level so it will take priority and play
         animation["Reload"].clip.AddEvent(reloadEvent); // Add the event to an AnimationClip
 
         animation["Reload"].wrapMode = WrapMode.Once;    // Set the animation to play once
         animation.Play("Reload");                        // Play the animation
         }
 
 function reload2(){
 
     reloadGunSound();
 }
 
 function reloadGunSound(){
 
     audio.volume = 0.5;
     audio.loop = false;
     audio.clip = reloadSound;
     audio.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 Blink · Oct 06, 2012 at 03:42 PM 0
Share

Im not sure how to alter this script to what Im trying to do, to be honest I haven't a clue how to. $$anonymous$$y animations have a separate script that works, and Ive added an animation event to when ever I want the footstep sound to be played. Can you help me out with the scripting part, this is what I have so far that doesn't work.

  function footstepsEvent(){



 var footstepEvent = new AnimationEvent();

 footstepsEvent.functionName = "footstepsEvent";        

 footstepsEvent.time = 0.3;

 animation["gun walk"].clip.AddEvent(footstepsEvent); // Add the event to an AnimationClip



    

}

var footsteps : AudioClip;

function footstepsSound(){

 audio.clip = footsteps;

 audio.Play();

}

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

11 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

Related Questions

Play from a variety of sounds? 1 Answer

multiple Animation Events in an Animation 0 Answers

how do u add sound clips to an animation using animation events? 1 Answer

Can you fix my code? (Audio for doors) 2 Answers

Sprinting Audio Problem 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