Math bug?

I don’t know where i’m missing but i can’t get the desired result…

hpPercent shouldn’t be 106,2?
If i use floats to the hps it works correctly but not thus… why?

Mmm is this in play mode?

That’s not a bug, that’s just the result of trying to use ints. You’re not going to get a float as a result when you’re using integer math. If you don’t want curHP or maxHP to be floats, use parseFloat in the calculation, since JS doesn’t have a direct way of casting ints to floats (although I think it might in Unity 3, but there’s nothing in the docs about how to do it). As an aside, there’s no reason to be calculating hpPercent every frame in Update; it should be done just once when either of those values changes.

–Eric

Yes…

thx man…

how do I use the method parseFloat in unity? I trying hpPercent = (parseFloat(curHP.ToString())/parseFloat(maxHP.ToString())*180; But the compiler is returning me a error…

EDIT:
Thxxx! I get it… C=

You don’t want a string, leave that out. Just parseFloat.

–Eric

hehe, i’m so noob --’
i saw this here, so i thought … xP
but if it’s not necessary to update hpPercent every frame, so where i’ll update? I need to see what’s the value of it because i’m making smooth damaging decrement:

...
if(Input.GetButtonDown("Jump"))GetDamage(15); // just for test..
...

function GetDamage(damage){
    for(a=damage;damage>0;damage--){
        curHP--;
        yield WaitForSeconds(0.009);
    }
}

Whenever you change curHP or maxHP, then you calculate hpPercent. You can make that a function:

function CalculateHPPercent () {
	hpPercent = (parseFloat(curHP)/maxHP)*180.0;
}

So in your GetDamage function, you’d do

curHP--;
calculateHPPercent();

Also, “yield WaitForSeconds(0.009);” won’t work right; see here.

–Eric

I see, it’s working properly on my windows, but i’ll fix that, thanks for the advices .

float(someInt) doesn’t work? Another reason to steer clear of “javascript”

Yeah, but there are a number of things that work in JS and not C#, so there’s no reason to be a language bigot.

–Eric