When I press down the middle button the camera rotates. When I scroll the camera zooms in and out.
My camera rotates around the player correctly, but the zoom doesn’t work. I want to zoom based on the current rotation of the camera. I only edited the zooming in portion and it doesn’t work as expected at all… Zooming out works in the sense that it zooms properly… but it zooms only in one linear direction - not based on the camera rotation.
I have no idea what to do! ![]()
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
private Vector3 offset;
private Vector3 mouseOrigin;
private bool isRotating;
//public GameObject player;
public float turnSpeed;
public float zoomSpeed;
private Vector3 currentPos;
public GameObject PlayerObj;
public float cameraMoveSpeed = 20.0f;
void Update()
{
if (Input.GetMouseButtonDown(2))
{
mouseOrigin = Input.mousePosition;
isRotating = true;
}
if (!Input.GetMouseButton(2)) isRotating = false;
if (isRotating)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
transform.RotateAround(PlayerObj.transform.position, Vector3.right, -pos.y * turnSpeed); //changed Vector3.right to PlayerObj.transform.right
transform.RotateAround(PlayerObj.transform.position, Vector3.up, pos.x * turnSpeed);
}
float zoom = Input.GetAxis("Mouse ScrollWheel");
if (zoom > 0)
{
Quaternion rot = Camera.main.transform.rotation;
Vector3 zoomVector = rot * (new Vector3(0.0f, zoom, 0.0f)) - transform.position;
transform.position = new Vector3(transform.position.x + zoomVector.x, transform.position.y + zoomVector.y, transform.position.z + zoomVector.z);
}
if (zoom < 0)
{
transform.position = new Vector3(transform.position.x, transform.position.y + zoomSpeed, transform.position.z - zoomSpeed);
}
}
}