How to make object turn transparent then solid again through a time duration?

I want to actually animate an object that can after a period of time it becomes transparent,then solid again. In other words its in a sort of state of flux changing its appearance: solid>transparent>solid. How to do this?Thank you.

1 Answer

1

from those Unity Script Reference links it can be derived : to modify the alpha channel of a color, of a material, that is associated with the renderer, use

renderer.material.color.a = newAlpha;

Note that the alpha channel value is between 0 and 1 (not 0 and 255). And all the colour values are the same, between 0 and 1.

To do this over time, you need to implement a timer.

This is from one of my scripts. It has the basic functionality you are asking for. As the health is decreased, the alpha is increased :

#pragma strict

var health : float = 100.0;
private var startingHealth : float;
var healthDecayRate : float = 5.0;

function Start() 
{
	startingHealth = health;
	renderer.material.color.a = 0.0;
}

function Update() 
{
	DecreaseHealth();
}

function DecreaseHealth() 
{
	var decayModifier : float = startingHealth / healthDecayRate;
	
	health -= decayModifier * Time.deltaTime;
	
	// calculate the alpha
	var newAlpha : float = 1.0 - (health / startingHealth);
	
	renderer.material.color.a = newAlpha;
	
	if ( health <= 0.0 )
	{
		Debug.Log( "Player is OUT OF HEALTH" );
	}
}

if you want the alpha to decrease as the health drops, change this line :

// calculate the alpha
var newAlpha : float = health / startingHealth;