- Home /
Question by
smirlianos · Aug 22, 2016 at 06:57 PM ·
animationcamerafpslerpfieldofview
Lerping Field Of View is buggy
I have ths script that is supposed to make the camera's FoV to change when you press the right mouse button and return to normal when you release it. The problem is that it never reaches neither 40 or 60 degrees completely, and after 1-2 presses, it stops working. Any help?
pragma strict
var cam : Camera; var weapon : Transform;
function Update () {
if(Input.GetMouseButtonDown(1))
{
weapon.animation.Play("GunAim");
CamFovChange(40);
}
if(Input.GetMouseButtonUp(1))
{
weapon.animation.Play("GunAimEnd");
CamFovChange(60);
}
}
function CamFovChange(amount : int) {
while(cam.fieldOfView != amount)
{
cam.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, amount, 10*Time.deltaTime);
yield;
}
}
Comment
Best Answer
Answer by Jessespike · Aug 22, 2016 at 07:09 PM
That's not really how lerp should be used. Actually it's not even needed here.
#pragma strict
var cam : Camera;
var weapon : Transform;
var targetFov : float = 60;
function Update () {
if(Input.GetMouseButtonDown(1))
{
weapon.animation.Play("GunAim");
targetFov = 40;
}
if(Input.GetMouseButtonUp(1))
{
weapon.animation.Play("GunAimEnd");
targetFov = 60;
}
cam.fieldOfView = Mathf.MoveTowards(cam.fieldOfView, targetFov, 10*Time.deltaTime);
}