- Home /
camera zoom
so i have this script which i want to rotate around and zoom in and out of my player (Tornado). my distance determins how far away the camera is from the tornado so i want when i scroll my mouse the distance will change making me get closer and further from the tornado. here is my script (void update) is where im having trouble with changing distance
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX = 50.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
public float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
private void Start() {
camTransform = transform;
cam = Camera.main;
}
private void Update()
{
currentX += Input.GetAxis ("Mouse X");
currentY += Input.GetAxis ("Mouse Y");
currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
distance += Input.GetAxis ("Mouse ScrollWheel");
distance = Mathf.Clamp (distance, 0.0, 10.0);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0,0,-distance);
Quaternion rotation = Quaternion.Euler(currentY,currentX,0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt(lookAt.position);
}
}
Answer by Nixmortem · May 06, 2018 at 08:01 AM
I'm working on a RTS that uses the ScrollWheel to do the same type of thing. I think your problem is you don't have a speed to multiply the axis by. This is how mine works:
distance -= Input.GetAxis("Mouse ScrollWheel") * speed;
If you want to zoom in when you scroll down, use += instead of -=. The way mine is I have it so when the player scrolls down, the camera zooms out. Mine is also all in the Update method so I'm not sure if it will work quite the same way for you.