- Home /
Fade out a material that has a texture
Ok what I am asking for is fairly simple however it is giving me problems. I have also seen similar questions asked but none of the suggestions have worked for me. I am trying to create a shock wave that expands outwards then fades away programaticaly. I am using this code:
var Dammage : float = 1; //How much dammage the projectile does on impact;
var Speed : float = 1; //How fast does the projectile move
var Lifetime : float = 10; //How long does the projectile last
private var TimePassed : float = 0;
function Update ()
{
transform.localScale += Vector3(Speed,Speed,Speed);
TimePassed += Time.deltaTime;
if(TimePassed >= Lifetime / 2)
{
//Fade Material
renderer.material.color.a = 1 - 1 * TimePassed / Lifetime;
Debug.Log("Should fade");
}
if(TimePassed >= Lifetime)
{
Destroy(gameObject);
}
}
This does not throw up any errors in the compiler nor at run-time, however it also has no effect. The material I am using is a "Unlit/Transparent" so it does have an alpha channel. However, it does also have a texture assigned to it that has its own alpha settings. Is there something special I need to do to get a material to fade out when it has a texture, or am I just doing something wrong?
Thanks in advance for the help : )
Answer by robertbu · Feb 25, 2014 at 10:45 PM
You need a shader that 1) supports transparency and 2) has an Color property. Unlit/Transparent does not have an Color property, so setting the alpha value in the color does nothing. I use a shader that came with another package for what you are doing here, but I believe this shader from the Wiki should do the job:
http://wiki.unity3d.com/index.php/UnlitAlphaWithFade
Also Unity's Diffuse shader has a Color property, so you can start by using that and put a directional light in the scene just to get your code working.
[1]: http://wiki.unity3d.com/index.php/AlphaSelfIllum
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Renderer.material not cloning? 3 Answers
Material doesn't have a color property '_Color' 4 Answers
How to Make a Character Flicker? 1 Answer
Can't animate Material.Color properties 3 Answers