- Home /
(c#) Transparency Problem - Toggle not Gradient
Hi guys. I'm trying to fade a material on a plane from semi-opaque to transparent when the game slows down, as tied to the Time.timescale
variable.
The problem is that even though the alpha channel on the material is changing gradually (I can see when I'm printing it), the plane is just flickering between totally opaque at 255 and totally transparent at 0.
I tried a bunch of different types of transparent shaders, but the one I'm using right now is Transparent/diffuse
, and I made sure the texture was a png. Any help would be much appreciated, thank you very much!
Here's the code where I'm setting Time.timescale
:
public void slowDown ()
{ //As you can see, slowDown changes the speed gradually
if (timeStop) {
if (Time.timeScale >= .2f) {
Time.timeScale -= .05f;
} else if (Time.timeScale < .2f) {
Time.timeScale = .2f;
}
} else if (!timeStop) {
if (Time.timeScale < 1) {
Time.timeScale += .1f;
} else if (Time.timeScale >= 1){
Time.timeScale = 1;
}
}
Here's where I'm doing my alpha assignments:
void Update ()
{
if (xa.timeStop) {
if (renderer.material.color.a != 255 - (255 * Time.timeScale)) {
renderer.material.color = new Color (renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, tempAlph);
tempAlph += 40;
}
}
else{
if (renderer.material.color.a >0){
renderer.material.color = new Color (renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, tempAlph);
tempAlph -= 40;
}
}
}
Answer by Bunny83 · May 05, 2013 at 07:08 PM
This is simply because the Color struct uses float values, so values between 0 and 1.
It worked! Thanks. I was reading about alpha before I started writing and I guess assumed it went from 0-255 here too