Camera zoom?

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!

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

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

this should fix it:

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);
    }

}
1 Like

You should use smooth damp instead of lerp:

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

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

or

zoomedIn = Input.GetButton("Fire2");