- Home /
slowly make gui image more transparent
In first person shooter games like Call of Duty and others, whenever you're hurt you get a little blood-like splat on your screen and that slowly becomes transparent. Is there any simple way to do this? Or would I have to create every single image?
Answer by DaveA · Apr 02, 2012 at 09:26 PM
Use a script or Animation Editor to run the alpha on the main color of the material it uses from 1 to 0.
Thanks. But that's not really going to work. The script I use to place the image on the screen uses a texture. How can I place a material on the center of the screen?
Here's the code I tried using.
var position : Rect; var show = false; function Start() { position = Rect( ( Screen.width - texture.width ) / 2, (Screen.height - texture.height ) / 2, texture.width, texture.height ); } function OnGUI() { if (show) GUI.DrawTexture( position, texture ); }var texture : Texture2D;
Answer by aldonaletto · Apr 02, 2012 at 11:30 PM
I answered a similar question some time ago: http://answers.unity3d.com/questions/189677/blood-damage-like-call-of-duty.html
Thanks! Based upon what I read I tried to make what I wanted. (Basically have a method that's called and it sets the image fully visable and it slowly becomes transparent. Rather than having blood all the time) So I tried this and nothings happening. (Btw, I can't figure out how to put all my code into the code box thingy)
var hurt : GUITexture; private var alpha = 100;
function Start () { var r = Rect(-Screen.width/2, -Screen.height/2, Screen.width, Screen.height); hurt.pixelInset = r; }
function Update () { if (alpha > 0) alpha--;
hurt.color.a = $$anonymous$$athf.Clamp(alpha, 0, 1);
}
function Hurt() { alpha = 100; }
Hey there,
You're on the right track. The code you have there works, however because you are subtracting one integer value per frame (because you are dynamically typecasting, the compiler assumes alpha is an int), its jumping straight from fully opaque to fully transparent in one frame, which isn't what you want.
I ins$$anonymous$$d used Time.deltaTime and a speed multiplier to smooth the fade (I would use Lerp, but to be honest, I've never fully understood how to get them to work properly, because the docs are wrong ...)
Here's code in which your texture immediately begins to fade:
var hurt : GUITexture;
private var alpha : float = 1;
private var decRate : float = 0.5;
function Start () {
var r = Rect(-Screen.width/2, -Screen.height/2, Screen.width, Screen.height);
hurt.pixelInset = r;
}
function Update () {
alpha -= Time.deltaTime * decRate;
alpha = $$anonymous$$athf.$$anonymous$$ax(0, alpha);
hurt.color.a = $$anonymous$$athf.Clamp01(alpha);
}
If you want some help getting it to fade after a certain amount of time has elapsed, just ask :)
Hope that helps, $$anonymous$$lep
P.S. If you want to format code in comments properly, highlight the whole code in the answer box, press the 101010 button, then copy newly formatted code into your comment and your good to go :) Sometimes it can be a bit touch and go though ...
Thanks $$anonymous$$lepto. I tried putting your code in place of $$anonymous$$e, but nothing shows up. I added a debug to make sure the Hurt method (Which I added) was actually called. By the way, I'm no entirely sure what value I should set alpha to to make it visible. That may be the problem.
To make it entirely visible? The alpha of a material or texture is based on a range from 0 to 1 (0 being fully transparent, 1 being fully opaque), so to make your texture fully opaque you would set its alpha value to 1. :)
So, I'm assu$$anonymous$$g that your Hurt() function is called when you are shot? Is the debug you added being called? $$anonymous$$ake sure in that function that alpha is being set to 1 as opposed to 100 (however even with a value of 100 the texture should still show up). Hmm ...
I have tested everything in my code and it all works fine, so perhaps showing me how you've inputted it into your own code would help?
Your answer
Follow this Question
Related Questions
Fade GUITexture 1 Answer
FadeIn/FadeOut GUI Menu and Text 1 Answer
Repeat fade in and out 1 Answer
How can I move Text and Image component in unison inside of Canvas 0 Answers
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers