UnityGameProjectsCode / AbandondedGame / DoorSqueak.cs
DoorSqueak.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorSqueak : MonoBehaviour
{
    private AudioSource aSource;
    private Rigidbody body;
    private float rbMagnitude;
    public float movementThreshold;

    private void Awake()
    {
        aSource = GetComponent<AudioSource>();
        body = GetComponent<Rigidbody>();
        aSource.pitch = Random.Range(0.5f, 1.5f);
    }

    private void Update()
    {
        rbMagnitude = Mathf.InverseLerp(0.0f, 1.0f, body.velocity.magnitude);

        if (rbMagnitude >= movementThreshold)
        {
            aSource.volume = rbMagnitude / 5;
        }
        else
            aSource.volume = 0.0f;
    }
}