Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Gherid_lacksGPS · Oct 21, 2013 at 07:18 AM · guitriggerfade

How to fade in onGUI on trigger enter/exit? Gui shows on trigger, but no fade.

Have this script from a YT tutorial by SpeedTutor:


 #pragma strict
 
 private var guiShow : boolean = false;
 
 var fadeColor : Texture;
 
 function OnTriggerStay (Col : Collider)
 {
     if(Col.tag == "Player")
     {
         guiShow = true;
     }
 }
 
 function OnTriggerExit (Col : Collider)
 {
     if(Col.tag == "Player")
     {
         guiShow = false;
     }
 }
 
 function OnGUI()
 {
     if(guiShow == true)
     {
         GUI.DrawTexture(Rect(Screen.width / 1000.0, Screen.height / 1000.0, 2048, 1024), fadeColor);
     }
 }


Pops a gui texture from enter until exit. Would like to fade texture "in" on enter, then "out" on exit. For instance, during a respawn or as a "flash" effect. I'm a scrub at code and have only recently started playing around with scripting, my knowledge of syntax, order and structure is very poor. If someone could help me out as to which function to put where in order to acheive the desired effect, that would be splendid.

TIA

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 TonyLi · Oct 21, 2013 at 01:37 PM

GUI.color sets the global tinting for the GUI. It's an (r,g,b,a) value, where a is the alpha (transparency). If you set it to something less than one, the GUI will be semi-transparent.

To fade in or out, you need to gradually change the alpha value. You can use coroutines to do this, as in the code below. The FadeIn() and FadeOut() coroutines fade over 1 second. I made them short to keep the example clear. In practice, you'll probably want to modify the coroutines to update the alpha value every frame and use a variable to specify the fade duration. The Coroutines manual page has an example that does this.

 private var guiShow : boolean = false;
  
 var fadeColor : Texture;
 var currentGUIColor : Color;
  
 function OnTriggerStay (Col : Collider) 
 {
     if ((Col.tag == "Player") && !guiShow)
     {
         FadeIn();
     }
 }
  
 function OnTriggerExit (Col : Collider)
 {
     if ((Col.tag == "Player") && guiShow)
     {
         FadeOut();
     }
 }
  
 function OnGUI()
 {
     if (guiShow)
     {
         var originalColor = GUI.color;
         GUI.color = currentGUIColor;
         GUI.DrawTexture(Rect(Screen.width / 1000.0, Screen.height / 1000.0, 2048, 1024), fadeColor);
         GUI.color = originalColor;
     }
 }
 
 function FadeIn()
 {
     guiShow = true;
     for (var i = 0; i < 10; i++) 
     {
         currentGUIColor = new Color(1,1,1, i/10);
         yield WaitForSeconds(0.1f);
     }
     currentGUIColor = new Color(1,1,1,1);
 }
 
 function FadeOut()
 {
     for (var i = 10; i > 10; i--) 
     {
         currentGUIColor = new Color(1,1,1, i/10);
         yield WaitForSeconds(0.1f);
     }
     currentGUIColor = new Color(1,1,1,0);
     guiShow = false;
 }
 


[1]: http://docs.unity3d.com/Documentation/Manual/Coroutines.html

Comment
Add comment · Show 2 · 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
avatar image Gherid_lacksGPS · Oct 22, 2013 at 04:06 AM 0
Share

Thanks for the input. It's still not fading as I'd like, but this gives me something to play with and look into. Again, thank you and if you have any more advice/help please feel free.

avatar image TonyLi · Oct 22, 2013 at 12:52 PM 0
Share

Happy to help. What's not working as you'd like?

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Application LoadLevel on Input.KeydownEnter TextField 0 Answers

fade in out multi images then go to next scene 1 Answer

Why can't void be used in this context? 2 Answers

Transitions When Pausing? 1 Answer

Fade In GUI on proximity 1 Answer


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