Help required scripting a GUI texture [RESOLVED]

So here’s my predicament, I have here an adapted piece of JavaScipt that was once used to fade out a GUI text I have adapted it so it fades GUI textures instead. Anyway I was wondering if anyone would be so kind to adapt this further for me.

Basically I need the GUI texture to fade in when the player’s hit points reach 30 and below. I also need it to fade away again if the hit points reach above 30 again.

Here’s the code…

CODE REMOVED, NEW CODE BELOW

Thanks in advance :smile:

Okay, here’s another code that I adapted from the FadeIn script from the Unify Community Wiki. What I’m trying to do is add a GUI Texture that fades in when HP is lower than 25 and fades out again when HP is exceeding 25. (Like in Call of Duty how the edges of the screen go red when health is low)

I have little to no experience scripting so excuse me if this makes no sense.

CODE REMOVED, NEW CODE BELOW

I’m not sure about those two static variables… I’m trying to access a variable from another script, If someone could explain how that is done you could set me on the right path. Cheers.

Don’t worry all, I worked it out. I thought I’d post the script for anyone who wants it.

//blood splat HUD element
var fadeDuration:float=0.5;
var initialDelay:float=5;
private var timeLeft:float=0.5;

FPSPlayer.hitPoints = 100;

function Awake () {
   timeLeft = fadeDuration;
}

function Update () {
   if (initialDelay > 0){
      initialDelay = initialDelay-Time.deltaTime;
   } else {
      if (FPSPlayer.hitPoints < 25.0)
         fade(true);   
      else {
      	if (FPSPlayer.hitPoints > 25.0)
         fade(false);
      }
   }
}

function fade(direction:boolean){
   var alpha;
   if (direction){
      if (guiTexture.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture.color.a=0.5-(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (guiTexture.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture.color.a=alpha/2;
      } else {
         timeLeft = fadeDuration;
      }
   }
}

Just change your hitPoint variable to a static variable and apply this script to the GUITexure that you want to fade in and out between certain levels of HP (25.0 by script default).

Is it possible to make this work for any resolution?

Like adapting this using

var almostdead : Texture;

And drawing the texture?(which would be possible to fit in any screen)

The problem is, i dont know how to do the fading part. The one in that code dosnt work with a variable type “texture”.

If you could post what you’ve done to the code perhaps I might be able to help, I think I know what you’re talking about though. It should be possible.

Thanks for posting your solution but I was wondering if you could help me make it work for my GuiTexture menu. I want to be able to fade a menu via the user pressing ‘space’. I had it working but without a fade via the following code

public var Menudisplay : int = 1;
var menu : GUITexture; 
public var Level : int = (Application.loadedLevel);

function Start () 
{ 
	if (Application.loadedLevel == 0)
	{
   menu.enabled = true; 
   Menudisplay = 1;
   }
   
   else if (Application.loadedLevel == 1)
   {
   menu.enabled = false; 
   Menudisplay = 0;
   }
   
}

function Update () 
{
	var ClockScript = GameObject.FindWithTag("Clock");
	
if ((ClockScript.GetComponent(Clock).displaySeconds == 10)  (Menudisplay == 1))
	{
	Menudisplay = 0;
	menu.enabled = false;
	}
	
	if (Input.GetKeyDown ("space")  (Menudisplay == 1))
	{
	Menudisplay = 0;
	menu.enabled = false;
	}

	else if (Input.GetKeyDown ("space")  (Menudisplay == 0))
	{
	Menudisplay = 1;
	menu.enabled = true;
	}
}

And then I tried adding bits of your code to mine and can’t seem to get it nailed. I’m really quite new to js coding (all coding to be precise) but I’m starting to understand it. I just need your help if possible.

public var Menudisplay : int = 1;
var menu : GUITexture; 
var fadeDuration:float=0.5;
private var timeLeft:float=0.5;
public var Level : int = (Application.loadedLevel);

function Awake () {
   timeLeft = fadeDuration;
}

function Start () 
{ 
	if (Application.loadedLevel == 0)
	{
   menu.enabled = true; 
   Menudisplay = 1;
   }
   
   else if (Application.loadedLevel == 1)
   {
   menu.enabled = false; 
   Menudisplay = 0;
   }
   
}

function Update () 
{
	var ClockScript = GameObject.FindWithTag("Clock");
	
if ((ClockScript.GetComponent(Clock).displaySeconds == 10)  (Menudisplay == 1))
	{
	Menudisplay = 0;
	menu.enabled = false;
	}
	
	if (Input.GetKeyDown ("space")  (Menudisplay == 1))
	{
	Menudisplay = 0;
	fade(true);
	}

	else if (Input.GetKeyDown ("space")  (Menudisplay == 0))
	{
	Menudisplay = 1;
	fade(true);
	}
}	

function fade(direction:boolean){
   var alpha;
   if (direction){
      if (guiTexture.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture.color.a=0.5-(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (guiTexture.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture.color.a=alpha/2;
      } else {
         timeLeft = fadeDuration;
      }
   }
   }

Thanks in advance

Worked like a charm for me, thanks, FYI, I used this code to create a black-out/pass out effect after sprinting for a long time.

Footers u need to change guiTexture.color.a to menu.color.a

complete code below tested on unity 3 just in case if anyone needs it

var timeLeft:float;
var fadeDuration:float;
var guiTexture1:GUITexture;
var initialDelay:float;


function Start(){
fadeDuration=0.5;
timeLeft=0.5;
initialDelay=5;

}

function Awake () {
   timeLeft = fadeDuration;
}


function Update () {

		// custom code 
			if(Input.GetKey("s")){
			print("Keyed");
			
			fade(true);
			}
				if(Input.GetKey("d")){
			print("Keyed");
			
			fade(false);
			}
			//Eof custom code
}


function fade(direction:boolean){
   var alpha;
   if (direction){
      if (guiTexture1.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture1.color.a=0.5-(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (guiTexture1.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture1.color.a=alpha/2;
      } else {
         timeLeft = fadeDuration;
      }
   }
}

Thanks rox

That’s cool, seeing how this topic has caught my attention again, I’ll share something. I constructed a rather nifty bit of code recently on a project I’ve been working on.

It requires the addition of a new variable to whatever script is managing health to work out how much health has been lost. Using this we can determine the opacity of the GUITexture.

function Update () {
	guiTexture.color.a = (playerHealth.HPlost / 100);
}

Assuming the base Health variable is 100 this should work in almost any scenario to create the CoD style red border health system. Alternately you can divide the ‘playerHealth.HPlost’ by 200, this will keep maximum transparency at 0.5 (128).

Any questions?