Camera zoom?

I’m making an fps game and I’m having trouble with aiming. I want a script (JavaScript) so that when Fire2 is held down then the camera will zoom in

Here’s my script right now:

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 try to zoom the camera shakes violently and then zooms in when i let right Fire2 go. Can someone help? Thanks!

Well, you may try to have a look at this thread:

The topic is surprisingly close…

1 Like

You need to use Input.GetButtonDown

it shakes violently because GetButton fires every frame that the button is pressed so zoomedIn is getting toggled each frame. GetButtonDown only fires when you first press the button.

@naked_chicken , it was already explained in the other thread.