x


Play sound on animation event

This is related to the 'footstep sounds when walking' question which has helped me a lot until now. I'm trying to make it so that on an animation event a function is called which plays a sound effect (be it a footstep, a drip sound, and so on). I would also like to trigger something like a GUITexture which will show an animation of the event (such as ripples expanding) but I haven't got that far yet.

I know that to edit the animation on an imported model I will have to duplicate the animation - i'll look into that later, although Rune's post here seems to have everything I need - so at the moment i'm testing it on a simple Unity sphere 'dripping' from a shower head.

On the frame where the sphere reaches the ground I have an animation event that calls this function:

var audioVolume = 1.0;
var collisionSoundEffect : AudioClip;

function playSound(){
    Debug.Log("Animation event called");
    audio.volume = audioVolume;
    audio.clip = collisionSoundEffect;
    audio.Play();

    // Code to show GUI animation
    otherscript = GameObject.Find("dripGUI").GetComponent("guiAnim");
    otherscript.dripPlaying = true;
}

@script RequireComponent(AudioSource)

It works but I only hear the sound when I am facing the sphere, despite having the sound set to '3D sound'. It becomes difficult to hear the sound at all when I get close to the sphere as it drops past the camera too quickly. Also the sound itself is very quiet. Changing the volume doesn't seem to do anything, although the Rolloff factor works.

EDIT - It's not just that I can't hear the sound; the animation of the sphere, and the related GUITexture animation from my other question, don't play either unless the sphere is in view of my camera.

I had a similar problem earlier when I used the 2D Gameplay Tutorial method (OnTriggerEnter). It worked fine on the PC, but the sound was very quiet on NPCs (I know the audio source on the PC would be closer to the camera's audio listener but it was still REALLY quiet on NPCs).

I intend to have a few of these audio sources which play a sound, (maybe trigger an in-game animation), and trigger a GUI animation. I want to use animation events because not all of them are triggered by a collision, and it would be good to have one script for all that allows me to set a sound effect, in-game animation (if need), and a gui animation to play.

If anyone can offer any suggestions, i'd really appreciate it.

Thank you for your time.

more ▼

asked Apr 02 '10 at 01:31 PM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

Something bizarre going on here. I just discovered that my sound was a stereo sound (I had used a placeholder mono sound earlier so must've overlooked this one). However forcing mono, or even using the other mono sound makes no difference, both still only play when i'm looking in that direction. But another game object that plays a mono sound on collision plays spatialized sound (the sound raises and lowers as I turn away and approach etc). http://Confused.com

Apr 02 '10 at 08:56 PM straydogstrut
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Doh! moment. I had 'Animate Only If Visible' ticked.

Sorry for wasting everyone's time.

more ▼

answered Apr 02 '10 at 09:10 PM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1060
x176
x46

asked: Apr 02 '10 at 01:31 PM

Seen: 8362 times

Last Updated: Apr 02 '10 at 08:44 PM