- Home /
Question by
ge64 · Apr 02 at 05:54 PM ·
field of viewuntiy3d
Field Of View changing on click, not automatically
I'm trying to increase the Field of View on a player object when their "boost" is active and then decrease back to the default after a second. I'm using some code from a tutorial to adjust the field of view back and forth on click and it works exactly as expected, but I want it to return to normal FOV after a second or two. I'm new to C# and Unity so I can't seem to find the fault in the logic that is preventing the script from working.
private float boostRate = 1.5f;
private float nextBoost;
public int zoom = 75;
public int normal = 60;
public float smooth = 15;
public bool isZoomed = false;
void Update() {
//What works
if(Input.GetKeyDown(KeyCode.LeftShift)) {
isZoomed = !isZoomed;
}
if (isZoomed) {
ZoomOut();
}
else {
ZoomBack();
}
//What I want to work
if(Input.GetKeyDown(KeyCode.LeftShift) && Time.time > nextBoost) {
nextBoost = Time.time + boostRate;
ZoomOut();
Invoke("ZoomBack", 2.0f);
}
}
void ZoomOut() {
GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, zoom, Time.deltaTime * smooth);
}
void ZoomBack() {
GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, normal, Time.deltaTime * smooth);
}
Comment
Best Answer
Answer by Hellium · Apr 02 at 07:06 PM
void Update() {
if (isZoomed) {
ZoomOut();
}
else {
ZoomBack();
}
if(Input.GetKeyDown(KeyCode.LeftShift) && Time.time > nextBoost && !isZoomed) {
nextBoost = Time.time + boostRate;
isZoomed = true;
Invoke(nameof(StartZoomBack), 2f);
}
}
void StartZoomBack() {
isZoomed = false;
}