- Home /
How do I add sound to a sliding door animation?
I could not find an answer to this anywhere.
I have an animation done in Unity's animation editor of two doors that slide open when I get close to them using raycast, how can I get my doors to play a sound effect when they are openning?
I don't know if you need to see it but this is my door code:
enum DoorStates {open, closed}; var doorState : DoorStates;
function Awake() { doorState = DoorStates.closed; }
function Update () { }
function Open() { animation.Play("PwrStationDoorsOpen"); doorState = DoorStates.open;
}
Answer by FLASHDENMARK · Mar 04, 2011 at 09:24 PM
enum DoorStates {open, closed}; var doorState : DoorStates; var doorSound : AudioClip;
function Awake() { doorState = DoorStates.closed; }
function Update () { }
function Open() { animation.Play("PwrStationDoorsOpen"); audio.PlayOneShot(doorSound); doorState = DoorStates.open;
}
The only thing you really have to do is to add a variable(var) called something like:
var doorSound : AudioClip;
//And when you open the door:
audio.PlayOneShot(doorSound);
I understand some of this but what would I put in the "Something happend" I need and example ;)
I tried Answer 2, it works but my sound seems to play when the door animation is finnishing for some reason.?
O$$anonymous$$ Problem solved, what I did was find an mechanical door sound that starts sooner, the other sound I was using had a slight delay at the beginning. $$anonymous$$y other option which I think would have worked also would have been to change the start time of my door animantion. In any event it's working peachy keeno now lol
Answer by efge · Mar 04, 2011 at 09:24 PM
You could use AudioSource.PlayOneShot inside your function open(), but when you want to avoid overlapping sounds you should use AudioSource.isPlaying.
Edit:
When you call PlayOneShot and animation.Play at the same time (like in OrangeLightning's answer) they should "play" together. If you want more control or an exact start time you could use Animation Events.
Take a look at the reference: Using Animation Events
I tried this but the sound seems to play when the door is open but I need it to play as the door is opening as they are sliding doors, I hope I explained that right ?
It sounds to me your sound clip have some silence in the beginning (no pun intended).
That could be the the case you know, I was listenning to it closly and it does seem to start a little slow, I began searching for someway to see if I can start of the sound playing a bit sooner but I could not find anything in the reference guide on that.
I was also toying with the idea of having a box collider triger the sound sooner but that might seem kind of odd if the player walked through the box accidentally not intending to go through the door.
Baring all of that I might just have my sound placed wrong??
Your answer
Follow this Question
Related Questions
FPSController Collision with moving objects 2 Answers
Object gone aftar play - Animation problem. 0 Answers
Animate Doors and Button Through Script 1 Answer
Door animation after press a key 2 Answers
Animation clip does not exist? 1 Answer