Question by
Black-Spirit · Mar 07, 2021 at 12:28 PM ·
c#audiodistancenewbiedistortion
How to change audio based on distance from multiple prefabs?
Hi All. What I'm trying to achieve is have a radio loose signal quality the further you are from radio towers. I have wrote a basic script that I cleaned up a bit for posting here. I feel I'm going about it completely wrong, and it most likely is so simple. From basic testing, it kinda does what I need, though if there are more than one radio tower, it picks the last one. Any insight of a better approach would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class TestingSignal : MonoBehaviour
{
public float LowSignal;
public float MediumSignal;
public float HighSignal;
public Transform radio;
private Transform radioTower;
public AudioMixerSnapshot radioLow;
public AudioMixerSnapshot radioMedium;
public AudioMixerSnapshot radioHigh;
private void Awake()
{
radio = this.transform;
radioTower = GameObject.FindGameObjectWithTag("Radio Tower").transform;
}
void Update()
{
if (DistanceFromTower() <= HighSignal)
{
Debug.Log("High Signal Range");
//Transition to Snapshot...
}
else
{
Debug.Log("Maybe Wrong Way of doing this....");
}
}
private float DistanceFromTower()
{
return Vector3.Distance(radioTower.position, radio.position);
}
}
Comment