- Home /
Mathf.Lerp is just jumping to maximum, no 5.0 interpolation.
#pragma strict
private var LightState : String = "Off";
function Start(){
light.intensity = 0.0;
}
function Update () {
if(GC.hour > 17.5){
if(LightState == "Off"){
light.intensity = Mathf.Lerp(0.0,1.0,5.0); <---------------------
if(light.intensity == 1.0){
LightState = "On";
}
}
}
if(GC.hour > 5.5 && GC.hour < 15.5){
if(LightState == "On"){
light.intensity = Mathf.Lerp(1.0,0.0,5.0); <---------------------
if(light.intensity == 0.0){
LightState = "Off";
}
}
}
}
What gives? It must be something I don't know about as I can't be much different from the script reference. I thought this was a simple function but it's not performing as expected. ~Confused~
Answer by Dave-Carlile · Feb 20, 2013 at 01:41 PM
From the documentation.
The t
parameter is what you use to control the resulting value. t
always varies between 0 and 1. It returns a value that's t
percent between the from
and to
values that you pass in. For example, when t
is 0, the function returns the from
value. When t
is 1, it returns the to
value. When t
is 0.5 it returns a value halfway between from
and to
.
You'll note that the documentation states that t
is clamped between 0 and 1. So you when you pass in 5, the function converts it to 1, and returns the to
value.
Oh damn, interpreted that incorrectly. I thought it was just simply interpolating between 0 and 1 and that whatever you fed it would be converted into increments. Ie 5/1 = increments of 0.2, 10/1 increments of 0.1
So how would I create a 5 second timer? 1/(Time.deltaTime * 5.0) ?
You're kind of on the right track, but that formula will always return more or less the same value since Time.deltaTime
is generally the same value if you have a constant frame rate. Try something like this (I don't do much with UnityScript/Javascript, so this may not be comletely valid syntax)...
var timer : float; // declare this at the top
var totalTime : float = 5.0;
function Update()
{
// add elapsed time to the timer
timer += Time.deltaTime;
// what percentage are we to the total time?
// this will be a value between 0 and 1
var t : float = timer / totalTime;
// this will lerp between 0 and 1 over totalTime seconds
var value : float = $$anonymous$$athf.Lerp(0, 1, t);
}
You might also look at iTween in the asset store. I've not used it, but from what everyone says it handles things like this in a very elegant manner and eli$$anonymous$$ates a lot of ugly code like this.
Your answer
Follow this Question
Related Questions
Casting-related error: "^ cannot be applied to double and int" 1 Answer
Mathf.Lerp won't stop snapping 2 Answers
NetworkTransform not interpolating? 0 Answers
AngryBots like character rotation system 1 Answer
Problem with slerp 0 Answers