Answers for "Pause unused Audio Sources C# Unity"

C#
0

Pause unused Audio Sources C# Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckIfAudible : MonoBehaviour
{
  AudioListener audioListener;
  AudioSource audioSource;
  float distanceFromPlayer;

  void Start()
  {
    // Finds the Audio Listener and the Audio Source on the object
    audioListener = Camera.main.GetComponent<AudioListener>();
    audioSource = gameObject.GetComponent<AudioSource>();
  }

  void Update()
  {
    distanceFromPlayer = Vector3.Distance(transform.position, audioListener.transform.position);
    
    if (distanceFromPlayer <= audioSource.maxDistance)
    {
      ToggleAudioSource(true);
    }
    else 
    {
      ToggleAudioSource(false);
    }
  }

  void ToggleAudioSource(bool isAudible)
  {
    if (!isAudible && audioSource.isPlaying) 
    {
      audioSource.Pause(); 
    }
    else if (isAudible && !audioSource.isPlaying)
    {
      audioSource.Play();
    }
  }
}Copy
Posted by: Guest on October-05-2021

C# Answers by Framework

Browse Popular Code Answers by Language