fade in and fade out of a gameobject

hy,

i get a question.

i have an gameobject, which i want to fade out, if i press the “l” key.

so i tried to write a script down:

function Update() { 

if (Input.GetKeyDown("l")) { 

FadeIn(); 

} 

} 



function FadeIn() { 
	
	

var duration : float = .1; //the time in seconds to move 





for (t=0.0;t<duration;t+=Time.deltaTime) { 
renderer.material.color.a = Mathf.Lerp(1.0, 0.0, t/duration);

yield; 


}

but it doesnt works.

i’m sure anyone has already done this, doesn’t looks to be so difficult.

so thank you in advance.
gruss
n

If I remember correctly, you have to create a new color and assign that color to the material. It seems like a good approach is given for the Material.color doc page: http://unity3d.com/Documentation/ScriptReference/Material-color.html. Just set you begin color (what you start with) and you end color (the Color with its final alpha) in Start() or the Inspector.

yeah, but i’m confused how to implement an alpha to it. because its only the alpha changing that i want.
or is there a possibility to show game objects on/off?

thank you

Are you using a material that becomes transparent based on the alpha channel? If not, it obviously won’t be effected by changing the alpha. :slight_smile:

In C# you’d need to make a copy of the color, change the alpha then assign that copy back to the material’s color for it to work, but that code above should work in javascript IIRC.

Also, you can make an object stop showing up using the renderer.

renderer.enabled = false;

If you want to fade you can do something like this

var colorStart : Color;
var colorEnd : Color;
var duration = 1.0;

function Start () {
  colorStart = renderer.material.color;
  colorEnd = Color(colorStart.r, colorStart.g, colorStart.b, 0.0);
}

function OnMouseDown () {
  FadeOut();
}

function FadeOut ()
{
  for (t = 0.0; t < duration; t += Time.deltaTime) {
    renderer.material.color = Color.Lerp (colorStart, colorEnd, t/duration);
    yield;
  }
}
1 Like

i tried to toggle the object on off by pressing v, but it doesn’t work at all.

here’s my script:

function Update() { 
if (Input.GetKeyDown("v")) { 
renderer.enabled = false;
} 
}

i already sorry for questioning again, this must be some stupid fault i know it.

No faults at all…the only way that script wouldn’t work is if it wasn’t attached to an object. Or, when you say “toggle”, do you mean you want the V key to turn the object back on again too? Because that code will just turn the object off (or if it’s off to begin with, it will stay off). If you do want an actual toggle, then change “renderer.enabled = false;” to “renderer.enabled = !renderer.enabled;”. That way the renderer will have the opposite value whenever you press the key.

–Eric

1 Like

What exactly do you mean by that? Nothing at all happens when you press “v”? I ask that as I created a new project, created a new simple cube and then attached the following JavaScript to it and all worked great (the visibility of the cube was toggled between true/false each time I pressed “v”):

function Update() {
  if (Input.GetKeyDown("v")) {
    renderer.enabled = !(renderer.enabled);
  }
}

edit: use eric’s…

yeas, thank you. its exactly that!

but the visible quality has gone worse since i used my script.
what can i do get better quality and better aliasing?

Go to Edit → Project Settings → Quality, and change the Editor Quality setting. Of course, what that quality setting does, exactly, depends on how you set up the quality settings below. Also you might need to restart Unity when you do that to see any effect.

–Eric

1 Like

i took already 16 super sampling, but there is still this “non antialiased” edges.

As usual, I’m too late for the party, but want all the fun of being there.

I’ve incorporated this script, including the V toggle. But, the material snaps on and off - it doesn’t fade. Do I need to have the object have a particular shader?

Oh, and BTW, this is how the script currently stands:

var colorStart : Color;
var colorEnd : Color;
var duration = 3.0;

function Start () {
  colorStart = renderer.material.color;
  colorEnd = Color(colorStart.r, colorStart.g, colorStart.b, 0.0);
}


function Update() {
if (Input.GetKeyDown("v")) {
renderer.enabled = !(renderer.enabled); 
}
}

function FadeOut ()
{
  for (t = 0.0; t < duration; t += Time.deltaTime) {
    renderer.material.color = Color.Lerp (colorStart, colorEnd, t/duration);
    yield;
  }
}

Thanks.

OK…solved some of the problem.

Code now looks like:

var colorStart : Color;
var colorEnd : Color;
var duration = 3.0;

function Start () {
  colorStart = renderer.material.color;
  colorEnd = Color(colorStart.r, colorStart.g, colorStart.b, 0.0);
}


function Update() {
if (Input.GetKeyDown("v")) {
  FadeOut();
} 
}

function FadeOut ()
{
  for (t = 0.0; t < duration; t += Time.deltaTime) {
    renderer.material.color = Color.Lerp (colorStart, colorEnd, t/duration);
    yield;
  }
}

But…how can I append the code so that when I hit “v” again, it fades the object back up…?

wadamw said:

It’d probably be easier to toggle between two different keys or use GetKeyDown and GetKeyUp for the “v” key (but in that case you have to hold the key down until the object fades). Otherwise–although I could be wrong about it–I think you’ll have a lot of “else…if” and “switch…case” statements, as in the code at the end of this thread about combo button presses:

http://forum.unity3d.com//viewtopic.php?t=2217&highlight=combo

Here’s the script toggling between two keys, with colorStart and colorEnd switched around in the FadeIn function:

var colorStart : Color; 
var colorEnd : Color; 
var duration = 1.0; 

function Start () { 
  colorStart = renderer.material.color; 
  colorEnd = Color(colorStart.r, colorStart.g, colorStart.b, 0.0); 
} 

function Update() { 
if (Input.GetKeyDown("v")) {
  	FadeOut();
	}
if (Input.GetKeyDown("b")) {
	FadeIn();
	}
} 

function FadeOut () 
{ 
  for (t = 0.0; t < duration; t += Time.deltaTime) { 
    renderer.material.color = Color.Lerp (colorStart, colorEnd, t/duration); 
    yield; 
  } 
}

function FadeIn ()
{
  for (t = 0.0; t < duration; t += Time.deltaTime) { 
    renderer.material.color = Color.Lerp (colorEnd, colorStart, t/duration); 
    yield; 
  } 
}

(I’m pretty new at this coding stuff, but I’m trying to sound like I know what I’m doing
:wink: )

Thanks much for that. It would still be nice to have it toggle on one button - but I can work on that later.

using this script, is there a way to start with the material transparent?

wadamw said:

Actually, you don’t have to code it in at all. In the Alpha shader, just click on the main color and use the Opacity slider to set the transparency where you want it.

I added some things and prettied up your script:
• Using a virtual button instead of GetKey. Set it up in the editor under Edit>Project Settings>Input and make sure its name matches the one you set in the inspector.
• The script only uses one button. To keep track of whether it’s faded or not, we use a boolean named “faded”.
• We use a boolean called “fading” to block input while we are transitioning. This prevents snapping from one color to another.
• Made everything private except the things you need to change.

var fadeTime = 1.0;
var buttonName = "Toggle Material";
//color records and state booleans
private var solidColor : Color; 
private var fadedColor : Color;
private var fading : boolean = false;
private var faded : boolean = false;
//record color
function Start() { 
	solidColor = renderer.material.color; 
	fadedColor = new Color(solidColor.r, solidColor.g, solidColor.b, 0.0); 
} 
//check for input only if we aren't in the middle of fading
function Update() { 
	if (!fading  Input.GetButtonDown(buttonName)) { 
		if (faded)
			FadeIn();
		else
			FadeOut();
	}
} 
//set fading and lerp from faded to solid over fadeTime
function FadeIn() {
	fading = true; 
	for (t = 0.0; t < fadeTime; t += Time.deltaTime) { 
		renderer.material.color = Color.Lerp(fadedColor, solidColor, t/fadeTime); 
		yield; 
	}
	fading = false; 
	faded = false;
} 
//set fading and lerp from solid to faded over fadeTime
function FadeOut() { 
	fading = true;
	for (t = 0.0; t < fadeTime; t += Time.deltaTime) { 
		renderer.material.color = Color.Lerp(solidColor, fadedColor, t/fadeTime); 
		yield; 
	}
	fading = false;
	faded = true;
}
2 Likes