Fading a normally solid character to transparent.

Hi, I have good graphics and hlsl experience but am new to unity.

I wish to fade a normally solid character to transparent. Any suggestions how to do this in Unity?

For efficiency I expect it is better to swap the material for another, rather than always rendering a transparent material with full alpha. Also you might wish to use several different materials for different effects. Can you swap materials, or should I use a different object with the same mesh and different materials?

A second issue is that when dealing with something more complicated than a billboard, just varying alpha can produce bad artifacts since polygons are not properly depth sorted. Tricks to fix this could involve filling in the z-buffer first or rendering the background to a texture first.

Any pointers?

Color MyColor = R.material.color;
MyColor.a = 0.1f;
R.material.color = MyColor;
R.material.shader = Shader.Find("Lightmapped/TransparentDiffuse");

So yes it works and is pretty easy. I swapped the materials so the artist do not have to mind any shader related stuff.

The bad artifacts are mainly a result of several transparent materials on top of each other (alpha blended, alpha testing is no problem). So i dont thnik that you have to take into account any special tricks. But your suggestions would work in unity.

Thanks for that! When I looked at the Material class also stumbled across a sample for toggling predefined materials also (converted from their javascript):

using UnityEngine;
using System.Collections;

public class ToggleMaterialScript : MonoBehaviour {

public Material  [] materials = new Material[2];
public float changeInterval = 0.33f;
	
void Start()
{
// Set main color to red
		renderer.material.color = Color.red;
// Change renderer's material each changeInterval
// seconds from the material array defined in the inspector.

}

void Update ()
{ 
    if (materials.Length == 0) // do nothing if no materials
        return; 

    // we want this material index now 
    int index = (int)(Time.time / changeInterval); 
    // take a modulo with materials count so that animation repeats 
    index = index % materials.Length;
    // assign it to the renderer
    renderer.sharedMaterial = materials[index]; 
}

}

It looks like you’ve got the material side of things worked out. This is the shader you’ll want: AlphaVertexLitZ