Hey guys… So I’ve already implemented most of the functionality here…
I just don’t understand how to make the camera follow my character without the zoom working incorrectly.
Camera following my character:
When I tried to adopt the traditional “camera follow” method (what I’ve commented out) the zoom no longer worked properly. If I execute the line in update then my offset stays the same. Of course I understand I need to update this when I zoom but I’m not sure exactly how to incorporate it with the below addition I want to add to my script as well…
Zoom Based on rotate:
Well, essentially I use the middle mouse button to rotate around the camera. This works just how I want it. But my problem is that the camera zoom should be a vector that is pointing away from where it is currently facing.
So something like the negative value of this.transform.rotation???
Any help is immensely appreciated! Also, for any readers/future readers, feel free to use some of this code for your own camera.
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;
//void Start()
//{
// offset = transform.position - player.transform.position;
//}
void Update()
{
//currentPos = transform.position + offset;
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(transform.position, transform.right, -pos.y * turnSpeed);
transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.position = new Vector3(transform.position.x, transform.position.y - zoomSpeed, transform.position.z + zoomSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.position = new Vector3(transform.position.x, transform.position.y + zoomSpeed, transform.position.z - zoomSpeed);
}
}
}