:shock: Noob Crisis: Averted! (again) :shock:
I’m familiar with the Integer division problem from my introductory computer science courses. What I’m not familiar with is how to neutralize them in Unity. I’m using the following code for a modular health bar:
@script AddComponentMenu( "GUI/HP Meter" )
var yourX : int;
var yourY : int;
var yourWidth : int;
var yourHeight : int;
var meterBase : Texture2D;
var meterBar : Texture2D;
var me: player;
var meterBaseRect;
var meterBarRect;
var meterBaseHeight;
var meterBarHeight;
var meterLength;
var meterMaxLength;
function Start()
{
me = FindObjectOfType( player );
meterMaxLength = yourWidth;
meterLength = ( me.hp / me.maxhp * 1.0 ) * meterMaxLength;
print( "Player stats: " + me.hp + "/" + me.maxhp + " -- Evaluates to " + ( me.hp / me.maxhp * 1.0 ) );
//print( "meterLength: " + meterLength );
meterBaseHeight = yourHeight;
meterBarHeight = yourHeight;
meterBaseRect = Rect( yourX,yourY, meterMaxLength,meterBaseHeight );
meterBarRect = Rect( yourX,yourY, meterLength,meterBarHeight );
}
function OnGUI()
{
GUI.DrawTexture( meterBaseRect, meterBase );
GUI.DrawTexture( meterBarRect, meterBar );
}
function Update()
{
if( Input.GetKeyDown( KeyCode.D ) )
{
me.hp--;
meterLength = ( me.hp / me.maxhp * 1.0 ) * meterMaxLength;
print( "Player stats: " + me.hp + "/" + me.maxhp + " -- Evaluates to " + ( me.hp / me.maxhp * 1.0 ) );
//print( "meterLength: " + meterLength );
meterBarRect = Rect( yourX,yourY, meterLength,meterBarHeight );
}
}
The debug bar at the bottom of the screen shows:
As you can see, I’ve been trying to use the C+±style workaround of adding the double/float 1.0 to the calculation to try to get it to passively cast the integer division as a double division. While this could be easily solved by simply switching the integer values in the player script object to doubles in code, what tools exist to accomplish what I’m trying to here?