- Home /
Can't figure out how Time.deltaTime can works in this case
Hello,
I must say that I begin in Unity scripting. ( I did the Walker Boys tutorials which introduced me well ).
Here is the situation : When using Time.deltaTime with a Vector3.Lerp, it acts in a way and when using with Color.Lerp, it acts another way;
01- With Vector3.Lerp
var keyUpEnabled : boolean = true;
var keyUpPos : Transform;
var keyUpSpeed : float = 8.0; // speed for the key when getting up
// position for the key when up
function Update ()
{
KeyUpAnim ()
}
function KeyUpAnim () // up animation
{
if ( keyUpEnabled )
{
transform.position = Vector3.Lerp ( transform.position, keyUpPos.transform.position, keyUpSpeed * Time.deltaTime ); // up animation
if ( transform.position == keyUpPos.transform.position )
{
keyUpEnabled = false;
}
}
}
When looking at the doc for Vector3.Lerp, that says : "static function Lerp (from : Vector3, to : Vector3, t : float) : Vector3 " "t is clamped between [0...1]. When t = 0 returns from. When t = 1 returns to. When t = 0.5 returns the average of from and to. "
I checked Time.deltatime by printing it every frame and it stays around 0.015, so in this case t = 8 X 0.015 so 0.12. Since t is relatively constant and doesn't reach 1, the game object with this script should stay near the start position. But I can't figure out why : it makes a smooth movement from the start position to the keyUpPos position as if t was gently changing from 0 to 1.
02- With Color.Lerp
// Colors
var yellow : Color = Vector4 (1, 0.92, 0.016, 1);
var red : Color = Vector4 (1, 0, 0, 1);
// Toggles
var colorLerpEnabled : boolean = true;
function Update ()
{
ColorLerp ();
}
function ColorLerp ()
{
if ( colorLerpEnabled )
{
renderer.material.color = Color.Lerp ( yellow, red, 8*Time.time );
//yield WaitForSeconds ( 4) ;
if ( renderer.material.color == red )
{
colorLerpEnabled = false;
}
}
}
Contrary to the precedent exemple, in that case, the game object with this script attached got an oranged yellow color due to the constant value of t = 0.12. ( so according to plan ).
That makes no sens to me in the first exemple.
Thanks if you got any idea.
Answer by Bunny83 · Aug 17, 2012 at 07:33 PM
That's why the first example looks totally different (and i don't talk about that it uses Vector3 instead of Color). In the first example you use the current value as lerp start value and the second value is your "target" value. This is actually a trick where you can use lerp to get a smooth decelerated movement / fading.
The usual way how lerp is used can be seen in the answer of Khada.
If you want this "decelerated" fading, use it like in the first example:
renderer.material.color = Color.Lerp ( renderer.material.color, red, Time.deltaTime );
Note that you can't compare renderer.material.color to red because you will never reach the full color red with this method.
For example. When this line is executed C will be 90% of it's old value and 10% red. When it's executed again it will again take 90% of the old value, which is already a little bit red, and 10% red.
C = Color.Lerp (C, red, 0.1 );
It slowly get's closer to red, but it will never be red.
Lerp isn't doing something magic. It really just do this:
function Lerp(Start : Vector3, End : Vector3, t : float) : Vector3
{
t = Mathf.Clamp01(t); // keep t within 0 and 1
return Start*(1-t) + End*t;
}
Thank you for taking time to explain me! :) I didn't know about "the trick" used when using current value. Thank you again.
Answer by Khada · Aug 17, 2012 at 07:17 PM
Lerp your Vector3 like this:
Vector3 start;
Vector3 end;
float t = 0; //<- starts at 0
void Update()
{
Vector3 newPos = Vector3.Lerp(start, end, t);
t += Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Trying to set up a lerp position system for the camera and failing... 2 Answers
Stealth AI Script. If less than or equal to not working correctly. Any fixes ? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Unable to Color.Lerp in two directions (Solved) 1 Answer
Why is the "t" value clamped to [0, 1] in Lerp functions? 3 Answers