- Home /
Mathf.Lerp Question
Hi, a really noob question, i got this code from the unity manual
// Fades from minimum to maximum in one second
var minimum = 10.0; var maximum = 20.0;
function Update () { transform.position = Vector3(Mathf.Lerp(minimum, maximum, Time.time), 0, 0); }
Now how would u make it instead of one second say 3 or 4? I tried Time.deltaTime * 4 but the results are weird.
EDITED: My goal is to move a gun (FPS) to the center of the screen when we zoom in roughly at the same speed as the cam FOV decreases then return it to the original position.
I have this code so far:
function LateUpdate () { if (Input.GetButtonDown ("Fire2")){ pressing = true; } if (Input.GetButtonUp ("Fire2")){ pressing = false; } // Calculate the desired distance if (pressing){ camera1.fieldOfView = Mathf.Lerp(camera1.fieldOfView, zoomFOV, Time.deltaTime * lerpSpeed); gun.localPosition = Vector3(Mathf.Lerp(minimum, maximum, Time.time ), -0.2390689, 0.4984837); DOF.enabled = true; } else if (!pressing) { camera1.fieldOfView = Mathf.Lerp(camera1.fieldOfView, normalFOV, Time.deltaTime * lerpSpeed);
gun.localPosition = Vector3(Mathf.Lerp(maximum, minimum, Time.time ), -0.2390689, 0.4984837);
DOF.enabled = false;
}
better understanding lerp and deltatime: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html?sort=oldest
Answer by aldonaletto · Oct 08, 2011 at 11:50 PM
This is a bad example, because it uses Time.time - it starts counting when the game begins, and never returns to zero, so you will only see this working once. It's better to do something like this:
var minimum = 10.0; var maximum = 20.0; var duration: float = 4; // duration in seconds var timer: float = 0; // set this to zero to repeat the cycle
function Update () {
transform.position = Vector3(Mathf.Lerp(minimum, maximum, timer), 0, 0);
timer += Time.deltaTime/time; // add time elapsed since last Update
}
EDITED: You can do that in a simpler manner. Since Mathf.Lerp(from, to, t) returns a value between from and to proportional to t, you can increment t from 0 to 1 when Fire2 is pressed, and return to 0 when it's released. This can be done by adding or subtracting Time.deltaTime/duration from the variable t. The variable t is clamped between 0 and 1, so when you press or release Fire2 t always take the same time to go or return.
var t: float = 0; var duration: float = 3;
function LateUpdate () { if (Input.GetButton("Fire2")){ // if fire2 pressed... DOF.enabled = true; // enable DOF t += Time.deltaTime/duration; // t goes towards 1 } else { // if fire2 not pressed... DOF.enabled = false; // disable DOF t -= Time.deltaTime/duration; // t goes towards 0 } t = Mathf.Clamp(t, 0, 1); // keep t between 0 and 1 camera1.fieldOfView = Mathf.Lerp(normalFOV, zoomFOV, t); gun.localPosition.x = Mathf.Lerp(minimum, maximum, t); }
Hi, thank you 4 the quick reply. $$anonymous$$y goal is to move a gun (FPS) to the center of the screen when we zoom in roughly at the same speed as the cam FOV decreases then return it to the original position.
I have this code so far:
function LateUpdate () {
if (Input.GetButtonDown ("Fire2")){ pressing = true; } if (Input.GetButtonUp ("Fire2")){ pressing = false; } // Calculate the desired distance if (pressing){ camera1.fieldOfView = $$anonymous$$athf.Lerp(camera1.fieldOfView, zoomFOV, Time.deltaTime * lerpSpeed);
gun.localPosition = Vector3($$anonymous$$athf.Lerp($$anonymous$$imum, maximum, Time.time ), -0.2390689, 0.4984837);
DOF.enabled = true;
} else if (!pressing) { camera1.fieldOfView = $$anonymous$$athf.Lerp(camera1.fieldOfView, normalFOV, Time.deltaTime * lerpSpeed);
gun.localPosition = Vector3($$anonymous$$athf.Lerp(maximum, $$anonymous$$imum, Time.time ), -0.2390689, 0.4984837); DOF.enabled = false; }
@Testing, simply EDIT your original question and put all the code, etc, in there.
Sheesh, I'd wish they'd fix the Lerp examples already, they're useless - or have been put there on purpose to confuse newbies :-(
Yes, the examples can only be understood by those who know already how it works.
Answer by Patel-Sagar · Feb 04, 2013 at 12:49 PM
I do it this simple way..
float temp; bool lerpStarted; float noOfSeconds;
void callThisToStartLerp() { temp = Time.time; lerpStarted = true; }
void Update() { if(lerpStarted) { transform.position = Vector3(Mathf.Lerp(minimum, maximum, (Time.time - temp)/noOfSeconds), 0, 0); } }
In this Way, You can use lerp for any no. of seconds defined in noOfSeconds. and you can use yield to false the bool var lerpStarted.
Your answer
Follow this Question
Related Questions
Trying to lerp in different states in a switch statement 0 Answers
Move Transform to Target in X seconds 3 Answers
Jump with mathf.lerp problem 2 Answers
how to use time on lerp 2 Answers