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.
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.