Hello. I am making a game with a gun, and so far it’s going good; with exception of one error. I have a line of code going saying if this float is less than float canShoot = false. That works, however my code is saying at 1 canShoot = false, not at 0. Here is the code:
var ammo : float = 30;
var lastAmmo : float = 0;
function Shoot () {
if(ammo > lastAmmo)
canShoot = true;
else
canShoot = false;
if(Input.GetMouseButtonDown(0) && canShoot == true)
animation.Play("Shoot");
}
Why use float? Use int instead and your problem will be solved, except if at some point, you need 2.76 ammoes…
With floating point imprecision, even a statement like
var number : float = 1f;
number -= 1f;
Debug.Log(number > 0f);
Might return true;
Also, even if you use floats, a statement like 1f > 0f WILL return true. It is more likely that you made a mistake while debugging or you’re changing values at runtime that results in unexpected results.