- Home /
C# Adding and counting up scores with Mathf.Lerp
Hello. I'm seriouisly spending too much time solving this and still stuck in here...
my project is about obtaining land as much as you can and the player has only 3 chances.
everytime the player gain land I want to count up from the last score to current score(last score + land I just obtained)
I'm using Mathf.Lerp and here it is.
private float land; //the score from each tries
private float totalScore; //the score combined
totalScore += Mathf.Lerp (0, land, Time.deltaTime * 0.5f);
slider.value = _totalScore; //a slider that shows current score.
Q01:
I put the Lerp method in Update() Function and it didn't work.
(totalScore kept going up and didn't stop)
if I put it in separated function, Lerp works only once showing very small value.
Q02:
after 1st try, float "land(ths land I just obtain)" is not the value that this Lerp should end with.
this Lerp shoud start from the last value to current value.
how should I set from & to values in Lerp insted of 0 & land?
someone please help me out of this :) thank you for reading anyway!
Answer by NoseKills · Mar 28, 2015 at 08:14 PM
Did you read how Matf.Lerp works and what the parameters mean? It simply returns a value between the first two parameters based on the third parametes. If the third parameter is 0, it returns the first parameter. If third parameter is 0.5f, it returns the value that sits at the halfway point between the first two parameters.
You are feeding in Time.deltaTime *0.5f
which means that in every update you increase totalScore by some value that's (depending on framerate) a few percent from 0 towards the value of land
. Actually the value you add is Time.deltaTime *0.5f * land
since your first parameter is 0.
What you probably want to do is store the currently shown score and the new score whenever you get more points and set a timer float to 0. Then in update Lerp it like
float pointAnimDurationSec = 2f;
float pointAnimTimer = 0f;
// var for storing the actual current score.
// end point of the Lerp
float currentScore = 0;
// var for storing the score just before points were last added
// start point of the Lerp
float savedDisplayedScore = 0;
// a variable for the "animated" score you should show in the UI.
// We can't put the result of Lerp into
// the vars above because that would mess up the result of the next Lerp
float displayedScore = 0;
// ^^ Added the word "displayed" because neither
// of these mark how many points th player really has.
// Afterall you animate the score just to make it pretty
void AddPoints(float points)
{
// A
// what if you get more points before last points finished animating ?
// start the animation again but from the score that was already being shown
// --> no sudden jump in score animation
savedDisplayedScore = displayedScore;
// B
// the player instantly has these points so nothng gets
// messed up if e.g. level ends before score animation finishes
currentScore += points;
// Lerp gets a new end point
pointAnimTimer = 0f;
}
void Update ()
{
pointAnimTimer += Time.deltaTime;
float prcComplete = pointAnimTimer / pointAnimDurationSec;
// don't modify the start and end values here
// prcComplete will grow linearly but if you change the start/end points
// it will add a cumulating error
displayedScore = Mathf.Lerp(savedDisplayedScore, currentScore, prcComplete);
}
With this code animating the new points always takes 2 seconds. Add a miltiplier to Time.deltaTime to get a slower or faster animation
hey thank you I understand how $$anonymous$$athf.Lerp works! :)
thanks to your "+= Time.deltaTime" solution, now the score counts up fine but it starts from the value I just make.
(because I don't understand your SavedDisplayedScore part)
I' been studying C# for 5 month and digging a book but it's not easy :(
I set total score with += and used in Lerp like below.
float score; //score I get on each tries.
float totalScore += score; //to pile up scores
float timerFloat += Time.deltaTime;
_totalScore = $$anonymous$$athf.Lerp (score, totalScore, timerFloat);
Like you said, I should store totalScore(before I get new score and add it) and use it as the first parameter in $$anonymous$$athf.Lerp.
but I can't think of right way to do so.. more explanation would be truly appreciated :)
$$anonymous$$odified my answer and added comments. I named the variables like that to signify that those variables are for animation purposes only. So you should never save those anywhere.. otherwise you might for example save a smaller score just because the animation hadn't finished when the level ended.
It's always a good thing to keep UI variables and logic separate from your game's core logic. From the game logic's perspective currentScore
is the only relevant variable. Don't use it for anything else.
Also, there was for example the issue of what happens when you get more points when the score is still animating the previous addition. Do you instantly jump to the new lerp start score and animate from there to actual score or do you just increase the target score so there's no jump in the score... I did the latter.
$$anonymous$$ostly a few issues that you possibly didn't think about yet because you didn't get that far
wow I just can't thank you enough for this. I'll try it and let you know how it worked!
and I wasn't even aware of the issue you mentioned and I think second one(no jumping) is much better too.
Your answer
Follow this Question
Related Questions
How to Lerp for a number to get BIGGER instead of smaller? 1 Answer
how to use time on lerp 2 Answers
Continuing movement after animaton on GetButtonUp 0 Answers
Mathf.Lerp happens instantly 2 Answers