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