- Home /
Showing and Hiding guiText
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)
Answer by straydogstrut · Apr 11, 2010 at 02:11 AM
I came across a tiny script by David Helgason in the forums for showing subtitles from an array. It didn't do anything radically different from my existing code, but the use of two yield statements has been helpful.
Now, when the player is in range, my captions show for the specified time before disappearing for the specified delay time. There's still a couple of problems though: When the player moves out of range, it disappears but unfortunately it's not immediate, it still waits until the delay time has passed.
Also, I've decided that it's a little annoying to have it switching on/off, so, with the existing code, setting duration to 0 keeps the caption on the screen, but it no longer disappears when the player moves out of range. Against all logic as far as I can see, whenever I put in an if(inRange == false) that hides the caption, it never lets the caption show.
I'm making slow but steady progress, but if anyone has any ideas i'd be grateful.
function showCaption () { // This function is responsible for showing the caption for the audio
if(inRange){
myCaptionObj.guiText.text = myMessage;
if(!textOn){
myCaptionObj.guiText.enabled = true;
textOn = true;
if(myDuration > 0){
yield WaitForSeconds(myDuration);
myCaptionObj.guiText.text = "";
myCaptionObj.guiText.enabled = false;
yield WaitForSeconds(myDelay);
textOn = false;
}
}
}
}
Your answer
Follow this Question
Related Questions
Making a sped up timer in unity 1 Answer
Disable the function timer? 1 Answer
Timer lag at GUIText drawing. 1 Answer
GUI elements vanish when publishing 1 Answer
When object i in Array is enabled, all other objects in Array are disabled. 3 Answers