- Home /
 
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.
Answer by Bunny83 · Feb 26, 2011 at 01:28 PM
In C# position is a property. That means by accessing position you get a copy of the Vector3.
 Use could use
transform.Translate(0,scroll*zoomSpeed,scroll*zoomSpeed,Space.World);
 
               
               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
That clears it up, thanks. I already did it with fieldofview, but I didn't like it that way and as I have a fixed camera angle started working on this one.
Your answer
 
             Follow this Question
Related Questions
Lock Camera to be within a box type area? 1 Answer
Finding the limits of transform coordinates 1 Answer
Changeing Camera Position 1 Answer
How To Slow Camera Position Transform? 3 Answers