Search Unity

Scripting Field of view

Discussion in 'Scripting' started by alejobrainz, Sep 13, 2005.

  1. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    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);
    }
    }
     
  2. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    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!
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.
     
  4. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    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);
    }
    }
    }
     
  5. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    I think in C# this would translate to:

    Code (csharp):
    1. int zoomSpeed = 5;
    2. int minFOV = 20;
    3. int maxFOV = 100;
    4.  
    5. function Update ()
    6. {
    7.   // assign zoom value to a variable
    8.   float delta = Input.GetAxis ("Zoom") * -zoomSpeed;
    9.   if(Input.GetButton ("Zoom"))
    10.   {
    11.     // make sure the current FOV is within min/max values
    12.     if((Camera.main.fieldOfView + delta > minFOV)
    13.       (Camera.main.fieldOfView + delta < maxFOV))
    14.     {
    15.       // apply the change to the main camera
    16.       Camera.main.fieldOfView = Camera.current.fieldOfView + delta;
    17.       print ("FOV: " + Camera.main.fieldOfView);
    18.     }
    19.   }
    20. }
    21.  
    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?
     
    Tlyons likes this.
  6. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    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):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ClassName : MonoBehaviour {
    6.  
    7.   public float zoomSpeed = 5;
    8.   public float minFOV = 20;
    9.   public float maxFOV = 100;
    10.  
    11.   void Update ()
    12.   {
    13.     // assign zoom value to a variable
    14.     float delta = Input.GetAxis ("Zoom") * -zoomSpeed;
    15.     if(Input.GetButton ("Zoom"))
    16.     {
    17.       // make sure the current FOV is within min/max values
    18.       if((Camera.main.fieldOfView + delta > minFOV)
    19.         (Camera.main.fieldOfView + delta < maxFOV))
    20.       {
    21.         // apply the change to the main camera
    22.         Camera.main.fieldOfView = Camera.main.fieldOfView + delta;
    23.         Debug.Log ("FOV: " + Camera.main.fieldOfView);
    24.       }
    25.     }
    26.   }
    27. }
    28.  
    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.
     
  8. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    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.
     
  9. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    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);
    }
    }
    }