Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 LukaKotar · Oct 16, 2013 at 02:52 AM · c#audiotime

Door opening sound, according to opening speed/direction?

Hi.

I wanted to implement a dynamic door opening sound. I want the door's Y rotation to work like the time bar in a music player. You can move the point somewhere on that bar, and move back/forward on the clip's time.

But there are two main issues:

  • if the clip is for example 6 seconds long, and I have a door rotate from 0 to 90 degrees, it would be hard to determine at which point of these 6 seconds the audio should play.

  • Correct me if I'm wrong, but as far as I know, Unity does not have a way to slow down an audio clip in real time, or to play it backwards anyway.

Basically, I want the door to make a sound that is being played at the speed of the door being opened, backward or forward, depending on which direction the door is being rotated. I have some code that allows me to open the door using mouse movement, so I cannot just play a clip normally, as the door is not always opened at the same speed, or the player might stop moving the mouse at any time he/she likes.

Could this, or something similar be achieved in Unity?

Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Oct 16, 2013 at 05:21 AM

Unfortunately, Unity's sound engine doesn't allow such level of control. On the other hand, a creaking sound may be reasonably synthesized in Unity from individual creaks: each time the door rotates some small angle (2 degrees, for instance), play a creak sound randomly selected from an array - you may even modify the pitch according to the speed.

The script below was used to test this "creaking synthesizer": it rotates the object to which it's attached when the left button is pressed and the mouse is moved horizontally. You must attach it to a door and assign 3 to 5 creak sounds to the array creak in the Inspector (download a zip file with 5 sounds here).

 using UnityEngine;
 using System.Collections;
 
 public class DoorSound : MonoBehaviour {
     
     public AudioClip[] creak;
     public float creakAng = 2; // creak each creakAng
     public float speed = 20; // mouse sensitivity
     public float pitchControl = 0.1f; // control pitch sensitivity

     private float angle = 0;
     private float lastAngle = 0;
     private float lastCreak = 0;
 
     void Update () {
         if (Input.GetMouseButton(0)){ // if left button pressed...
             // control door angle with mouse movement
             angle += Input.GetAxis("Mouse X") * speed;
             // if moved more than creakAng...
             if (Mathf.Abs(angle - lastAngle) > creakAng){
                 lastAngle = angle; // update lastAngle
                 float deltaT = Time.time - lastCreak; // calc time from last creak
                 lastCreak = Time.time;
                 // increase pitch somewhat according to speed:
                 audio.pitch = Mathf.Clamp((0.5f+pitchControl)/(0.5f+deltaT), 0.9f, 1.5f);
                 // play a randomly selected creak sound:
                 audio.PlayOneShot(creak[Random.Range(0, creak.Length)]);
             }
             // set door angle about Y:
             transform.eulerAngles = new Vector3(0, angle, 0);
         }
     }



creaksounds.zip (3.9 kB)
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 kbhumber · Jan 04 at 10:32 PM 0
Share

For some reason, Im not able to download the .zip file. When I click on the link, I get taken to a blank page. Would you be able to reupload the .zip file? Thanks so much! Am curious to try this.

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.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Fade in AudioLister over time #c 1 Answer

Distribute terrain in zones 3 Answers

TimeScale with AudioSource 1 Answer

[SOLVED] Issue with music and Don't Destroy on load 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges