So I am trying to make a little score that ticks up as you play, the longer you survive the more points you get. Pretty basic stuff… so basic that I’m confused why I’m having issues with it!
Basically, I know that I should multiply my score addition by Time.DeltaTime to keep it constant across all machines that run my game. Since Time.DeltaTime represents a float value, I have the amount of points I want to add to my score every frame as a float value instead of an int - But in my game I want my score to remain a whole number so it looks nicer. (Nobody wants to get 100.483038 points, that’s just ugly.)
So I was trying to use MathF.Round(pointsToBeAdded * Time.DeltaTime) to try to round up my number each frame… But then my score counts up to 4 and stays there constantly, which is odd to me. Without MathF.Round, my number ticks up like it should but still has the ugly decimal, with MathF.Round, it stops at 4 points!
What gives!? And what can I do to have a pretty looking score? Thanks!
The point here, Time.deltaTime returns values smaller then 1, so rounding it before addition will give you tons of toubles.
Best thing to do here would be to store the score as a float and only round it before displaying, like
At the start of a level, you can do something like:
float startTime = Time.time;
Then, whenever you want to calculate the score, it’s just:
int score = Time.time - startTime
This will make sure the score is always an int by means of truncating the decimal. (Of course, if you want to apply some sort of multiplier, you’ll probably want to do that before stuffing the value into the int.) I think this is a pretty simple solution, but it’s also sort of limited… It depends on what all you need.
fafase answer should be what you need you can also do this inline with fewer variables as
//where score is int score;
score = (int)(Time.time+0.5f);
the cast will simply trim the decimal off so first we want to incress the value by .5 such that 2.5 becomes 3 while 2.4 becomes 2.9 because
(int)2.9f = 2
Also since you are creating a score based on the number of seconds since X you could avoid a few pointless calls and perform the calculation elsewhere as opposed to on Update i.e.