Hello. 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); } }
This is just a guess, as I haven't tried this myself, but you may need to declare a variable, which the camera FOV will adjust to. For example: var zoomedFOV = 50f; function Update() { //adjust the FOV variable zoomedFOV += Input.GetButton("Zoom"); Camera.current.fieldOfView = zoomedFOV; } I usually script in C#, so if my JS is wacky, I apologize. Let me know if this works or helps!
Try using Camera.main instead of Camera.current. current Camera should only be used when inside render callbacks. And yes, i just changed the script documentation to reflect this. Thanks.
Thanks! It works. This is the corresponding script (which reads off a button called zoom assigned to the + and - keys: //Zoom Script Component - Zerofractal Studio var zoomSpeed : int = 5; var minFOV : int = 20; var maxFOV : int = 100; function Update () { //Zoom var delta = Input.GetAxis ("Zoom") * -zoomSpeed; if (Input.GetButton ("Zoom")) { if ((Camera.main.fieldOfView + delta > minFOV) (Camera.main.fieldOfView + delta < maxFOV)) { Camera.main.fieldOfView = Camera.current.fieldOfView + delta; print ("FOV: " + Camera.main.fieldOfView); } } }
I think in C# this would translate to: Code (csharp): 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?
you are right about line 16. i wonder how it worked before As for the FOV. it is not really a game but a technical applciation. the WIP can be downloaded from: http://bogota.zerofractal.com/alejandro.gonzalez/VR Demo/stand-alone/cynergi.zip The + and - keys let you zoom in or out thanks!
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. Code (csharp): 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.
thanks for the edits! I'm still learning C# and very new to JS so I'm glad to learn the differences and the required pieces.
I added a time delta to have FPS independent speed. and minor touch ups: var Speed : int = 100; var minFOV : int = 20; var maxFOV : int = 100; function Update () { var delta = Input.GetAxis ("Zoom") * -Speed * Time.deltaTime; if (Input.GetButton ("Zoom")) { if ((Camera.main.fieldOfView + delta > minFOV) (Camera.main.fieldOfView + delta < maxFOV)) { Camera.main.fieldOfView += delta; print ("FOV: " + Camera.main.fieldOfView); } } }