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.

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();

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 :slight_smile:
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