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?
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);
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.
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;
}
}
}
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.