Could someone write me a 'zoom' script?

Could someone to write me a script to make a gun zoom in when you hold down Z then when you release Z it zooms back out.What I need is a machine gun zoom,the kind on halo.

Plus, I need to know what kind of script I would write it in and what object the script would be in.

I would really appreciate any help I can get,Thanks.

You want something like this:

function Update()
{
  if (Input.GetKeyDown("z"))
    Camera.main.fieldOfView = 10;
  if (Input.GetKeyUp("z"))
    Camera.main.fieldOfView = 90;
}

If you want that zoom effect to interpolate over, say, a littel over a second, that's also easy

var fov = 90;
function Update()
{
  if (Input.GetKeyDown("z"))
    fov = 10;
  if (Input.GetKeyUp("z"))
    fov = 90;
  if (Camera.main.fieldOfView < fov)
    Camera.main.fieldOfView++;
  if (Camera.main.fieldOfView > fov)
    Camera.main.fieldOfView--;
}

There's ways to adjust the speed, let me know if you need that.

Not a spoon feed community, but if you ask more of a specific question people are more than willing to help. Ex for when you press z if(Input.GetKeyDown("z") { //zoom camera } You need to make a nested if statment if you know what that means.

-Mike G.

Hey you can use the following script if you want to zoom in 2d . this is pinch to zoom.

float previousDistance;
float zoomSpeed = 8.5f;

float minZoomOut = 1;
float maxZoomOut = 15;

GameObject selectedObject;

void Update()
{
  
    if (Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began))
    {

        // calibrate the distance value;
        previousDistance = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);

    }
    else if (Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
    {
        float distance;
        Vector2 touch1 = Input.GetTouch(0).position;
        Vector2 touch2 = Input.GetTouch(1).position;

        distance = Vector2.Distance(touch1, touch2);

        //camera based on pinch/zoom
        float PinchAmount = (previousDistance - distance) * zoomSpeed * Time.deltaTime;

        if (Camera.main.orthographic)
        {
            //Debug.Log("PinchAmount Val is "+ PinchAmount);
         
            Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + (PinchAmount * 0.05f), minZoomOut, maxZoomOut);
            
        }

        else
            Camera.main.transform.Translate(0, 0, PinchAmount);

        previousDistance = distance;
    }
}