My 2D game requires a sound to have the same volume along the y axis, but a different volume depending on the distance to the camera on the x axis. I can’t figure out how to code the last part of the script though, can you help me? The volume should stay at 0 until the distance is equal/smaller than MinDist at which point the volume should grow linearly until the distance is equeal/smaller than MinDist.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spatialFader : MonoBehaviour
{
private float pos;
private float pos_other;
private float dist;
private AudioSource source;
public float MinDist;
public float MaxDist;
private float sqrMinDist;
private float sqrMaxDist;
void Start ()
{
source = GetComponent<AudioSource> ();
sqrMinDist = MinDist * MinDist;
sqrMaxDist = MaxDist * MaxDist;
}
void Update () {
pos_other = GameObject.Find("Main Camera").transform.position.x;
pos = transform.position.x;
dist = pos - pos_other;
print("Distance to other: " + dist);
//this is the part where I don't know what to do anymore:
var t =
source.volume = Mathf.Lerp(0,1,t);
}
}