Reputation: 193
I have a integer that holds the player's highscore and it is shown when the player dies in my game over panel. When the player gets a new highscore, I created a function to increase the int to make a cool highscore count up effect; it works, however it is not as fast as I'd hope it to be, especially if the player were to get a very high highscore, like for example : 400. Is there a way to make my highscore count up function much faster?
This is my code:
public int i;
public Text highScore;
void Update () {
if(playerDeath == false) {
i = 0;
}
else if (playerDeath == true) {
i++;
if (i >= PlayerPrefs.GetInt ("Highest", 0)) {
i = PlayerPrefs.GetInt ("Highest", 0);
}
highCoinText.text = "" + i.ToString ();
}
}
Upvotes: 0
Views: 1253
Reputation: 516
This is an ideal place to use a lerp (linear interpolation)
public float t = 0.0f; //renamed i to t, for convention (i is for loops, t is for lerping or easing function "time" variables
public Text highScore;
public float highScoreAnimationLength = 2.0f; //how long in seconds it should take to go from score 0 to the players final score
void Update () {
if(playerDeath == false) {
t = 0.0f;
}
else if (playerDeath == true) {
t = Mathf.MoveTowards(t, 1.0f, Time.deltaTime/highScoreAnimationLength); //move t closer to 1.0, we use Time.deltaTime to make it move in "realtime" rather than frame time, MoveTowards will always stop at the target value, so we never go over it.
int displayedScore = (int)Mathf.Lerp(0, highScore, t); //Here we use a lerp to calculate what number to display, we then cast it to an int to display it correctly (no decimals)
highCoinText.text = "" + displayedScore.ToString ();
}
}
Here we use 2 handy functions, MoveTowards and Lerp, they're similar and both very handy.
MoveTowards takes a number as it's first argument, a target as it's 2nd, and a "max distance" as it's third, it will move the first argument towards the target argument, but only add or subtract up to the third argument in order to reach it. MoveTowards will never go over the target. You can think of it as MoveTowards(from, to, amount)
Lerp takes similar arguments (Lerp(from, to, t) but behaves differently. It takes the same from and to arguments, but the third argument is where it should place it's result linearly. You can imagine a number line starting at "from" ending with "to" and the third argument "t" is how far to place it along that line, with 0 being exactly on "from", 1 being exactly on "to" and 0.5 being exactly in the middle.
Here we use movetowards to increment our internal variable "t", which we're using as a timer to tick from 0 to 1, we use Time.deltaTime, which is essentially the duration of the current frame in seconds, to seperate our calculation from our framerate, meaning the process should be practically identical at 10fps, or 60fps. All we are doing is moving "t" from 0 to 1, over "highScoreAnimationLength" (2) seconds.
We then use the result from t as our argument for Lerp, which means over those 2 seconds, we will linearly interpolate (in simpler terms, slide) between our initial score (0) and our target score (highScore).
The advantages of doing it this way are multiple:
Extra Credits: You can look into "easing functions" for more specific, fine tuned control over your animation. Lerp looks very stiff as it has no acceleration and deceleration, easing functions are essentially a way to Lerp but with a smoother motion. I've already made this fairly convoluted so I won't bog you down with even more info, but they're well worth looking into for extra polish.
Upvotes: 2
Reputation: 529
The reason it isn't going up very fast is because this code is in an Update
function, which means that it will be called every frame of the game. So, the number will increase once per frame, which means the lower the frames per second, the more time it will take to reach the highscore. For instance, if the game was running at 20 fps, and the highscore was 400, it would take 20 seconds to reach the highscore. This also means that the speed of the animation will vary from system to system. I would try incrementing the number by a larger amount to speed up the animation,
Upvotes: 0