Field of view on device - not working [SOLVED]

Hi all
i need some input on why field of view is not updating on android device .
i am using fov to achieve zooming and it works perfectly in play mode on the editor
but its not working on android.

I am using the tutorial found here

And this is how i have adapted it into my project
I added a camera reff in script to have more control because i am working with 2 cameras active for vr.
other edits was to switch from screen.width/height to pixelwidth/height. etc…

Even when i try to have a native set up with 1 main active camera the zoom only works in editor and not on device.

Without any further knowledge to what might be causing the problem. i am left thinking its a device issue
maybe i need to add aspect ratio calculation in zoom script i tried and failed parts of it are commented out in the code

Am also thinking fov or main camera might be different on device and have no clue how to accesses them
Am also thinking maybe view-port data could be added in script but beyond basics i cant figure out where and how to add it effectively

before further making my code more messy and complex i decided to get some feed back from you all

THANKS all input are welcome and appreciated

var caml : Camera;
var camr : Camera;
var crosshairTexl : Texture2D; //crosshair images go here
var crosshairTexr : Texture2D; //crosshair images go here
var crosshairTexl1 : Texture2D; //crosshair images go here
var crosshairTexr1 : Texture2D; //crosshair images go here
var crosshairTexl1d : Texture2D; //crosshair images go here
var crosshairTexr1d : Texture2D; //crosshair images go here
var zoom : int = 2; //determines amount of zoom capable. Larger number means further zoomed in
var normal : int = 60; //determines the default view of the camera when not zoomed in
var smooth : float = 5; //smooth determines speed of transition between zoomed in and default state
public var zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
public var fieldOfView = 60f;
var positionl : Rect; //position of the crosshair image
var positionr : Rect; //position of the crosshair image
static var OriginalOn = true;

function Start(){
caml.fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);

}

function Update(){
//caml.fieldOfView = fieldOfView * (16f/9f) / (caml.pixelWidth  / caml.pixelHeight);
//camr.fieldOfView = fieldOfView * (16f/9f) / (camr.pixelWidth / camr.pixelHeight);

positionl = Rect((caml.pixelWidth - crosshairTexl.width) / 2, (caml.pixelHeight - crosshairTexl.height) /2, crosshairTexl.width, crosshairTexl.height); //determines width/height of our crosshair GUI texture
positionr = Rect((camr.pixelWidth*2.99f - crosshairTexr.width) / 2, (camr.pixelHeight+0.2 - crosshairTexr.height) /2, crosshairTexr.width, crosshairTexr.height); //determines width/height of our crosshair GUI texture
if(Input.GetButtonDown("Fire2"))//(Input.GetKeyDown("z"))
{        //This function toggles zoom capabilities with the Z key. If it's zoomed in, it will zoom out
zoomedIn = !zoomedIn;
}
if(zoomedIn == true){    //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.
crosshairTexl = crosshairTexl1; //crosshair images go here
crosshairTexr = crosshairTexr1; //crosshair images go here

GetComponent(Camera).fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
//camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);

}
else{
GetComponent(Camera).fieldOfView = Mathf.Lerp(caml.fieldOfView,normal,Time.deltaTime*smooth);
//camr.fieldOfView = Mathf.Lerp(camr.fieldOfView,normal,Time.deltaTime*smooth);

  crosshairTexl = crosshairTexl1d; //crosshair images go here
crosshairTexr = crosshairTexr1d; //crosshair images go here
}
}
function OnGUI()
{
if(OriginalOn == true)
{
GUI.DrawTexture(positionl, crosshairTexl); //"draws" crosshair texture
GUI.DrawTexture(positionr, crosshairTexr); //"draws" crosshair texture

//Cursor.visible = false; //disables cursor from being visible
}
}

Another way of zooming without fov on android?

I have the same problem… I think its a limitation of VR, the FOV is probably defined by your device and set of google cardboard glasses… after scanning that QR code, my guess is they fix the value in the engine.

Other hacky options would be…

  1. artificially move your camera closer to target… along direction of Camera.forward vector… but the FOV would look kinda wrong, and you’d probably get clipping issues… (unless you have fixed / constrained view direction)
  2. try finding all cameras in the scene and changing fov on all of them… maybe you can get around unity limitations
  3. add another scene camera, try setting it as a render to texture… see if altering fov on that still works… I bet it does. that might open some doors, but maybe some complex ones…

Thanks jonPQ
it seems plus able for google cardboard to readjust some device setting but in my case
that was not what was causing fov not to work.

moving the camera for zooming needs advanced coding to get it to behave like a camera zoom.
Constraints rotation at original source would need to be maintained at the new position.
rotation at the new location further down the z axis would have to act like a child of the camera at the original source.

prior to reading your reply i had already tried and failed with moving camera for zoom
there would also be a need to convert world space to local space to maintain actual z/forward direction for player gameobject.

if(caml.transform.position.z<20){
caml.transform.position = Vector3(caml.transform.position.x, caml.transform.position.y,caml.transform.position.z+2);

}

this scenario throws up more errors to be the natural/easy way to achieve zoom

in my case i only had 2 cameras in my scene and was already trying to adjust the fov on them.
i printed to console log active/main cameras and it prints out my only 2 cameras in scene as the active cameras extra confusion was every thing seems to work perfectly in the editor and 0 errors in log. i made sure i fixed all other unrelated issues but when i deploy on device it still did not work.

