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 Anymeese · Jul 17, 2013 at 07:09 PM · lerptransparencytransparentflashblink

make a gameobject lerp transparency?

I'm trying to make my player gameobject flash/blink transparency (over 1 second, go from alpha 1.0 to 0.1 and back up to 1.0)

Firstly, I know to make an object transparent, you have to (and I have done):

1) change the object's material from diffuse to transparent -> diffuse

2) use the code:

 Color tempcolor = gameobject.renderer.material.color
 tempcolor.a = (float between 0.0 and 1.0); //0 is invis, 1 is fully visible
 gameobject.renderer.material.color = tempcolor;

I then put this in a While loop (while my player's gameobject has not moved) and added in code to increase/decrease the float accordingly each time this was called. However when I try to playtest the game, Unity freezes/crashes. I'm not sure what's wrong with it so I'm not sure what to fix :/

Would it be better to make this a coroutine? Use mathf.lerp? What should I do? (NOTE: While it's flashing, it should still be able to read the rest of the code in my update function. Hitting wasd is what ends the flashing)

Thanks for any help :)

EDIT: So this is my current code: http://pastebin.com/7Nh0thL6 -- hopefully it should be quick/easy to understand!

My problem is that no matter what I set waitTime to, it still updates the alpha every frame rather than waiting after it updates. I want an entire cycle to take 0.75sec but currently it's entirely dependent on FPS and happens way too quickly

Comment
Add comment · Show 2
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 kromenak · Jul 17, 2013 at 08:20 PM 0
Share

When a "while" loop is encountered, it will loop indefinitely until the condition is false. Therefore, if you are testing a conditional in a while loop, you must change the conditional as part of that while loop, or the while loop will never exit. This will cause Unity to freeze and ultimately crash.

Since you want the value to change over time, it actually doesn't make sense to update the alpha in a while loop because the entire while loop will execute in a single game frame. It will appear that you get to the target alpha immediately.

You could certainly use a coroutine for this:

 private IEnumerator AlphaCoroutine(float targetAlpha)
 {
 while(color.a < targetAlpha)
 {
     color.a += someVal;
     gameObject.renderer.material.color = color;
 
     yield return null;
 }
 }

The yield statement causes one while loop block to run per frame. Also note that code only works for increasing alpha; you'd have to make some changes to remove alpha as well.

You can also use the Update() function with no while loop; just update the alpha up to your target alpha each update loop, and set the material color.

avatar image Anymeese · Jul 18, 2013 at 02:07 AM 0
Share

makes sense, ty :) Currently it's not showing any deviation in the alpha values, probably because it's being called once every frame. How can I make it update the alpha every 0.1 seconds, regardless of the fps? I tried adding a waitForSeconds at the end of the coroutine but that didnt help

2 Replies

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

Answer by Meltdown · Jul 18, 2013 at 02:11 AM

Don't use a while loop. Use Mathf.MoveTowards, which changes a value over time to a target value...

 Color tempcolor = gameobject.renderer.material.color
 tempcolor.a = Mathf.MoveTowards(0, 1, Time.deltaTime);
 gameobject.renderer.material.color = tempcolor;


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 Anymeese · Jul 18, 2013 at 02:27 AM 0
Share

thanks for the help! This makes the code much simpler. Now I just have:

 if(lastDirection == 'n')
         {
             Color tempColor = cubee.renderer.material.color;
             if(transparencyGoingUp)
             {
                 tempColor.a = $$anonymous$$athf.$$anonymous$$oveTowards(0.1f, 1f, Time.deltaTime);
                 transparencyGoingUp = false;
             }
             else //if it's going down
             {
                 tempColor.a = $$anonymous$$athf.$$anonymous$$oveTowards(1f, 0.1f, Time.deltaTime);
                 transparencyGoingUp = true;
             }
             cubee.renderer.material.color = tempColor;
         }

However it's still not changing everything, probably because it's being called every frame. How can I get the change in one direction (0.1 -> 1 or 1 -> 0.1) to happen over 0.75sec?

avatar image CodeAssembler · Apr 16, 2015 at 02:00 PM 0
Share

I liked the method since is fairly easy to implement and works in both directions but basically the problem in here is that for it to work you need to make sure you specify the actual variable value to be subtracted/incremented as one of the parameters, otherwise it will not work as expected since if you don't then it will increment/subtract any fixed amount you specify only once (even if its in a loop). So it needed be something like this:

 ... Class body ... 

 CanvasGroup theTree;
 
 void Start ()
 {
    theTree = GetComponent<CanvasGroup>();
 }
 
 
 ... coroutine body ... 
 
 float stepVal = 0.004f; // the amount to add or substract in time
 
 while (theTree.alpha > 0)
 {
      theTree.alpha = $$anonymous$$athf.$$anonymous$$oveTowards(theTree.alpha, 0, stepVal);
               
      yield return null;
 }
 
 ... coroutine body ...

I tried this and it worked just fine, note that you need to play around guessing a bit in order to find the ideal target step values so you can get results that works fine for your case scenario. Note that in my example theTree is a CanvasGroup component added to the UI element of a Canvas (from the recently added UGUI features) which has an alpha value element by default and that will fade all of its child objects automatically for you. Hope it be of help.

avatar image
0

Answer by Anymeese · Jul 21, 2013 at 07:40 AM

So.. This is my current code: http://pastebin.com/7Nh0thL6 -- hopefully it's very quick/easy to understand :)

My only problem is no matter what I set my timer variable (waitTime), the flashing happens incredibly quickly, as if the alpha is being incremented/decremented every frame. How do I space it out over a certain amount of time (roughly 0.75sec)?

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

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

ingame transparency 2 Answers

Transparent bumped specular 0 Answers

lerp transparency 2 Answers

Adding Transparency to a Diffuse / Spec / Normal Shader 0 Answers

Half transparent black circle 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