using UnityEngine; using UnityEngine.AI; public class MovementAudio : MonoBehaviour //plays movement audio and adjusts the volume based on speed { public AudioSource source; private float speed; private Rigidbody parentBody; private NavMeshAgent agent; public bool nonPlayer; private void Awake() { if (!nonPlayer) parentBody = transform.parent.gameObject.GetComponent<Rigidbody>(); else agent = transform.parent.gameObject.GetComponent<NavMeshAgent>(); } private void Update() { float sourceVolume; if (!nonPlayer) { speed = parentBody.velocity.magnitude; sourceVolume = Mathf.InverseLerp(0, 600, speed); } else { speed = agent.velocity.magnitude; sourceVolume = Mathf.InverseLerp(0, 600, speed); } if (speed > 1) { source.volume = sourceVolume; if (source.isPlaying == false) source.Play(); } } }