UnityContrib / framework

Unity Contribution Framework
MIT License
8 stars 2 forks source link

Audio volume control based on NavMeshPath #2

Open robintheilade opened 9 years ago

robintheilade commented 9 years ago

Make this code generic, optimized and commented.

using System;
using System.Collections;
using UnityEngine;

namespace Game
{
    public class AudioNavigationDistance : MonoBehaviour
    {
        private Transform player;
        private AudioSource[] audioSources;
        private NavMeshAgent navMeshAgent;
        public float maxAudioableDistance = 100.0f;
        public bool isDebuggingEnabled;

        private IEnumerator Start()
        {
            var audioSettings = GameObject.FindObjectOfType<AudioSettings>();

            while (Application.isPlaying)
            {
                var player = GameObject.FindGameObjectWithTag("Player");
                if (player != null)
                {
                    this.player = player.transform;
                    break;
                }

                yield return null;
            }

            this.audioSources = this.GetComponents<AudioSource>();
            this.navMeshAgent = this.GetComponent<NavMeshAgent>();

            while (Application.isPlaying)
            {
                var path = new NavMeshPath();
                if (!this.navMeshAgent.CalculatePathSafe(this.player.position, path))
                {
                    yield return null;
                    continue;
                }

                var start = this.transform.position;
                var end = this.player.position;
                end.y = start.y;
                var distance = path.Distance(start, end);
                var volume = Mathf.Lerp(audioSettings.sfxVolume, 0.0f, distance / this.maxAudioableDistance);
                var priority = (int)Mathf.Lerp(100.0f, 0.0f, distance / this.maxAudioableDistance);
                Array.ForEach(this.audioSources, a =>
                {
                    a.volume = volume;
                    a.priority = priority;
                    a.enabled = volume != 0.0f;
                });
                if (this.isDebuggingEnabled)
                {
                    Debug.Log(
                        "distance=" + distance + Environment.NewLine +
                        "volume=" + volume + Environment.NewLine +
                        "priority=" + priority + Environment.NewLine +
                        "start=" + start + Environment.NewLine +
                        "end=" + end + Environment.NewLine +
                        "path=" + path.ToDetailedString(start, end) + Environment.NewLine
                        );
                    if(distance > 100.0f)
                    {
                        path.DrawPath(start, end, Color.magenta, 5.0f);
                        Debug.Break();
                    }
                }
                yield return new WaitForSeconds(Mathf.Clamp(distance - this.maxAudioableDistance, 0.1f, 10.0f));
            }
        }
    }
}
robintheilade commented 9 years ago

Dependent on issue https://github.com/UnityContrib/framework/issues/1