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!

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 ([16583-creaksounds.zip|16583]).

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