Time.DeltaTime Bug
Hello everbody, I have a score (int) with Vector.Lerp and a Level Bar with Vector3.Lerp. My goal is to show them both in the Gameoverscreen after another. But if I try to enable the Level Bar after I check that the score has now been Displayed the Vector3.Lerp of the Level Bar Ignores Time.DeltaTime and just jumpes to his targetPosition.
My Code looks something Like this:
void Update () {
score=Vector3.Lerp(score,target_score,Time.deltaTime);//Calculates a smooth Score Display
score_text.text=(Mathf.RoundToInt(score.x)).ToString();
//I tried to disable the score_text when (score == target_score) and enable the Xp but Then the error appears
//Shows Xp running smooth
showxp = Vector3.Lerp (showxp, showTargetxP, Time.deltaTime);
FillBar.fillAmount = showxp.x / (Level * 100.0f);
if (XpAdd != 0) {
if (FillBar.fillAmount > 0.995f) {
Level++;
LevelText.text = "LEVEL " + Level;
LevelUp ();
}
}
}
void LevelUp(){
int LevelGoal = Level * 100 - BaseXp;
if (XpAdd < LevelGoal) {
showxp = new Vector3 (BaseXp, 0);
showTargetxP = new Vector3 (BaseXp + XpAdd, 0);
BaseXp = BaseXp + XpAdd;
XpAdd = 0;
} else {
showxp = new Vector3 (BaseXp, 0);
showTargetxP = new Vector3 (Level * 100, 0);
XpAdd = XpAdd - LevelGoal;
BaseXp = 0;
}
}
Thank you in advance
Hey! The Time.deltaTime only gives you the time it took to complete the last frame so at 60Hz framerate it would give you something like 0.01666. I didn't quite catch what you mean but i doubt that's what you want here. For a full time since game start you can use Time.realtimeSinceStartup or Time.timeSinceLevelLoad. Or if you need a timer you have to use a variable and add Time.deltaTime to it every frame. Hope this helps!
yes Helped a lot, my Lerp also looks cleaner now thank you
Answer by Deathdefy · Feb 27, 2018 at 01:53 PM
You need to iterate on Time.deltaTime. When lerping, 0 will return "a" and 1 will return "b". The issue is that Time.delta will always return the difference in time from this frame to the last. So as @JedBeryll said, 60 frames per second 0.01666. So you will never actually lerp from 0 -> 1.
So with below you will use "animationTime" to determine how long you want it to take to fully do your lerp. Keep in mind because this is in Update its just going to begin when your object turns on so you will probably need to add handling of when this counter should start(unless you want it to start how you already have it).
private Vector3 score;
private Vector3 target_score;
private Text score_text;
private Vector3 showxp;
private UnityEngine.UI.Image FillBar;
private Vector3 showTargetxP;
public int Level { get; private set; }
public int XpAdd { get; private set; }
public Text LevelText { get; private set; }
public int BaseXp { get; private set; }
private float animationTime = 3.0f;
private float totalTime = 0.0f;
void Update()
{
float lerp = totalTime / animationTime;
score = Vector3.Lerp(score, target_score, lerp);//Calculates a smooth Score Display
score_text.text = (Mathf.RoundToInt(score.x)).ToString();
totalTime += Time.deltaTime;
//I tried to disable the score_text when (score == target_score) and enable the Xp but Then the error appears
//Shows Xp running smooth
showxp = Vector3.Lerp(showxp, showTargetxP, lerp);
FillBar.fillAmount = showxp.x / (Level * 100.0f);
if (XpAdd != 0)
{
if (FillBar.fillAmount > 0.995f)
{
Level++;
LevelText.text = "LEVEL " + Level;
LevelUp();
}
}
}
void LevelUp()
{
int LevelGoal = Level * 100 - BaseXp;
if (XpAdd < LevelGoal)
{
showxp = new Vector3(BaseXp, 0);
showTargetxP = new Vector3(BaseXp + XpAdd, 0);
BaseXp = BaseXp + XpAdd;
XpAdd = 0;
}
else
{
showxp = new Vector3(BaseXp, 0);
showTargetxP = new Vector3(Level * 100, 0);
XpAdd = XpAdd - LevelGoal;
BaseXp = 0;
}
}
You're confusing the 2 common equations using Lerp. If the 2 points are fixed, the last value should move from 0 to 1. But the OP is using the other "move me 2% closer" method - notice how showxp is the first thing in the Lerp and also in front of the =. That gives a "fast-then-slow" motion. What they wrote works fine - something else must be wrong with the code.
You are correct, oversight on my end. I will adjust my answer to account for this when I have a moment
Thank you very much ! I found the Error, it was just a thinking mistake Have a nice day
Your answer
Follow this Question
Related Questions
Color.Lerp over HP 1 Answer
Move player distance forward over time. 0 Answers
Color.lerp for Spriterender is not smooth 1 Answer
i have no clue what is wrong with the script (help) 1 Answer
If statement being ignored 2 Answers