Hello I tried to make zoom in and out script for my game but the problem is that it just rotates my camera and is not transforming my cam's position to the player. So please help! Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Zoom : MonoBehaviour { void Update() { if (Input.GetAxis("Mouse ScrollWheel") < 0) { GetComponent<Transform>().localPosition = new Vector3(transform.localPosition.x - 2f, transform.localPosition.y - 2f, transform.localPosition.z); transform.Rotate(2, 0, 0); } if (Input.GetAxis("Mouse ScrollWheel") > 0) { GetComponent<Transform>().localPosition = new Vector3(transform.localPosition.x + 2f, transform.localPosition.y +2f, transform.localPosition.z); transform.Rotate(-2, 0, 0); } } }
First of all, Every object in a Scene has a Transform so don't use GetComponent every time you want to access it, just use: Code (CSharp): transform.localPosition And your camera rotates because of: Code (CSharp): transform.Rotate in lines 13 and 18
Use fieldOfView property from the camera to increase / decrease zoom. https://docs.unity3d.com/ScriptReference/Camera-fieldOfView.html
Nah I tried but it seems to be kinda weird. I want to make it zoom by transforming it's position I don't want to mess with field of view.
Just like you can use transform.Rotate to rotate, you can also use transform.Translate to move an object.