Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by straydogstrut · Apr 10, 2010 at 02:20 AM · timerguitextenable

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         } 
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges