Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created November 11, 2013 21:47
Show Gist options
  • Save jakevsrobots/7421023 to your computer and use it in GitHub Desktop.
Save jakevsrobots/7421023 to your computer and use it in GitHub Desktop.
Example script to fade in an audio player when entering a trigger. There should be an AudioSource component also attached to this game object, and it should probably be in "loop" mode.
using UnityEngine;
using System.Collections;
public class TriggerSound : MonoBehaviour {
float targetVolume = 0;
void Start() {
audio.Play();
}
void Update() {
audio.pitch = Mathf.MoveTowards(audio.volume, targetVolume,
Time.deltaTime);
}
void OnTriggerEnter() {
targetVolume = 1;
}
void OnTriggerExit() {
targetVolume = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment