x


Screen flashes red when taking damage

How would I make the screen flash red whenever the player takes damage?

more ▼

asked Oct 23 '10 at 09:15 AM

Lanipoop gravatar image

Lanipoop
62 6 8 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 26 '10 at 12:02 AM

noradninja gravatar image

noradninja
790 17 23 41

I suppose you could use Mathf.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 timing, 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.

Oct 26 '10 at 12:06 AM noradninja

WOW that worked beautifully! I got a couple errors, but I was able to fix it :) Thank you so much!!!

Oct 26 '10 at 02:58 AM Lanipoop

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?

Oct 26 '10 at 03:12 AM Lanipoop

Never mind, I fixed it. Thanks again!!!!!!!!

Oct 26 '10 at 03:14 AM Lanipoop

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 :)

Oct 27 '10 at 12:51 PM noradninja
(comments are locked)
10|3000 characters needed characters left

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).

more ▼

answered Oct 24 '10 at 02:43 PM

noradninja gravatar image

noradninja
790 17 23 41

Thanks! Would you be able to give me an example of some pseudo-code?

Oct 25 '10 at 12:05 AM Lanipoop

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.

Oct 25 '10 at 12:38 AM Lanipoop
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x485
x329
x270
x248
x15

asked: Oct 23 '10 at 09:15 AM

Seen: 3203 times

Last Updated: Nov 08 '12 at 04:09 PM