Search Unity

Field of view on device - not working [SOLVED]

Discussion in 'Android' started by ppg, Nov 15, 2016.

  1. ppg

    ppg

    Joined:
    Apr 5, 2015
    Posts:
    42
    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
    http://xenosmashgames.com/crosshairs-and-camera-zoom-in-unity3d/

    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



    Code (JavaScript):
    1. var caml : Camera;
    2. var camr : Camera;
    3. var crosshairTexl : Texture2D; //crosshair images go here
    4. var crosshairTexr : Texture2D; //crosshair images go here
    5. var crosshairTexl1 : Texture2D; //crosshair images go here
    6. var crosshairTexr1 : Texture2D; //crosshair images go here
    7. var crosshairTexl1d : Texture2D; //crosshair images go here
    8. var crosshairTexr1d : Texture2D; //crosshair images go here
    9. var zoom : int = 2; //determines amount of zoom capable. Larger number means further zoomed in
    10. var normal : int = 60; //determines the default view of the camera when not zoomed in
    11. var smooth : float = 5; //smooth determines speed of transition between zoomed in and default state
    12. public var zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
    13. public var fieldOfView = 60f;
    14. var positionl : Rect; //position of the crosshair image
    15. var positionr : Rect; //position of the crosshair image
    16. static var OriginalOn = true;
    17.  
    18. function Start(){
    19. caml.fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
    20. camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);
    21.  
    22. }
    23.  
    24. function Update(){
    25. //caml.fieldOfView = fieldOfView * (16f/9f) / (caml.pixelWidth  / caml.pixelHeight);
    26. //camr.fieldOfView = fieldOfView * (16f/9f) / (camr.pixelWidth / camr.pixelHeight);
    27.  
    28. positionl = Rect((caml.pixelWidth - crosshairTexl.width) / 2, (caml.pixelHeight - crosshairTexl.height) /2, crosshairTexl.width, crosshairTexl.height); //determines width/height of our crosshair GUI texture
    29. 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
    30. if(Input.GetButtonDown("Fire2"))//(Input.GetKeyDown("z"))
    31. {        //This function toggles zoom capabilities with the Z key. If it's zoomed in, it will zoom out
    32. zoomedIn = !zoomedIn;
    33. }
    34. 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.
    35. crosshairTexl = crosshairTexl1; //crosshair images go here
    36. crosshairTexr = crosshairTexr1; //crosshair images go here
    37.  
    38. GetComponent(Camera).fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
    39. //camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);
    40.  
    41. }
    42. else{
    43. GetComponent(Camera).fieldOfView = Mathf.Lerp(caml.fieldOfView,normal,Time.deltaTime*smooth);
    44. //camr.fieldOfView = Mathf.Lerp(camr.fieldOfView,normal,Time.deltaTime*smooth);
    45.  
    46.   crosshairTexl = crosshairTexl1d; //crosshair images go here
    47. crosshairTexr = crosshairTexr1d; //crosshair images go here
    48. }
    49. }
    50. function OnGUI()
    51. {
    52. if(OriginalOn == true)
    53. {
    54. GUI.DrawTexture(positionl, crosshairTexl); //"draws" crosshair texture
    55. GUI.DrawTexture(positionr, crosshairTexr); //"draws" crosshair texture
    56.  
    57. //Cursor.visible = false; //disables cursor from being visible
    58. }
    59. }
     
  2. ppg

    ppg

    Joined:
    Apr 5, 2015
    Posts:
    42
    Another way of zooming without fov on android?
     
  3. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    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...
     
  4. ppg

    ppg

    Joined:
    Apr 5, 2015
    Posts:
    42
    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.

    Code (JavaScript):
    1. if(caml.transform.position.z<20){
    2. caml.transform.position = Vector3(caml.transform.position.x, caml.transform.position.y,caml.transform.position.z+2);
    3.  
    4. }
    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
    https://forum.unity3d.com/threads/camera-zoom-in-and-out.37193/
    http://answers.unity3d.com/questions/14381/main-camera-switch.html
    http://stackoverflow.com/questions/20573166/focal-length-to-field-of-view
    http://answers.unity3d.com/questions/419009/change-field-of-view-over-time.html

    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
    Code (JavaScript):
    1. 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!! :)

    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
    Code (JavaScript):
    1. (fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);)
    try
    Code (JavaScript):
    1. (fieldOfView  = 5;)
    NOT GETTING ERRORS IN CONSOLE LOG DOES NOT MEAN EVERY THING IS ACCURATE
    EXAMPLE
    Code (JavaScript):
    1. GetComponent(Camera).fieldOfView
    will not show u errors but like me u might be needing something like this
    Code (JavaScript):
    1. GetComponent.<Camera>().fieldOfView
    i would generalise my issues as a depreciated functions and conversion error
    this is what i had before
    Code (JavaScript):
    1.  
    2. 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.
    3. //positionl=positionlz;
    4. //positionr=positionrz;
    5.    //crosshairTexl = crosshairTexl1; //crosshair images go here
    6.    //crosshairTexr = crosshairTexr1; //crosshair images go here
    7. // caml=Camera.current;
    8. GetComponent(Camera).fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
    9.  
    10. /////caml.fieldOfView = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
    11. /////camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);
    12.  
    13.  
    14. //GetComponent.<Camera>().main.fieldOfView = Mathf.Clamp(GetComponent.<Camera>().main.fieldOfView + 1,5,8);
    15.  
    16. //caml.fieldOfView =  Mathf.Clamp(caml.fieldOfView ,5,8);
    17. //camr.fieldOfView =  Mathf.Clamp(camr.fieldOfView ,5,8);
    18. //GetComponent(Camera).fieldOfView =  Mathf.Clamp(Camera.main.fieldOfView + 1,5,8);
    19. }
    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
    Code (JavaScript):
    1.  
    2. 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.
    3. //positionl=positionlz;
    4. //positionr=positionrz;
    5.    //crosshairTexl = crosshairTexl1; //crosshair images go here
    6.    //crosshairTexr = crosshairTexr1; //crosshair images go here
    7. // caml=Camera.current;
    8. //GetComponent(Camera).fieldOfView  = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
    9. GetComponent.<Camera>().fieldOfView--;
    10. if (GetComponent.<Camera>().fieldOfView<10)
    11. {
    12. GetComponent.<Camera>().fieldOfView=10;
    13. Debug.Log (GetComponent.<Camera>());
    14. }
    15. /////caml.fieldOfView = Mathf.Lerp(caml.fieldOfView ,zoom,Time.deltaTime*smooth);
    16. /////camr.fieldOfView = Mathf.Lerp(camr.fieldOfView ,zoom,Time.deltaTime*smooth);
    17.  
    18.  
    19. //GetComponent.<Camera>().main.fieldOfView = Mathf.Clamp(GetComponent.<Camera>().main.fieldOfView + 1,5,8);
    20.  
    21. //caml.fieldOfView =  Mathf.Clamp(caml.fieldOfView ,5,8);
    22. //camr.fieldOfView =  Mathf.Clamp(camr.fieldOfView ,5,8);
    23. //GetComponent(Camera).fieldOfView =  Mathf.Clamp(Camera.main.fieldOfView + 1,5,8);
    24. }
    25.  
    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.<Camera>().fieldOfView / should work for all your active cameras.

    so to summarise things
    Code (JavaScript):
    1.  GetComponent.<Camera>().fieldOfView
    is different from
    Code (JavaScript):
    1.  GetComponent(Camera).fieldOfView
    Thanks again jonPQ for your input
    appreciate
     
  5. ppg

    ppg

    Joined:
    Apr 5, 2015
    Posts:
    42
    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
    Code (CSharp):
    1. GetComponent.<Camera>().fieldOfView
    here is the c# for zoom that did not work on device but works in editor

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class zoomcsharp : MonoBehaviour {
    5.     //
    6.     public Camera caml;
    7.     public float zoomSpeed = 20f;
    8.     public float minZoomFOV = 10f;
    9.     public float maxZoomFOV = 60f;
    10.     public bool zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
    11.     void Update () {
    12.         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
    13.  
    14.             zoomedIn = !zoomedIn;
    15.  
    16.         }
    17.  
    18.         if (zoomedIn == true) {
    19.  
    20.             caml.fieldOfView -= zoomSpeed/8;
    21.             //Camera.main.fieldOfView -= zoomSpeed/8;
    22.             if (caml.fieldOfView < minZoomFOV)
    23.             {
    24.                 caml.fieldOfView = minZoomFOV;
    25.             }
    26.  
    27.         }
    28.         else
    29.         {
    30.             caml.fieldOfView = maxZoomFOV;
    31.         }
    32.     }
    33.  
    34.     public void ZoomIn()
    35.     {
    36.         caml.fieldOfView -= zoomSpeed/8;
    37.         if (caml.fieldOfView < minZoomFOV)
    38.         {
    39.             caml.fieldOfView = minZoomFOV;
    40.         }
    41.     }
    42. }
    hope it helps others with zoom issues on android
     
  6. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    Thanks for the detailed explanation :)
     
  7. ppg

    ppg

    Joined:
    Apr 5, 2015
    Posts:
    42
    Thanks for your input jonPQ
    (detailed) :) lol think am prepping up for tutor mode.
    hope it was helpful
    appreciate.
     
  8. Netuddmeg

    Netuddmeg

    Joined:
    Mar 2, 2018
    Posts:
    14
    In c# the code is wrong i think:
    GetComponent.<Camera>().fieldOfView
    The right is:
    GetComponent<Camera>().fieldOfView