Still on the theme of sound effects (I'll get there eventually!), I'm trying to implement a captions system in my game. Currently, I have three functions: one to check the distance between the player and the sound source, one to play the sound, and one to show the caption. Basically, whenever the player enters within range of a sound source, I want to show the caption. Then, after a specified time, the caption should disappear. If the player moves out of the range of the sound source, the caption should immediately disappear.
This is my second system of three - the first showing guiTextures - so i've attempted to use the same framework here. The following script is attached to each sound source, and at the moment it successfully shows whatever caption is set to the myMessage variable through the editor. I am using one guiText object for all captions.
I now need to:
1) Hide the caption after a specified time.
The original timer code is from Unity Game Development Essentials and is intended to work for one off events like collisions. The problem here is that I am calling the showCaption() function from the Update() function so it never gets a chance to disappear. The playOneShot() function also calls showCaption() at intervals based on animation events. I have tried adding a second If condition to avoid running through the showCaption() function if it is already showing (which worked for a similar situation using guiTextures), but this did not work here.
2) Hide the caption if the player moves outside the distance defined by the range variable.
This actually worked previously by putting myCaptionObj.guiText.enabled = false; in the second part of the if(inRange) condition, however I found it only worked for some objects, while others didn't show their caption at all.
I've put the whole script below (sorry) so you can get a better idea of how things interrelate, but the function i'm concerned about is showCaption(); towards the bottom.
// store a reference to the object this script is attached to and the player char
var thisSoundSource : GameObject;
var thePlayer : GameObject;
// variables for the guiText captions
var myCaptionObj : GameObject;
var myMessage : String;
private var textOn : boolean = false;
private var timer : float = 0.0;
// variables to handle sound range
var soundRange : float = 4.0;
private var dist : float;
private var inRange : boolean;
// determines whether this system is showing as soon as the game starts
// ie. difference between a stereo playing and a tap that must be turned on
var isEnabled : boolean = true;
// special check to make sure one-off events don't run through the Update() function
var isLooped : boolean;
function Start(){
thisSoundSource.audio.Stop();
timer = 0.0;
textOn = false;
myCaptionObj.guiText.text = "";
}
function Update () {
// if we are using the captions system (as opposed to the visuals system)
if(soundSystemController.captionsOn){
// if this sound source/caption system is on
if(isEnabled){
checkRange();
// if this is a continuous sound event, rather than a one-off animation event
if(isLooped){
playSound();
showCaption();
}
} else {
// stop the audio
if(audio.isPlaying){
audio.Stop();
}
}
}
}
function checkRange () {
// This function is responsible for monitoring the player's distance
// to the sound source
dist = Vector3.Distance(thisSoundSource.transform.position, thePlayer.transform.position);
//check whether you are within target proximity
if (dist < soundRange) {
inRange = true;
} else {
inRange = false;
}
}
function playSound () {
// This function is responsible for playing the audio source
// attached to this object
if(!audio.isPlaying){
audio.Play();
}
// manually reducing the sound volume with distance
var currentSoundVol = (1.0 - Mathf.Clamp01(dist / soundRange)) * thisSoundSource.audio.maxVolume;
thisSoundSource.audio.volume = currentSoundVol;
}
function showCaption () {
// This function is responsible for showing the caption that corresponds
// with the audio
if(inRange){
textOn = true;
} else {
textOn = false;
}
// setting and showing the caption
if(textOn){
myCaptionObj.guiText.text = myMessage;
myCaptionObj.guiText.enabled = true;
timer += Time.deltaTime;
}
// timer for life of the caption
if(timer >=5){
textOn = false;
timer = 0.0;
}
}
function playOneShot(whichFoot : String){
// This function is called from animation events for non-repeating
// one-time audio events (footsteps etc)
if(soundSystemController.captionsOn){
playSound();
showCaption();
}
}
@script RequireComponent(AudioSource)