• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SoBiT · May 20, 2013 at 12:02 AM · audiosounddragrigidbody

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoBiT · May 20, 2013 at 12:29 AM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw a waveform on a mobile texture 1 Answer

Audio 'multiplying' when using audio.PlayOneShot() 2 Answers

How can I use Unity's Doppler effect with high velocity objects? 1 Answer

Checking collision between 2 triggers based on distance. Possible? 1 Answer

Audio signal plug-in? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges