- Home /
How to do a fractional percentage
I'm looking to do a health bar and looking at the answer here: http://answers.unity3d.com/questions/237381/to-draw-a-health-bar.html
However, 'playerHeathBar = playerHealth / playerHealthMAX;' seems to just result in zero, both when I print it out and in a tiny bar on the screen that does not change when playerHealth is adjusted. It seems the fraction is being removed? I've been searching around for about fifteen minutes and trying variations to find some way to avoid that occuring and cannot find any. What's the method?
Answer by AlucardJay · Aug 22, 2012 at 07:12 AM
Here is the edited code again, although I have fixed the original answer (on the other question). Never realized the errors as there was no reply to that question, with 450+ views am surprised no-one else mentioned it !
var healthTexture : Texture2D; // this is the texture displayed in the GUI.Box
var playerHealth : float = 75.0; // use your health var here
var playerHealthMAX : float = 100.0; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage
function OnGUI()
{
playerHealthBar = playerHealth / playerHealthMAX; // playerHealth as a percentage
if (playerHealth > 0)
{
// GUI.Box(Rect((Screen.width/2) - 100, 10, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 110, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true); // much nicer looking display
}
}
Edit : Here is a direct copy-paste of the code I tested and is working for me. My texture is 512x52 pixels
var healthTexture : Texture2D; // this is the texture displayed in the GUI.Box
var playerHealth : float = 75.0; // use your health var here
var playerHealthMAX : float = 100.0; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage
function OnGUI()
{
playerHealthBar = playerHealth / playerHealthMAX; // playerHealth as a percentage
if (playerHealth > 0)
{
GUI.Box(Rect((Screen.width/2) - 100, 40, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 10, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
}
}
here is the script for the above picture :
#pragma strict
function Start () {
}
function Update () {
}
var healthTexture : Texture2D; // this is the texture displayed in the GUI.Box
var playerHealth : float = 75.0; // use your health var here
var playerHealthMAX : float = 100.0; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage
function OnGUI()
{
playerHealthBar = playerHealth / playerHealthMAX; // playerHealth as a percentage
if (playerHealth > 0)
{
GUI.Box(Rect((Screen.width/2) - 100, 10, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 40, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
playerHealthBar = 0.05;
GUI.Box(Rect((Screen.width/2) - 100, 80, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 110, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
}
}
Also, after looking at this script again, I wasn't happy with the application of the texture. I don't use Unity's GUI (I have made my own), but I played around with GUI.DrawTextureWithTexCoords.
http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTextureWithTexCoords.html
If you replace GUI.Box with :
GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 110, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
the display bar looks much nicer with a texture =]
I'm trying to figure what error you fixed - was it 'playerheath' in the previous code sample? I actually spotted that error at one point, but thought I had hit delete at an unfortunate moment and something else I'd added still made the code not work. Anyway, enough story, thanks for the edit! This appears to work now.
This was the bugged line :
playerHeathBar = playerHealth / playerHealth$$anonymous$$AX;;
This is the fix :
playerHealthBar = playerHealth / playerHealth$$anonymous$$AX;
missing an l and an extra ;
This was from my first unity game, I just used GUI.Box with different colours and it was enough for me. But looking back at the texture example, I wasn't happy so played around a little with it. Not perfect, as the image doesn't scale to the rect, rather shows a portion of the image, but this could be good if you had a flame graphic or a box that was shaded from red to green, then just the red part would show if the health was low. Where you take it is up to you! Have Fun =]
Your GUI.DrawTextureWithTexCoords is co$$anonymous$$g up with an error for me?
After messing around I found adding a 'var customGUISkin: GUISkin;' then before the GUI.box a 'GUI.skin = customGUISkin;' then hitting create in the project overview and making a GUI. Go into it and click on normal, then add a background to it (in this case, one which is 200pixels wide). Not quite perfect as the graphic doesn't shrink properly at around five health or less, but it looks nice.
I have just edited the answer to include a direct copy of the code I tested, however the only difference is the start Y value of the Rect (was 110, now 10. I had several different gui items when testing, that's why). SO not sure why it is not working for you. Again, I use my own made GUI, not Unity's so am really not familiar with gui style/skin etc. As long as the health calculation works, the display is up to personal preference. I agree, when the value drops below 5, the GUI.Box overlaps on itself, but the GUI.DrawTextureWithTexCoords looked fine.
Answer by ScroodgeM · Aug 22, 2012 at 04:32 AM
float playerHeathBar = (float)playerHealth / (float)playerHealthMAX
force to cast values to float. integers doesn't have fractional part
I don't understand? As shown in the linked original, all variables are created as float variables to begin with. Integers aren't being used. And I tried your line verbatim and it gave errors, wanting semi colons added.
Your answer
Follow this Question
Related Questions
When is Velocity calculated? 1 Answer
How to do an absolutely massive calculation? 0 Answers
Angle between vectors - calculation 2 Answers
How many cameras can i have? 1 Answer
Rad to degree 1 Answer