I’m making a game where the player can travel along the x/z plane only. I have a camera above the player that looks down at the player at a 45-degree angle. The camera needs to follow the player and always point at the player, and it can rotate around the player and zoom in and out. When I began the project, I had the player parenting the camera, but now I want their rotations to be independent. The question is how do I make the camera follow the player without making the player the parent of the camera, while maintaining my tricky rotation code? (Or how can I keep the parent-child relationship and keep independent rotation?) Everything I’ve found online points to just setting the camera position equal to the player position, minus a z value. I’m not sure how that would work with my isometric view, and letting the camera rotate and zoom.
Here is my current code for the camera. When the player is the parent, the camera rotates whenever the player does. The the player is NOT the parent, it does not follow the player.
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour
{
public const int MIN_ZOOM_DISTANCE = 80;
public const int MAX_ZOOM_DISTANCE = 200;
Transform target;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
transform.LookAt(target);
if(Input.GetKey(KeyCode.Q) || Input.GetAxis("RightStick Horizontal") > 0)
{
transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
}
else if(Input.GetKey(KeyCode.E) || Input.GetAxis("RightStick Horizontal") < 0)
{
transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.Alpha9) || Input.GetAxis("RightStick Vertical") > 0)
{
if(Vector3.Distance(transform.position, target.position) <= MAX_ZOOM_DISTANCE)
{
transform.position += transform.forward * -1;
}
}
else if(Input.GetKey(KeyCode.Alpha0) || Input.GetAxis("RightStick Vertical") < 0)
{
if(Vector3.Distance(transform.position, target.position) >= MIN_ZOOM_DISTANCE)
{
transform.position += transform.forward;
}
}
}
}