- Home /
Script to constantly change FOV?
I've only been using Unity for a few days (for class) and I'm trying to create a scene to simulate being drunk. One of the things I'm trying to implement to do this is a script on the main camera that will smoothly slide the Field of View back and forth between 60 and 120 degrees. I'm having little success so far, as my current script only ticks back and forth between 58 degrees and 60 degrees. Can anyone set me on the right path?
My script:
using UnityEngine;
using System.Collections;
public class FOValter : MonoBehaviour
{
public float currentFOV = 90f;
public float rateOfChange = 0.1f;
public float ticker = 0;
void Update()
{
if (ticker == 30)
{
if (currentFOV > 118 || currentFOV < 62)
{
rateOfChange *= -1;
}
currentFOV += rateOfChange;
Camera.main.fieldOfView = currentFOV;
ticker = 0;
}
ticker++;
}
}
Answer by robertbu · May 09, 2013 at 04:03 AM
Here is an approach.
public class FOValter : MonoBehaviour {
public float startFOV = 90f;
public float deltaFOV = 28f;
public float speed = 10f;
private float timer = 0f;
void Update()
{
//if (ticker == 30)
{
Camera.main.fieldOfView = startFOV + Mathf.Sin(timer * speed) * deltaFOV;
timer += Time.deltaTime;
}
}
}
'deltaFOV' is how far on either side of the startFOV it will reach.
Your answer
Follow this Question
Related Questions
Change field of view over time 1 Answer
Camera Zoom/custom projection having trouble. 1 Answer
Lerping Field Of View is buggy 1 Answer
How to make a smash bros-like camera 3 Answers
Zoom camera based on position between player an object 2 Answers