|
How would I make the screen flash red whenever the player takes damage?
(comments are locked)
|
|
Fading a GUITexture is pretty easy:
Then, when wou want you screen to flash, just call 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 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)
|
|
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?
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)
|
