- Home /
Play Sound When Opening Door
Hi, I've got a brilliant idea but I don't know how to do this.
I'd like to play a sound when open/close a door (by using the DragRigidbody.js). Dragging already works. My door is openable in an angle from 0-90°. Now I'd like to play a sound which depends on the current angle. So the sound is also played in the right speed.
I'll need to devide the duration of the sound with 90 and only play the part that is currently needed.
Easiest example: The sound duration is 90 seconds. If the door is rotated from 0-15 the sound plays 0-15 seconds
I hope this is understandable. Unfortunatly I think this will be hardly possible but some scripting genius will be able to give me a suggestion.
Answer by Rydrako · May 20, 2013 at 12:25 AM
First of all you must add an Audio Source to your door object. And whenever you rotate the door or want the sound to play call this in your script:
audio.Play();
and when you want it to stop playing the sound:
audio.Stop();
So you can edit the DragRigidbody script maybe like this:
//New line after line 13
audio.Stop();
//Another new line after 64
audio.Play();
Well, my question was not understandable then :D
I only want to play a part of the sound. I found PlayScheduled to do this.. maybe I'm able to get what I want.
I try to play the sound realtime. If the door gets opened very slowly, the sound also plays in the same tempo. If its opened very fast, the sound also plays very fast
Answer by zagorskij · May 22, 2013 at 01:12 AM
ok your question is in fact definitely not easy.
PlayScheduled is a bti tricky, it was created more for sync reasons, to sync the start with an absolute time in that case with AudioSettings.dspTime. but you do not need this complication, you can use AudioSource.PlayDelayed , where the offset is specified in sec according to your clip.
About the tempo..well it's not easy task, to do it properly. Actually Unity does not let you to change ONLY tempo of a clip without changing the pitch, this is done for performance reasons. So you can work with AudioSource.pitch , it will change the tempo too, faster for values > 1, slower < 1, but it will change the pitch too, probably for a noise is not so dramatic, but try with voices :) if you don't like the result, you need to work with GetData and SetData, methods and playing chunks of the sound in sync with deltatime in your update methods, but this mean you need to manage pratically all the complexity of working with low level sound data
Your answer
Follow this Question
Related Questions
5.1 Channels is backwards in unity (non 3D) 0 Answers
Audio Settings Menu 2 Answers
play sound when moving stop sound when not 1 Answer
Making sounds wait for each other to finish 2 Answers
Unity wont play audio in editor, but will when the game is built. 2 Answers