Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class EcholocationBeeper : MonoBehaviour
- {
- [SerializeField] private float speed = 10f;
- [SerializeField] private float minDistance = 5f;
- [SerializeField] private AudioClip sound;
- [SerializeField] private LayerMask wallLayer;
- [SerializeField] private AudioClip[] sounds;
- private Vector3[] directions = new[]
- {
- Vector3.forward,
- Vector3.back,
- Vector3.left,
- Vector3.right
- };
- private Dictionary<Vector3, float> cooldowns = new();
- private Dictionary<Vector3, AudioSource> sources = new();
- private Dictionary<Vector3, IEnumerator> coroutines = new();
- private void Awake()
- {
- Transform holder = new GameObject("Echolocation Audio Sources").transform;
- for (int i = 0; i < directions.Length; i++)
- {
- GameObject go = new($"Echo [{directions[i]}]");
- var audio = go.AddComponent<AudioSource>();
- audio.clip = sounds[i];
- audio.spatialBlend = 1f;
- audio.panStereo = directions[i].x;
- audio.transform.parent = holder;
- sources[directions[i]] = audio;
- cooldowns[directions[i]] = 0f;
- coroutines[directions[i]] = null;
- }
- }
- private void Update()
- {
- void Raycast(Vector3 direction)
- {
- if (Physics.Raycast(transform.position, transform.rotation * direction, out var hit, 100f, wallLayer))
- {
- float distance = Vector3.Distance(transform.position, hit.point);
- float travelTime = distance / speed;
- if (distance > minDistance && (cooldowns[direction] == 0f || travelTime < cooldowns[direction]))
- {
- cooldowns[direction] = travelTime;
- if (coroutines[direction] != null)
- {
- StopCoroutine(coroutines[direction]);
- }
- coroutines[direction] = PlaySoundCoroutine(direction, hit.point, distance);
- StartCoroutine(coroutines[direction]);
- }
- }
- }
- cooldowns.Keys.ToList().ForEach(dir => Raycast(dir));
- }
- private IEnumerator PlaySoundCoroutine(Vector3 key, Vector3 position, float distance)
- {
- while (cooldowns[key] > 0)
- {
- cooldowns[key] -= Time.deltaTime;
- yield return null;
- }
- while (sources[key].isPlaying)
- {
- yield return null;
- }
- cooldowns[key] = 0;
- sources[key].volume = Mathf.InverseLerp(0f, 10f, distance);
- sources[key].transform.position = position;
- sources[key].Play();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement