Simple camera zoom function

Hi everyone.

I have a camera in my game which is always on the same angle and following a player.

So, if I would want the camera to zoom in or out with mouse wheel, I would change its y and z values correspondingly.

I tried to code it up(started with c# few days ago, bear with me) and came up with this:

using UnityEngine;
using System.Collections;

public class CameraControls : MonoBehaviour {

  public float zoomSpeed = 20;

  void Update () 

      float scroll = Input.GetAxis("Mouse ScrollWheel");
      if (scroll != 0.0f)
          camera.transform.position.y += scroll*zoomSpeed;
          camera.transform.position.z += scroll*zoomSpeed;

but that doesn't seem to be the way to change the positions, what am I doing wrong here? Thanks in advance.

In C# position is a property. That means by accessing position you get a copy of the Vector3.

Use could use


But normally to zoom in you just change the fov (field of view)

camera.fieldOfView += scroll*zoomSpeed;

or maybe:

camera.fieldOfView *= scroll*zoomSpeed;

ps. don't use `camera.transform` it's slower as `transform`