- Home /
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();
}
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();
}
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?
Try it again there was a "s" missing in .. var footstepsEvent = new AnimationEvent();
Ive attached it to where my animations are but my sound doesn't play, How should I have set the animation events up?
Have you added Component, Audio, Audio Source to you game object ?
Answer by Graham-Dunnett · Oct 05, 2012 at 09:14 PM
Ive looked at this and done it but It doesn't really help me scripting it, unless im just being dumb.
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();
}
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
Follow this Question
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