Sniper zooming (toggle)

how do i make it so when i click "z" once, and don't hold it, the camera will change.

private var baseFOV : float;

function Start () { baseFOV = Camera.main.fieldOfView; }

function Update () { if (Input.GetKey("z")) Camera.main.fieldOfView = 10; else Camera.main.fieldOfView = baseFOV;

}

Thats my script and if i hold z it will zoom in but how do i make it toggle on/off. and then how do i make all that so when i Zoom In there is a .5 sec delay.

Just store a bool in your script to toggle it like so

private var baseFOV : float;
private var zoomToggle : bool;

function Start () { 
    baseFOV = Camera.main.fieldOfView; 
    zoomToggle = false;
}

function Update () { 
    if (Input.GetKey("z")) {
        if(zoomToggle) {
            zoomToggle = !zoomToggle;
            Camera.main.fieldOfView = 10; 
        }
        else {
            zoomToggle = !zoomToggle;
            Camera.main.fieldOfView = baseFOV;
        }
    }
}

You will have to be a bit more specific about you 0.5 second delay. Do you want the delay to be while zooming in, do you want it to be a delay between pressing the button then applying the new zoom FOV?

EDIT : Fixed the toggle, wasn't actually toggling.

To add the pause, you can yield, but you can't do that inside an Update loop, so will have to add another function to handle it.

function Zoom(float zoomValue) {
    yield WaitForSeconds(0.5);
    Camera.main.fieldOfView = zoomValue;
}

Replace the calls in the update loop that set the fov value with a call to the Zoom function and it should work.

use this

var camera1 : Camera;

var camera2 : Camera;

function Start () {

camera1.enabled = true;

camera2.enabled = false;

}

function Update () {

if (Input.GetKeyDown (“z”)){

  camera1.enabled = false; 

  camera2.enabled = true; 

}

if (Input.GetKeyDown (“z”)){

  camera1.enabled = true; 

  camera2.enabled = false;

}

}

i use a modified version

EDIT:
to make it so you have to press the button again to go back to normal view
use this:

var camera1 : Camera;

var camera2 : Camera;

function Start () {

camera1.enabled = true;

camera2.enabled = false;

}

function Update () {

if (Input.GetKeyDown (“z”)){

camera1.enabled = false;

camera2.enabled = true;
}

if (Input.GetKeyUp (“z”)){

camera1.enabled = true;

camera2.enabled = false;
}

}