Int not getting rounded data
I have a problem, I can't really explain but the "points" integer is higher than it should be. It's disregarding the rounded airtime when being added to the score.
Here is my code.
void Update () {
if(!wheel1.isGrounded && !wheel2.isGrounded && !wheel3.isGrounded && !wheel4.isGrounded){
in_air = true;
}
else if(wheel1.isGrounded && wheel2.isGrounded && wheel3.isGrounded && wheel4.isGrounded){
in_air = false;
}
if(in_air){
trick_name = "Basic Jump";
airtime++;
} else if(!in_air){
trick_name = "";
airtime = 0;
scores = 0;
}
scores = scores + Mathf.CeilToInt(airtime / 50);
points = points + scores;
scoreTxt.text = trick_name + " " + scores.ToString();
pointTxt.text = points.ToString();
}
How do you know this and how do you know it's specifically the rounding that's failing?
For one:
$$anonymous$$athf.CeilToInt(airtime / 50);
doesn't seem to be logically sound. Looking at the unity docs, it should take in a float, and CeilToInt returns the smallest integer >= the float that is sent in, but you're just sending in an integer (I assume "airtime" is an integer as you are doing an increment on it earlier in the code) so it really just returns the calculation of (airtime / 50) considering that's integer division and won't return a float. The method probably accepts the integer because it can cast an int to a float, but you aren't gaining anything from using the CeilToInt method.
If you want rounded data, I suggest changing airtime to a float and/or incrementing it by some float value (not using ++), then change the division to:
(airtime / 50f);
and that should return a float, which will then correctly be used by CeilToInt.
Your answer
Follow this Question
Related Questions
Cannot convert float to int? 1 Answer
Convert "Double" to "Float" or "Int"? 1 Answer
Float surprisingly turns to int or 0 1 Answer
Slowly increase or decrease between two int values? 0 Answers