i did not get round to trying out render to texture settings but my guess would be it would work in editor and not on device
based on the series of tests/debugging i have done for fov using unity 5.

i checked out all this sites and nothing seems to work

so i fired up my old system and went through my old projects to see where i had implemented zoom before
using unity 4.

i found this

camera.fieldOfView=int;

The difference with this code was i was 100% it works because i still had the game this code was used in and the zoom works on the android device.

so my aim now was to use the zoom code exactly the way i had implemented it before
and IT WORKED!! :slight_smile:

for others having similar issues try these steps
MAKE SURE YOUR CAMERA DEPTH IS CORRECT AND IT IS NOT OVERLAPPED BY OTHER CAMERAS(play around with it -1/1/2)

WHEN DEBUGGING TRY SIMPLE INT RATHER THAN COMPLEX CALCULATIONS
instead of (fieldOfView = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);)
try (fieldOfView = 5;)

NOT GETTING ERRORS IN CONSOLE LOG DOES NOT MEAN EVERY THING IS ACCURATE
EXAMPLE

GetComponent(Camera).fieldOfView

will not show u errors but like me u might be needing something like this

GetComponent.<Camera>().fieldOfView

i would generalise my issues as a depreciated functions and conversion error
this is what i had before

if(zoomedIn == true){    //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.
//positionl=positionlz;
//positionr=positionrz;
   //crosshairTexl = crosshairTexl1; //crosshair images go here
   //crosshairTexr = crosshairTexr1; //crosshair images go here
// caml=Camera.current;
GetComponent(Camera).fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);

/////caml.fieldOfView = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
/////camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);


//GetComponent.<Camera>().main.fieldOfView = Mathf.Clamp(GetComponent.<Camera>().main.fieldOfView + 1,5,8);

//caml.fieldOfView =  Mathf.Clamp(caml.fieldOfView ,5,8);
//camr.fieldOfView =  Mathf.Clamp(camr.fieldOfView ,5,8);
//GetComponent(Camera).fieldOfView =  Mathf.Clamp(Camera.main.fieldOfView + 1,5,8);
}

AND THIS WAS WHAT I NEEDED AND WORKED FOR ME after taking the steps above i did resolve my issue the long painful way on my own

if(zoomedIn == true){    //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.
//positionl=positionlz;
//positionr=positionrz;
   //crosshairTexl = crosshairTexl1; //crosshair images go here
   //crosshairTexr = crosshairTexr1; //crosshair images go here
// caml=Camera.current;
//GetComponent(Camera).fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
GetComponent.<Camera>().fieldOfView--;
if (GetComponent.<Camera>().fieldOfView<10)
{
GetComponent.<Camera>().fieldOfView=10;
Debug.Log (GetComponent.<Camera>());
}
/////caml.fieldOfView = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
/////camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);


//GetComponent.<Camera>().main.fieldOfView = Mathf.Clamp(GetComponent.<Camera>().main.fieldOfView + 1,5,8);

//caml.fieldOfView =  Mathf.Clamp(caml.fieldOfView ,5,8);
//camr.fieldOfView =  Mathf.Clamp(camr.fieldOfView ,5,8);
//GetComponent(Camera).fieldOfView =  Mathf.Clamp(Camera.main.fieldOfView + 1,5,8);
}

for people working in vr my experince from this issue is that for fov to work u dont nessesarily have to reffrence your cameras / GetComponent.().fieldOfView / should work for all your active cameras.

so to summarise things GetComponent.<Camera>().fieldOfView is different from GetComponent(Camera).fieldOfView

Thanks again jonPQ for your input
appreciate

i did try to achieve zoom in c# and it did not work
again confusion comes from things working in editor but not on device
in this case i am referencing the cameras directly in the scene

my guess would be if i adapted what i have learnt i should be abale
to get it to work using GetComponent.<Camera>().fieldOfView

here is the c# for zoom that did not work on device but works in editor

using UnityEngine;
using System.Collections;

public class zoomcsharp : MonoBehaviour {
    //
    public Camera caml;
    public float zoomSpeed = 20f;
    public float minZoomFOV = 10f;
    public float maxZoomFOV = 60f;
    public bool zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
    void Update () {
        if (Input.GetButtonDown ("Fire2")) {//(Input.GetKeyDown("z"))//This function toggles zoom capabilities with the Z key. If it's zoomed in, it will zoom out

            zoomedIn = !zoomedIn;

        }

        if (zoomedIn == true) {

            caml.fieldOfView -= zoomSpeed/8;
            //Camera.main.fieldOfView -= zoomSpeed/8;
            if (caml.fieldOfView < minZoomFOV)
            {
                caml.fieldOfView = minZoomFOV;
            }

        }
        else
        {
            caml.fieldOfView = maxZoomFOV;
        }
    }

    public void ZoomIn()
    {
        caml.fieldOfView -= zoomSpeed/8;
        if (caml.fieldOfView < minZoomFOV)
        {
            caml.fieldOfView = minZoomFOV;
        }
    }
}

hope it helps others with zoom issues on android

Thanks for the detailed explanation :slight_smile:

Thanks for your input jonPQ
(detailed) :slight_smile: lol think am prepping up for tutor mode.
hope it was helpful
appreciate.

In c# the code is wrong i think:
GetComponent.().fieldOfView
The right is:
GetComponent().fieldOfView