- Home /
How to fade out over time?
Hi to all
I'm trying to make a gameobject to gradually disappear using transparency. I could find out that a nice way to do it, is to set its shader material to '`Transparent/Cutout/VertexLit`' , and then, if I modify the '`Alpha cutoff`' property to maximum, the object became invisible(fully transparent, doesn't matter).My question is, how to do this programatically? I have tried this code, but the object disappear instantly, not gradually,as I want, either the value is big or small(theoreticaly 255 is opaque, 0 transparent).The object is textured.
void Update(){
renderer.material.SetFloat("_Cutoff",200);
renderer.material.SetFloat("_Cutoff", 150);
renderer.material.SetFloat("_Cutoff", 100);
renderer.material.SetFloat("_Cutoff", 50);
renderer. material.SetFloat("_Cutoff",0);
}
Answer by Joshua · Aug 31, 2011 at 02:25 PM
What you're currently doing is setting the cutoff to 200, then 150, 100, etc every frame, so at the end of every frame it will be zero - so as you say it'll be invisible right from the start.
The easiest way to do this is set up a coroutine.
IEnumarator FadeOut( float time, Material material )
{
float index = 0.0f;
float rate = 1.0f/time;
while( index < 1.0f )
{
index -= rate;
material.SetFloat( "_Cutoff", index );
yield return null;
}
}
You can then simply call this coroutine to fade it out in a certain amount of time from, for instance, Start
float fadeOutTime = 3.0;
void Start()
{
StartCoroutine( FadeOut( fadeOutTime, renderer.material ) );
}
Thanks for the code,but I'm new to Unity and I met some problems, at the line with 'yield' (CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement). I have placed the IEnumerator function inside my class script, and then the code inside Start(), as you wrote it here. i have tryed to improve this but I don't get the transparency effect anymore then.Could you help me, please?
$$anonymous$$y mistake, I wrote yield which should've been yield return null. Revised the code, should work now. Gl.
using UnityEngine; using System.Collections;
public class FooBar : $$anonymous$$onoBehaviour {
IEnumerator FadeOut( float time, $$anonymous$$aterial material ) { float index = 0.0f; float rate = 1.0f/time; while( index < 1.0f ) { index -= rate; material.SetFloat( "_Cutoff", index ); yield return null;
}
}
float fadeOutTime = 3.0f;
// Use this for initialization
void Start () {
transform.Rotate(0,0,180);
StartCoroutine( FadeOut( fadeOutTime, renderer.material ) );
}
}
Look how the script attached to the gameObject looks like.But I don't know why, nothing happens. The object is still 100% opaque, nothing is happing to it.I have tried to modify values but nothing.... If I put += ins$$anonymous$$d of -= it appears and disappears very fast. Could you help me, please?
Try replacing 'index -= rate' with 'index -= Time.deltaTime * time'
Your answer
Follow this Question
Related Questions
Alpha not displaying when changed in code, unless changed manually in the inspector 0 Answers
Fading in/out SVGs without showing the half-transparent SVG-layers 1 Answer
How do I add transparency to a cg vertex lit shader 2 Answers
Alpha problem with textures on a plane 2 Answers
My .PNG gets solidified and an alpha channel. How?! 1 Answer