x


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?

more ▼

asked Apr 02 '12 at 07:29 PM

iguana72 gravatar image

iguana72
2 3 5 6

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

2 answers: sort oldest

Use a script or Animation Editor to run the alpha on the main color of the material it uses from 1 to 0.

more ▼

answered Apr 02 '12 at 09:26 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

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 texture : Texture2D; 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 ); }

Apr 02 '12 at 11:23 PM iguana72
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Apr 02 '12 at 11:30 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

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 = Mathf.Clamp(alpha, 0, 1);

}

function Hurt() { alpha = 100; }

Apr 03 '12 at 12:02 AM iguana72

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 instead 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 = Mathf.Max(0, alpha);
    hurt.color.a = Mathf.Clamp01(alpha);

}

If you want some help getting it to fade after a certain amount of time has elapsed, just ask :)

Hope that helps, Klep

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

Apr 03 '12 at 03:48 AM Kleptomaniac

Thanks Klepto. I tried putting your code in place of mine, 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.

Apr 03 '12 at 04:02 AM iguana72

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 assuming that your Hurt() function is called when you are shot? Is the debug you added being called? Make 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 ...

Apr 03 '12 at 04:21 AM Kleptomaniac

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?

Apr 03 '12 at 04:22 AM Kleptomaniac
(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:

x3680
x1172
x258
x189

asked: Apr 02 '12 at 07:29 PM

Seen: 674 times

Last Updated: Apr 03 '12 at 04:46 AM