- Home /
Slowly fades from opaque to alpha
Hey guys, I'm trying to make a material slowly changes it opacity. It's not working well because I only see my material when it's totally opaque or totally transparent(I can't see the fade effect) . Here's the code. Can someone tell me what I'm doing wrong?
using UnityEngine;
using System.Collections;
public class alphaControl : MonoBehaviour {
void Update () {
if(Input.GetKeyUp(KeyCode.T)) {
ToAlpha();
}
if(Input.GetKeyUp(KeyCode.F)) {
fromAlpha();
}
}
void ToAlpha () {
float alpha = transform.renderer.material.color.a;
while(alpha > 0) {
alpha -= Time.deltaTime;
print (alpha);
Color newColor = new Color(1, 1, 1, alpha);
transform.renderer.material.color = newColor;
}
}
void fromAlpha () {
float alpha = transform.renderer.material.color.a;
while(alpha < 1) {
alpha += Time.deltaTime;
print (alpha);
Color newColor = new Color(1, 1, 1, alpha);
transform.renderer.material.color = newColor;
}
}
}
Any help would be appreciated! Thanks from now.
Guto
Answer by Bunny83 · Mar 11, 2012 at 03:22 AM
Your code doesn't make much sense since your loop is executed without delay. You might want to use a coroutine instead. Also there's no need for two functions:
void Update ()
{
if(Input.GetKeyUp(KeyCode.T))
{
StartCoroutine(FadeTo(0.0f, 1.0f));
}
if(Input.GetKeyUp(KeyCode.F))
{
StartCoroutine(FadeTo(1.0f, 1.0f));
}
}
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = transform.renderer.material.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,aValue,t));
transform.renderer.material.color = newColor;
yield return null;
}
}
FadeTo will fade towards the value you pass in the time you specify. In the example above 1 sec.
If you want it to fade faster, just reduce the time:
StartCoroutine(FadeTo(1.0f, 0.5f));
Thanks! That's exactly what I'm looking for! Thank you again! (:
@$$anonymous$$DReptile: $$anonymous$$y bad ;) The first solution used this value to deter$$anonymous$$e the "direction" needed to get from the current value to the target value.
With the Lerp solution it's not needed anymore.
I will remove it, thanks for the hint ;)
@Partomo:
Well he probably wants to use the color "white". If you want to keep the color the material has you have to use
Color newColor = transform.renderer.material.color;
newColor.a = $$anonymous$$athf.Lerp(alpha,aValue,t);
transform.renderer.material.color = newColor;
"color" is a property, so you can't assign color.a directly. This would just invoke the "get" method of the color. So you get a copy of the color struct and than you change the alpha of that copy. This wouldn't change the actual color at all. You have to use a temp variable and assign it back to the property so the set-method get called.
just a note, I noticed that the coroutine returning null will cause it to get out of sync at times.. ins$$anonymous$$d, replace with "yield return new WaitForEndOfFrame();" to fix a strobe effect that may occur. I think that shows up when you have many items that may be running the coroutine... in my case, I had ~20 objects with large textures (0.7 $$anonymous$$B) covering quite a bit of screen space. this fixed the issue.
thanks for this! I replaced null with WaitForEndOfFrame() and it's looking a bit smoother and less frames to process.
Answer by ElRhino · Mar 09, 2012 at 05:44 PM
A non code solution might be to use the animation editor to fade out the alpha. You are able to set a key frame from 0%, and for 100%, and it will handle the fade for you. (and if you set the animation to ping pong, once it reaches the end it will go backwards, and then start again) Sorry I don't know much about the programming side.
That would be a way to do it, but I really, in first case, would want to do it by a code(Don't matter if is in C# or JS). But thanks for the reply! Someone else?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to add color/alpha control to this shader? 0 Answers
Default color issue 2 Answers
Multiple Cars not working 1 Answer
Object opacity/alpha? 1 Answer