- Home /
|| inside if statement not working. (noob)
Hi guys, what I am trying to do is get the screen to fade out black when a life is lost. A life is lost by connecting with the trigger (this works fine and the fading code works). Unfortunately I cannot seem to get it to work for multiple values (I want it to fade every time a life is lost, not just ones). Any help is appreciated :)
public var fadeOutTexture : Texture2D;
public var fadeSpeed = 0.3;
var lives = 3;
private var alpha = 1.0;
private var desiredAlpha = 0.0f;
private var fadeOut = false;
private var fadeDir = -1;
function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag =="Player")
lives--;
fadeOut = true;
}
function OnGUI(){
if (fadeOut){
GUI.color.a = alpha;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
alpha += fadeDir * fadeSpeed * Time.deltaTime;
}
}
Answer by Noah-1 · Oct 12, 2012 at 10:42 PM
I'm not sure of understanding your question, but I will try to answer it:
|| stands for OR example:
if(this || this){//do this}
try using && which stands for AND.
Yeah I tried it already but it didn't work :/ I am thinking maybe it's because I am asking it to repeat it multiple times (fade every time you die). Can the OnGUI function do this?
Answer by echofiend · Oct 12, 2012 at 11:38 PM
Right now you have it only triggering when your lives count == 1. In order to do this you need to implement a function for when you lose a life that fades the screen
public var fadeOutTexture : Texture2D;
public var fadeSpeed = 0.3;
var lives = 3;
private var alpha = 1.0;
private var desiredAlpha = 0.0f;
private var fadeOut = false;
private var fadeDir = -1;
function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag =="Player")
RemoveLifeAndFadeScreen();//This function is added
}
function OnGUI(){
//All of your alpha adjusting is done in Update() where most logic should be done
//From here all of your desired alpha will be set, so you just apply it
GUI.color.a = alpha;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
//This will fire everytime you lose a life
function RemoveLifeAndFadeScreen(){
lives--;
fadeOut = true;
}
}
function Update()
{
//check to see if we should be fading
if(fadeOut)
if(alpha > desiredAlpha)//change this comparison to however you need it currently
alpha += fadeDir * fadeSpeed * Time.deltaTime;
}
Also you will need to decide how you want to handle the "FadeIn" because if you fade it out once it will remain that way. Unless you manually bring it back up. So you will need to have a check to see if alpha=0, and then decide if you want to immediately bring it to 1.0 or reverse your fadeout.
I updated the code.
It fades when the collider is hit, but only once. How would I go about implementing it to fade every time it's hit? Thanks for the reply.
you would need to implement a function to reset the alpha whenever you think it should be... so if after lets say 5secs of black you make it light again, put a function in that says something of the sorts of if(whenTheyDie > 5 seconds){fadeToBlack = false; alpha = 1.0f;}
Did this work for you... sorry im not going to write your entire algorithm for this, but I gave you the Jist of what you needed to do
It did not, and I never stated I wanted you to write it for me, in fact, that's the last thing I want. If you write it for me, I do not learn, simple. I appreciate the help but my university lecturer showed me the right ropes, thanks anyway.
Answer by Bunny83 · Oct 12, 2012 at 11:33 PM
This won't work this way. Your "lives" variable is an integer so it holds a whole number (like 7, 2, 1, ...). When you do something like
if(lives==1 || lives==2 || lives==3)
the body of the if will be executed as long as lives is 1, 2 or 3. It doesn't "detect" changes of the variable. OnGUI is called several times each frame and the if statement is evaluated each time.
If i get it right you want to fade the screen out when you lost a live. Since you want to fade it out multiple times it has to revert back to normal at some point. You didn't give much information on your actual procedure...
I guess your "fadeOutTexture" is the texture you want to actually "fade in" so the screen is hidded by the texture. In this case you have to slowly increase the alpha.
One way is to use a coroutine like this:
public var fadeOutTexture : Texture2D;
public var fadeSpeed = 0.3;
var lives = 3;
private var alpha = 1.0;
private var fadeDir = 1.0;
function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag == "Player")
{
lives --;
alpha = 0.0f;
fadeDir = 1;
while(alpha < 1.0f)
{
alpha += fadeDir * fadeSpeed * Time.deltaTime;
yield null;
}
// Here the fade texture is completly visible
// Do something and when you want to fade back in, just revert:
fadeDir = -1;
while(alpha > 0.0f)
{
alpha += fadeDir * fadeSpeed * Time.deltaTime;
yield null;
}
// here, the texture is invisible again
}
}
function OnGUI()
{
if (alpha > 0.0f)
{
GUI.color.a = alpha;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
}
Your answer
Follow this Question
Related Questions
If \ Else how does the program reads it? 2 Answers
How to make an if statement with two conditions? 1 Answer
How can I perform a += on an if statement? 1 Answer
|| conditional statement not working 1 Answer
nested if statements doubt 2 Answers