- Home /
Score count up to target score - works tooo fast
I was wondering how I would slow down the following.
I'm trying to make one of the old game scores that when you get some points it counts up to the total score or a second or so depending on the sizer of the number. I have this working but because the processing of unity and fps is so fast that it appears at the number instantly. But when I print out the score it counts up correctly.
What I did was just get the difference and then half it and increment it by that until it reaches the target score.
So if score was 0 and the player just got 50 pts the score would quickly count up to 50.
0, 25, 37, 43, 46, 48, 49, 50
The only problem I'm having is that it appears instantly. Are there any methods of slowing this sort of thing down? Or is there a better way to do this?
Answer by Thom Denick · Jan 22, 2011 at 04:58 PM
Here's the code I used in my project... It's in C#, but I think it's identical to JS here:
int score = 500000; float textBaseScoreIncreaseAnimationSpeed = 5.0f;
//Figure out what % of our total time our current frame occupies. //We do this by dividing the Time.deltaTime(time since last frame had been drawn) //By the amount of time we want our scroll-up to last. In this case, if our //deltaTime is 0.1, than we get a scoreMultiplier of 0.02. float scoreMultiplier = Time.deltaTime / textBaseScoreIncreaseAnimationSpeed;
//Simply multiply our total score against the new multiplier.
float scoreAdd = score * scoreMultiplier;
//Since we dealt with time, we had to use floats to calculate.
//This simply converts our number to an integer.
int scoreAddRound = Mathf.Ceil(scoreAdd);
Thanks Smorpheus, works a treat. Even more simple then the way I was doing it.
No problem. One of the advantages of explaining your own code is when you need to share. :)
Hey Smorpheus, I've been using your script and all working fine. I noticed I has sort of an irregular count up.
eg,
goes from 1, 5, 7, 9, 11, 15, 16, 18, 20, 22.
Is that because of the Time.delta that I get weird jumps if not it counts by 2? Is there a way to change the formula so it has a division by 2
eg 1 - 50
25 37 43 46 48 49 50 ? These are all converted to floor ints
Yeah this is really designed for counting up large numbers like multi-thousand point scores. Your jumps are happening because of Time.Delta I believe. Anyway, to get it to just count up in regular intervals just get rid of the Time.deltaTime and replace that with a flat float like 0.2f
Answer by code-sumit · Nov 16, 2014 at 12:52 PM
Try this :-
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FinalScores : MonoBehaviour
{
public Text ScoreText;
public float CountInterval = 0.05f, timer=0;
int Score = 500, i=0;
void Update()
{
timer+=Time.deltaTime;
if(i<Score && timer>=CountInterval)
{
i++;
ScoreText.text=i.ToString ();
timer=0;
}
}
}
Answer by zmar0519 · Jan 22, 2011 at 02:22 PM
you could, if you were trying to add score incrementally, you could use the x *Time.deltaTime. also, you could have each frame add to a dumb var (traditionally i) and when it reaches a point, you add score and set i to 0.
Answer by Jessy · Jan 22, 2011 at 01:54 PM
this URL is not working. can you please give some other reference?
Answer by user-11096 (google) · Apr 21, 2011 at 01:45 AM
Well i hope this works for you but you got to the game must check to see how fast you want things to go. In your function add this.
var seconds : int = Time.time;
var oddeven = (secodns %2)
if(oddeven)
{
Shoot(seconds)
}
also add a new function like shown, this function is used to find the seconds and saved time.
function Shoot(seconds)
{
if(seconds!=savedTime)
{
savedTime=seconds;
}
}
add a variable called
var savedTime = 0;
Well hope this script works for your, feel free to change the Shoot function to something what your really need to be like points time or what ever. But this is a example on saviing time in unity3d.
Your answer
Follow this Question
Related Questions
Scoring system 2 Answers
Trying to sync score on multiplayer LAN game 0 Answers
How do I keep the highest scoring value for a game session? 2 Answers
scoring sing guiText 2 Answers
BCE0019: 'GetComponent' is not a member of 'Object'. 3 Answers