Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera zoom?

Discussion in 'Scripting' started by Treasureman, Aug 7, 2014.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I'm making an fps game with camera zoom but my script doesn't work right

    Here's the script:

    var zoom : int = 40; //<span class="fdx72e" id="fdx72e_12">determines</span> 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 <span class="fdx72e" id="fdx72e_10">transition</span> between zoomed in and default state

    private var zoomedIn = false; //boolean that determines whether we are in zoomed in state or not

    function Update () {

    if(Input.GetButton("Fire2")){ //This function toggles zoom capabilities with Fire2. 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.

    camera.fieldOfView = Mathf.Lerp(camera.fieldOfView,zoom,Time.deltaTime*smooth);

    }

    else{

    camera.fieldOfView = Mathf.Lerp(camera.fieldOfView,normal,Time.deltaTime*smooth);

    }

    }

    Whenever i use it the camera shakes violently when i hold it in and will only zoom in when I let it go. Also how would i unfocus my camera when its zoomed in so that my fps character is blurry. That's not something I need but would hepful. Thanks!
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Your problem is because your asking if the key is constantly down. Which your then flipping to on off.

    this should fix it:

    Code (JavaScript):
    1. var zoom : int = 40; //<span class="fdx72e" id="fdx72e_12">determines</span> amount of zoom capable. Larger number means further zoomed in
    2. var normal : int = 60; //determines the default view of the camera when not zoomed in
    3. var smooth : float = 5; //smooth determines speed of <span class="fdx72e" id="fdx72e_10">transition</span> between zoomed in and default state
    4. private var zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
    5.  
    6. function Update () {
    7.     if(Input.GetButtonDown("Fire2")){
    8.         zoomedIn = !zoomedIn;
    9.     }
    10.  
    11.     if(zoomedIn == true) {
    12.         camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom,Time.deltaTime*smooth);
    13.     } else{
    14.         camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal,Time.deltaTime*smooth);
    15.     }
    16.  
    17. }
     
    FuzzyBalls likes this.
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    That's how I wanted it so that it wail zoom if held in
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Code (csharp):
    1. if(Input.GetButton("Fire2")){
    2.   zoomedIn = true;
    3. } else {
    4.   zoomedIn = false;
    5. }
    or

    Code (csharp):
    1. zoomedIn = Input.GetButton("Fire2");