# Camera zoom? (546195)

**URL:** https://discussions.unity.com/t/camera-zoom-546195/546195
**Category:** Camera
**Tags:** Scripting
**Created:** [August 7, 2014, 1:14pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195 "2014-08-07T13:14:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Treasureman](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/treasureman/32/457929_2.png) [@Treasureman](https://discussions.unity.com/u/Treasureman)
#### Post date: [August 7, 2014, 1:14pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195/1 "2014-08-07T13:14:09Z")

</div>

I’m making an fps game with camera zoom but my script doesn’t work right

Here’s the script:

var zoom : int = 40; //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

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!

---

<div class="post-metadata">

### Author: ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)
#### Post date: [August 7, 2014, 1:42pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195/2 "2014-08-07T13:42:31Z")

</div>

Please use code tags to make the code readable in the forum:

> **[Using code tags properly](https://discussions.unity.com/threads/using-code-tags-properly.143875/)**
>
> If you see someone who needs to know about code tags, please link them to this post: Please use code tags when posting code. You can "tag" your code by typing around your code. There is no overt "Code" tag button that automatically tags a...

---

<div class="post-metadata">

### Author: ![smitchell](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/smitchell/32/467382_2.png) [@smitchell](https://discussions.unity.com/u/smitchell)
#### Post date: [August 7, 2014, 1:54pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195/3 "2014-08-07T13:54:17Z")

</div>

Your problem is because your asking if the key is constantly down. Which your then flipping to on off.

this should fix it:

```javascript
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.GetButtonDown("Fire2")){
        zoomedIn = !zoomedIn;
    }

    if(zoomedIn == true) {
        camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom,Time.deltaTime*smooth);
    } else{
        camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal,Time.deltaTime*smooth);
    }

}

```

---

<div class="post-metadata">

### Author: ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)
#### Post date: [August 7, 2014, 1:56pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195/4 "2014-08-07T13:56:52Z")

</div>

You should use smooth damp instead of lerp:

> **[Unity - Scripting API: Mathf.SmoothDamp](https://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html)**

---

<div class="post-metadata">

### Author: ![Treasureman](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/treasureman/32/457929_2.png) [@Treasureman](https://discussions.unity.com/u/Treasureman)
#### Post date: [August 7, 2014, 4:51pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195/5 "2014-08-07T16:51:13Z")

</div>

> [@smitchell](#):
>
> Your problem is because your asking if the key is constantly down. Which your then flipping to on off.
> 
> this should fix it:
> 
> ```javascript
> 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.GetButtonDown("Fire2")){
> zoomedIn = !zoomedIn;
> }
> 
> if(zoomedIn == true) {
> camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom,Time.deltaTime*smooth);
> } else{
> camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal,Time.deltaTime*smooth);
> }
> 
> }
> 
> ```

That’s how I wanted it so that it wail zoom if held in

---

<div class="post-metadata">

### Author: ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)
#### Post date: [August 7, 2014, 4:58pm UTC](https://discussions.unity.com/t/camera-zoom-546195/546195/6 "2014-08-07T16:58:48Z")

</div>

```auto
if(Input.GetButton("Fire2")){
  zoomedIn = true;
} else {
  zoomedIn = false;
}

```

or

```auto
zoomedIn = Input.GetButton("Fire2");

```
