I was trying to assing a zoom functionality via + and - keys, but i didnt mange to geti it right i am updating the FOV for the current camera, but the new FOV is not rendered. do I have to call a methos to refresh the camera?
This is my simple script:
function Update () {
//Zoom
if (Input.GetButton (“Zoom”)) {
Camera.current.fieldOfView = Camera.current.fieldOfView + Input.GetAxis (“Zoom”);
print (Camera.current.fieldOfView);
}
}
int zoomSpeed = 5;
int minFOV = 20;
int maxFOV = 100;
function Update ()
{
// assign zoom value to a variable
float delta = Input.GetAxis ("Zoom") * -zoomSpeed;
if(Input.GetButton ("Zoom"))
{
// make sure the current FOV is within min/max values
if((Camera.main.fieldOfView + delta > minFOV)
(Camera.main.fieldOfView + delta < maxFOV))
{
// apply the change to the main camera
Camera.main.fieldOfView = Camera.current.fieldOfView + delta;
print ("FOV: " + Camera.main.fieldOfView);
}
}
}
There it is in case anyone cares to have this version. I’m a little more familiar with C# so I thought I’d share.
Just out of curiosity, how are you taking advantage of changing the FOV? What kind of game are you making?
Also, what happens if you change line 16 to “Camera.main.fieldOfView = Camera.main.fieldOfView + delta;” instead? Does it still work?
There is still some missing c# syntax…
1: Add public class ClassName : MonoBehaviour { … } around the class members.
2: Add public in front of public member variables (ok… this one is not actually required, as the default is public)
3: function Update() {…} becomes void Update() {…}
4: Add boilerplate namespace includes at the top of the script.
using UnityEngine;
using System.Collections;
public class ClassName : MonoBehaviour {
public float zoomSpeed = 5;
public float minFOV = 20;
public float maxFOV = 100;
void Update ()
{
// assign zoom value to a variable
float delta = Input.GetAxis ("Zoom") * -zoomSpeed;
if(Input.GetButton ("Zoom"))
{
// make sure the current FOV is within min/max values
if((Camera.main.fieldOfView + delta > minFOV)
(Camera.main.fieldOfView + delta < maxFOV))
{
// apply the change to the main camera
Camera.main.fieldOfView = Camera.main.fieldOfView + delta;
Debug.Log ("FOV: " + Camera.main.fieldOfView);
}
}
}
}
I also fixed the error with the line still using Camera.current, and changed all ints to floats in order to minimise the number of int/float conversions.