- Home /
Make screen flash red on Damage
Hi
I want my screen to show red when player gets hit or damaged. I found some answers online but they didn't seem complete.
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); }
Here is the code. I am not sure what to put under GUITextureobjectname? Also, where do I attache the GUITexture, on the player?
Thanks for any help.
Answer by Eric5h5 · Apr 03, 2011 at 09:24 PM
You don't attach the GUITexture to anything, just have it be somewhere in the scene. For GUITextureobjectname you use a reference to the GUITexture. i.e., "var myTexture : GUITexture;"
Answer by AVividLight · Apr 03, 2011 at 07:47 PM
Sorry I don't have time to explain this... Just add a red texture, and through a script call "Fade Out"
var fadeOutTexture : Texture2D; var fadeSpeed = 0.3;
var drawDepth = -1000;
//-------------------------------------------------------------------- // Private variables //--------------------------------------------------------------------
private var alpha = 1.0;
private var fadeDir = -1;
//-------------------------------------------------------------------- // Runtime functions //--------------------------------------------------------------------
function OnGUI(){
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//--------------------------------------------------------------------
function fadeOut(){ fadeDir = 1;
yield WaitForSeconds (0.5); fadeDir = -1; }
That's not a good idea, because then you're drawing an alpha texture over the screen all the time, regardless of whether the screen should be flashing or not (which means potentially big fill-rate hit depending on hardware).
Your answer
Follow this Question
Related Questions
Respawn with damage nwh 1 Answer
Damage Indicator not showing correct position of attack. 0 Answers
Please, I need a damage script 2 Answers
Looking for skilled individual to assist with problem 1 Answer