Question by
InjectWare · Aug 27, 2018 at 04:57 PM ·
audioaudiosourcefadeoutfadeinaudio.play
Audio Fade In / Fade Out with Trigger
Hello, this is a simple Audio fade In / Fade Out Script with trigger function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class (????????? Script Name ?????????) : MonoBehaviour {
[Header("+++ Audio Source +++")]
public AudioSource audio;
[Header("+++ Fade Out Setting +++")]
public float fadeOutFactor = 0.08f;
[Header("+++ Fade In Setting +++")]
public float fadeInFactor = 0.08f;
private bool fadeInOut = false;
void Start()
{
//Switches the AudioSource volume to 0.0
audio.volume = 0.0f;
audio.playOnAwake = true;
}
void Update()
{
if (audio.volume <= 0.0f) { audio.Stop(); }
if (audio.volume >= 1.0f) { audio.Play(); }
if (fadeInOut == true)
{
if (audio.volume < 1.0f)
{
audio.volume += fadeInFactor * Time.deltaTime;
}
}
if (fadeInOut == false)
{
if (audio.volume > 0.0f)
{
audio.volume -= fadeOutFactor * Time.deltaTime;
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
fadeInOut = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
fadeInOut = false;
}
}
}
Comment
Hi @InjectWare - You didn't actually define your question... yes it is a script with trigger, but what is the problem?
Your answer
Follow this Question
Related Questions
audio is not playing during IEnumerator 1 Answer
Audio Fade With Toggle 1 Answer
Audio not playing in game 1 Answer
How to reduce delay when playing sound 0 Answers
Pick Up Sound 0 Answers