- Home /
Revised; a fancy method of adding value to a score?
I put revised in the title because I've asked this same question before, and the answer I got, I thought would work. But after diving in, I'm left frustrated. (well technically the answer was sound, it just didn't give me the results I was after)
The look I'm going for is to have a value (score) get values added to it and have the score count up to that new value, hitting every number.
I've been playing around with Mathf.Lerp, as was suggested to me in the previous question, but they seem to only display certain numbers on its way to the new value.
So if I'm adding 25 to 100, and I'm using a lerp...I'll maybe see the numbers 110,113,117,122 - 125. Which isn't the look I'm going for. I want to see every number as the score races to its new value.
Anyone have any ideas, or have encountered this situation?
should the final number be reached in a specified amount of time or is the time taking to reach the answer flexible. If the time is fixed it might not be physically possible to show all numbers due to the number only being visibly changed on a frame change, which is restricted by the FPS?
Answer by Jessy · Dec 03, 2013 at 06:21 PM
You haven't explained why you don't "see" the numbers. If you want to increase by 1 every frame, then do this:
void Update(){
if (displayedScore != score) displayedScore++;
}
$$anonymous$$aybe I'm just trying to add to much value and have it lerp it too short of a time. I'm not asking this question the right way.
This is the code I have currently.
#pragma strict
var Start_Number: int = 0;
var End_Number: int = 25;
var current_Number: int = 0;
var Lerp_Slider = 0.0;
function Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
{
$$anonymous$$athlerp ();
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Q))
{
current_Number = 0;
}
}
function $$anonymous$$athlerp ()
{
var t: float = 0;
while (t < 1)
{
current_Number = $$anonymous$$athf.Lerp(current_Number, End_Number, Lerp_Slider + t);
t += .1;
yield WaitForSeconds (.1);
print (t);
}
}
$$anonymous$$aybe it's a FPS issue on my computer, but when I look over at the value as it goes up. It doesn't do it smoothly, it just jumps to values on it's way to the new score.
Have you tried Jessy's suggestion, I guess this would be the implementation:
var score : int = 100;
var displayedScore : int;
var scoreDiff : int;
function Start () {
displayedScore = score;
}
function Update () {
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Q)){
score += 25;
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W)){
score -= 25;
}
if(displayedScore != score){
scoreDiff = score-displayedScore;
displayedScore += scoreDiff/$$anonymous$$athf.Abs(scoreDiff);
}
}
function OnGUI(){
GUI.Label(Rect(0, 30, 100, 20), "Score: " + displayedScore);
}
Yes! That looks 100 times better, and more to what I was after! Now I need to go look up $$anonymous$$athf.Abs