Answers for "unity 2d sound lower at distance"

C#
0

unity 2d sound lower at distance

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Volume2D : MonoBehaviour
{
    public Transform listenerTransform;
    public AudioSource audioSource;
    public float minDist=1;
    public float maxDist=400;
 
    void Update()
    {
        float dist = Vector3.Distance(transform.position, listenerTransform.position);
 
        if(dist < minDist)
        {
            audioSource.volume = 1;
        }
        else if(dist > maxDist)
        {
            audioSource.volume = 0;
        }
        else
        {
            audioSource.volume = 1 - ((dist - minDist) / (maxDist - minDist));
        }
    }
}
Posted by: Guest on May-14-2021

C# Answers by Framework

Browse Popular Code Answers by Language