Issue with GUITexture width.

Within my project I have a GUITexture on an object controlled by a scripted that when the value of a characters HP goes down, it is shortened by that percent etc.

Although, the GUITexture works for the most part besides the fact that in order for the texture to be completely gone and or thinned out to where it is un-seen, the W value of the pixel inset must be -8.5.
However, that doesn’t do me any good because the percentage shouldn’t go below 0%.

How would I fix the pixel inset of the GUITexture so that when the W value is 0, the texture isn’t visible?

(P.S. As I’ve created the script so that the bar gradually slides up and down, I’d rather not use an if statement telling the bar to automatically jump to -8.5 because it would be an odd jump and it still would be un-proportional up until that point.)

Below are the current values of the GUITexture when it is in the correct position on the screen and filled to 100%. I am also using GUITexture (Legacy).

alt text

Hey, here is an option to use just a Texture of a health bar and control its size by tweaking width of GUI.DrawTexture method. There is a simple range mapping function used in order to convert health of a player into the size of the texture. Hope it helps.

using UnityEngine;
using System.Collections;

public class HUD : MonoBehaviour {

public  Texture healthBar; 						//your health bar texture
public  float playerHealth; 						//to hold player's health
private float healthBarLength; 					//to hold bar length (texture.pixelInset.width)

private float playerHealth_min    = 0;			//players health range min value
private float playerHealth_max 	  = 100;		//players health range max value
private float healthBarLength_min = 0; 			//bar length range min value
private float healthBarLength_max = 200;		//bar length range max value (desired bar max lenght)

private string playerHealth_text;				//spit it out on the screen	

void Start () 
{
	playerHealth = 100;					//initialize health to its max value
}

void Update () 
{
	//map range of health to range of healthBar length
	healthBarLength = (playerHealth - playerHealth_min) / 
					  (playerHealth_max - playerHealth_min) * 
					  (healthBarLength_max - healthBarLength_min) + healthBarLength_min;

	//constrain values to >= 0
	if (playerHealth <= 0) 	  playerHealth = 0;
	if (healthBarLength <= 0) healthBarLength = 0;

	//convert health to string
	playerHealth_text = ((int)playerHealth).ToString();

	if (playerHealth <= 25) 
	{
		Debug.Log("LOW HEALTH!");		//your code is here
	}
	if (playerHealth > 25) 
	{
		Debug.Log("WE ARE GOOD!");		//your code is here
	}
}

void OnGUI()
{
	GUI.Label(new Rect(30, 60, 100, 20), "Player Health");
	GUI.Label(new Rect(120, 60, 100, 20), playerHealth_text);

	GUI.DrawTexture (new Rect (30, 90, (int)healthBarLength, 20), healthBar);
}

}