Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed May 31, 2013 at 09:51 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by smirlianos · May 29, 2013 at 06:13 PM · alphafade3d textinout

Fade 3d text with lerp?

Hello!


I want the Main Menu 3D Texts to slowly fade out when the user hits play. I used this script but unfortunately it makes the letters black, not transparent.

Any help?


 #pragma strict
 var buttonState : int = 1;
 var click : AudioClip;
 var LevelToLoad : String;
 
 var quitButton : GameObject;
 var title : GameObject;
 
 var lerpedColor : Color = Color.white;
 private var stop : boolean;
 
 
 function OnMouseEnter () {
     if(!stop)
     {
         renderer.material.color = Color.blue;
         audio.PlayOneShot(click);
     }
 }
 
 function OnMouseExit () {
     if(!stop)
     {
         renderer.material.color = Color.white;
     }
 }
 
 function OnMouseUp () {
     if(buttonState == 1)
     {
         Camera.main.animation.Play();
         stop = true;
         //Application.LoadLevel("LevelToLoad");
     }
     else if(buttonState == 2)
     {
         Application.Quit();
     }
 }
 
 function Update () {
     if(stop)
     {
         lerpedColor = Color.Lerp(Color.white, Color.black, Time.time);
         renderer.material.color = lerpedColor;
         quitButton.renderer.material.color = lerpedColor;
         title.renderer.material.color = lerpedColor;
     }
 }
Comment
Add comment · Show 5
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 hiddenspring81 · May 29, 2013 at 06:34 PM 1
Share

Have you tried changing it to`lerpedColor = Color.Lerp(Color.white, Color.clear, Time.time);`

avatar image Eric5h5 · May 29, 2013 at 06:38 PM 0
Share

Use Fade.

avatar image OP_toss · May 29, 2013 at 06:49 PM 1
Share

You need to keep track of the start time and subtract it from the current time when lerping.

in On$$anonymous$$ouseUp:

 stop = true; 
 startTime = Time.time;

in Update:

 if (stop)
 {
 lerpedColor = Color.Lerp( Color.white, Color.clear, Time.time - startTime );

avatar image smirlianos · May 29, 2013 at 06:51 PM 0
Share

The Color.clear worked! Thanks! But it disapears the letters immediatly, probably because the time.time starts from the beggining and not from when I click the button... How can I solve that?

avatar image OP_toss · May 29, 2013 at 07:19 PM 0
Share

Look at my previous post. You need to know the start time.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by fafase · May 29, 2013 at 07:29 PM

@hiddenspring81:

 Have you tried changing it tolerpedColor = Color.Lerp(Color.white, Color.clear, Time.time);

this is wrong for two reasons, first lerping goes from the current value to the target value, and assign the result to current in order to linearly move (hence the name lerp). What you propose is to get the difference between white and black and assign the difference to the value. Second, your ratio is Time.time which means after 1s you have full move at once. Only during the first second can you see the interpolation.

Here is how you do it:

 void Update () {
   Color col = renderer.material.color;
   col.a = Mathf.Lerp (col.a,0f,0.01f);
   renderer.material.color = col;
 }

This is greatly simplified and should be put in a function for control, but the idea that you get the color and modified the a for alpha transparency. Lerp will get from the value of a which should be 1 down to 0 with a ratio of 0.01 per frame. As a result you will never really get down to 0 so you would probably favor something like:

 void Update(){
   ratio += Time.deltaTime;
   Color col = renderer.material.color;
   col.a = Mathf.Lerp (col.a,0f,ratio);
   renderer.material.color = col;
 }

This will interpolate over 1second. Adding a multiplier to ratio allows you to control the period of time, use < 1 for increasing time and > 1 to reduce time.

Note you can also use Color.Lerp.

Comment
Add comment · Show 4 · 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 OP_toss · May 29, 2013 at 07:36 PM 0
Share

Color is a value type so your way allocates another Color object in order to lerp the alpha and assign, and then reassign to the material. Not entirely sure that's much faster, although I haven't tested. I don't disagree though, it may be an improvement.

Also your ratio variable needs to be set to zero On$$anonymous$$ouseUp. Pretty much a different path to the same solution I provided. But the zero reset is what is actually causing his issue. He's not acknowledging when he started the fade.

avatar image fafase · May 29, 2013 at 07:47 PM 0
Share

This is a C# version, you would not have to do the Color assignment trick in UnityScript but in C#, Unity built-in structs are read only so you have to do this as you cannot modify the a directly.

Since Color is a value type that happens on the stack so no big issue.

As for the ratio, it is not the only issue here as I said this should help but should also be moved to a function, a coroutine would do it nice:

 IEnumerate LerpThat(float speed){
     float ratio = 0;
     while(ratio < 1){
       ratio += Time.deltaTime * speed;
       Color col = renderer.material.color;
       col.a = $$anonymous$$athf.Lerp (col.a,0f,ratio);
       renderer.material.color = col;
       yield return null;
     }
 }
avatar image hiddenspring81 · May 29, 2013 at 08:28 PM 0
Share

Sorry, I wasn't paying careful enough attention to how he was using Color.Lerp. Obviously, he wasn't Lerping correctly, I just noticed that he wasn't Lerping to the correct end-value.

avatar image smirlianos · May 31, 2013 at 09:32 AM 0
Share

Thanks guys, you helped me a lot!

Follow this Question

Answers Answers and Comments

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

Related Questions

How to fade in GUI text. 2 Answers

Easy fade in/out on level load/end? 2 Answers

Fade In and out when level is loaded 2 Answers

Alpha not displaying when changed in code, unless changed manually in the inspector 0 Answers

Fade In/out issue about a gameobject 0 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