- Home /
Answer by noradninja · Oct 26, 2010 at 12:02 AM
Fading a GUITexture is pretty easy:
function Fade (start : float, end : float, length : float, currentObject : GameObject) { //define Fade parmeters if (currentObject.guiTexture.color.a == start){
for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) { //for the length of time currentObject.guiTexture.color.a = Mathf.Lerp(start, end, i); //lerp the value of the transparency from the start value to the end value in equal increments yield; currentObject.guiTexture.color.a = end; // ensure the fade is completely finished (because lerp doesn't always end on an exact value) } //end for
} //end if
} //end Fade
function FlashWhenHit (){ Fade (0, 0.8, 0.5, GUITextureobjectname); yield WaitForSeconds (.01); Fade (0.8, 0, 0.5, GUITextureobjectname); }
Then, when wou want you screen to flash, just call FlashWhenHit:
if (Hit){
FlashWhenHit();
}
The above example (in FlashWhenHit) will fade your texture from 100% transparent (invisible) to 80% opaque (just slightly transparent) over 1/2 second. It checks to make sure the texture is 100% transparent before attempting the fade to eliminate visual errors, and at the end ensures it is at exactly 80% opacity. It will then wait 1/100th second, and fade the texture back out to transparent. You can, of course, adjust the starting and ending opacity by changing the start and end values in the function call, as well as how long the fade takes and what object if affects. The WaitForSeconds is in there so the texture will stay at its max opacity momentarily (to make it more visually obvious); the length of time is adjustable there too. Also, if you want the screen to flash a certain number of times, you could use a for loop with a counter that goes to 0 from, say, 3, to get the screen to flash 3 times, etc.
Hope this helps.
I suppose you could use $$anonymous$$athf.PingPong to just ramp the opacity up then back down too, I just do it this way because I think it gives me more control over the ti$$anonymous$$g, and I use Fade() to do lots of stuff like fading in and out of black at the start of a level or when a player dies, fading the UI in or out when a player touches the screen or moves the mouse, etc.
WOW that worked beautifully! I got a couple errors, but I was able to fix it :) Thank you so much!!!
Actually I am having a problem with the 2nd fade in FlashWhenHit().. The transparency only fades back to 0 when I get hit continuously. But if I only get hit once, it'll only do the first fade in FlashWhenHit() so my screen will stay red. How do I fix this?
No problem. Incidentally, were your erros something in the code I gave you or was it your implementation? I like code review and if I made a mistake, I'm curious what it was :)
Answer by noradninja · Oct 24, 2010 at 02:43 PM
A simple method:
Make fullscreen GUITexture, and set it to red. Set the opacity to 0%, and attach it to a separate camera on a layer above the main camera (so it is drawn on top of everything). Make sure you set the clear flag on this camera to Don't clear.
Set up your logic so whenever your player is hit, a variable is set to true momentarily.
Attach a script to the GUITexture that looks at the state of the hit variable, and if it is true, set the opacity of the GUITexture to 80% for a few frames and then back to 0% (this will give you the flash but also allow the player to still see the action on screen).
Thanks! Would you be able to give me an example of some pseudo-code?
I'm not sure how I would do the code for increasing the opacity for just a few frames and have it go back to 0.
Answer by Billu · Apr 01, 2015 at 04:39 PM
To show the red screen continuously instead of just instantly (for example when you are inside the fire zone and receives consistent burning damage), the fading of the opacity should be put in the update() function and changes in every frame. The following code may help in this situation.
bool increment = true;
float len = 0.5f;
float opac = 0.4f;
bool alarm;
// Update is called once per frame
void Update () {
if (alarm)
{
aColor = gameObject.guiTexture.color;
if (increment == true)
{
aColor.a += opac*Time.deltaTime/len;
} else
{
aColor.a -= opac*Time.deltaTime/len;
}
if (aColor.a > opac)
{
increment = false;
} else if (aColor.a < 0f)
{
increment = true;
}
gameObject.guiTexture.color = aColor;
}
}
When you want to start the consistent flashing screen, just set the variable 'alarm' to true. And whenever you want to stop this flashing, set the 'alarm' to be false, and also reset the opacity to 0.
Answer by hotlabs · Oct 24, 2021 at 04:14 PM
Posted a possible solution here: https://answers.unity.com/questions/1286867/screen-flashes-red-on-damage.html?childToView=1867776#answer-1867776
Your answer
Follow this Question
Related Questions
Red flash for each network player when he gets hit 1 Answer
Screen flashes red on damage? 4 Answers
Screen Flash When Hit -- Error Help From Pervious Question's Answer 1 Answer
Making screen flash texture when player is damaged 0 Answers
Fog working on my machine but not on others? /how to flash a colour on the screen 0 Answers