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 Catlinman · Jan 04, 2013 at 12:42 PM · colorupdatelerpongui

Trouble with OnGUI and Update

I am trying to make a GUI Texture fade in as the game starts. Currently the image does not fade and just hangs at zero oppacity. I am using OnGUI to draw the texture and Update to fade the textures alpha value.

 var guiImage : Texture2D;
 var guiPosition : Rect;
 var guiColor = Color.white;
 var scaleType : ScaleMode;
 var horizRatio : float = 800;
 var vertRatio : float = 600;
 var depthValue : float = 1;
 
 function Update(){
     guiColor.a = Mathf.Lerp(0,1,Time.deltaTime);
 }
 
 function OnGUI(){
     scale.x = Screen.width / horizRatio;
     scale.y = Screen.height / vertRatio;
     scale.z = 1;
 
     GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, scale);
     GUI.depth = depthValue;
     GUI.color = guiColor;
 
     GUI.DrawTexture(guiPosition,guiImage,scaleType,true);
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by whydoidoit · Jan 04, 2013 at 01:10 PM

Your problem is that you aren't using Lerp properly - you need to either Lerp with a value between 0 and 1 (not Time.deltaTime which will always be a smallish number and hence hover around the starting value - normally you would do this by adding Time.deltaTime to a float value that starts at 0) or you can Lerp between the current value and 1 which will damp towards the target:

   var t : float = 0;
   ...
   function Update()
   {
      t+=Time.deltaTime;
      guiColor.a = Mathf.Lerp(0,1,t);
   }


Or

   guiColor.a = Mathf.Lerp(guiColor.a, 1, Time.deltaTime);
Comment
Add comment · Show 1 · 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 Catlinman · Jan 04, 2013 at 02:28 PM 0
Share

I just noticed the same mistake - Thanks for pointing out though.

avatar image
3

Answer by MarkFinn · Jan 04, 2013 at 01:08 PM

 function Update(){
     guiColor.a = Mathf.Lerp(0,1,Time.deltaTime);
 }

is wrong.

Time.deltaTime is generally a very small number that doesn't change much from frame to frame.

What you want is to record the time you start and subtract that from current time and put that in your lerp instead (possibly multiplied by a value to modify the speed of fade-in.

Try:

     var startTime=0;
     
     function Start(){
      startTime=Time.Time;
 }
     
     function Update(){
         guiColor.a = Mathf.Lerp(0,1,Time.time-startTime);
     }

(may not be exactly right as I don't do javascript, tweak as necessary.)

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
avatar image
1

Answer by gbelini · Jan 04, 2013 at 01:06 PM

Hi,

This code work for me, this could help ?

 var guiImage : Texture2D;
 var guiPosition : Rect;
 var guiColor : Color = Color.white;
 var scaleType : ScaleMode;
 var horizRatio : float = 800;
 var vertRatio : float = 600;
 var depthValue : float = 1;
 var scale : Vector3;
 
 var fade : boolean = true;
 function Update()
 {
     var incr = Time.deltaTime;
     
     if (guiColor.a - incr <= 0 )
         fade = false;
     else if (guiColor.a + incr >= 1)
        fade = true;
       
     if (fade)
         guiColor.a -= incr;
     else
           guiColor.a += incr;
           
     
     
     //guiColor.a = Mathf.Lerp(0,1, Time.deltaTime);
 }
 
 function OnGUI(){
     scale.x = Screen.width / horizRatio;
     scale.y = Screen.height / vertRatio;
     scale.z = 1;
 
     GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, scale);
     GUI.depth = depthValue;
     GUI.color = guiColor;
 
     GUI.DrawTexture(guiPosition,guiImage,scaleType,true);
 }
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 Catlinman · Jan 04, 2013 at 01:11 PM 0
Share

Yes for some reason that actualy works... have to take a closer look at the differences. As far as I know $$anonymous$$athf.Lerp(0,1, Time.deltaTime); should actualy work.

avatar image Bunny83 · Jan 04, 2013 at 01:33 PM 0
Share

No, Time.delta time is the delta between this and the last frame. The value will stay about the same all the time (depends on your framerate). The t value of $$anonymous$$athf.Lerp has to go from 0 to 1

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

12 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

Related Questions

Lerp stopping/not working? 1 Answer

Lerping/Fading between multiple colours 1 Answer

Scene Fade in and out script not working 1 Answer

Change only players color but not texture 1 Answer

GetKeyDown is fired more than once in Update() 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