I am making a standard third-person camera setup, I want to rotate camera in “Y and X-axis” around player with player as pivot. I am using the angle to rotate and distance from camera with trig to find the camera movement in Z and Y-axis and rotation, but for some reason the Z-axis movement is too big when tilting the camera upwards or downwards, so it eventually clips through the player. Any hints on where my problem might be is appreciated.
public class Camera_Movement : MonoBehaviour {
public Transform playerTransform;
public Camera playerCamera;
public float cameraVerSpeed;
public float cameraHorSpeed;
public float zoomSpeed;
float distPlayerCamera;
float radiantToRotate;
float degToRotate;
float cameraMoveY;
float cameraMoveZ;
// Update is called once per frame
void Update ()
{
//Camera movement -right mouse button
if (Input.GetMouseButton(1))
{
//Change rotation degree to radians for Mathf sin and cos functions
degToRotate = -Input.GetAxis("Mouse Y") * cameraVerSpeed;
radiantToRotate = degToRotate * Mathf.Deg2Rad;
//Calculate camera Y and Z movement based on angle of rotation around the pivot
cameraMoveZ = distPlayerCamera - (distPlayerCamera * Mathf.Cos(degToRotate * Mathf.Deg2Rad));
cameraMoveY = distPlayerCamera * Mathf.Sin(degToRotate * Mathf.Deg2Rad);
//Camera Y-movement
//Change radianse rotation back to degrees and transform.translate relative to self
playerCamera.transform.Rotate(Vector3.right * degToRotate);
playerCamera.transform.Translate(0, cameraMoveY, cameraMoveZ, Space.Self);
}
}
}