Question by
SolidRubrical · May 31, 2018 at 06:30 PM ·
rotationcamera-movementtrigonometryfloatingpoint
Problem with rotating camera around player
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);
}
}
}
Comment
Best Answer
Answer by SolidRubrical · Jun 01, 2018 at 01:21 PM
I fixed my issue by replacing it with this line
playerCamera.transform.RotateAround(gameObject.transform.position, playerCamera.transform.right, degToRotate * cameraVerSpeed);
Your answer
Follow this Question
Related Questions
Rotate Object Axis On Swipe Cam Rotation - Android 1 Answer
MouseOrbitImproved slanted orbit 0 Answers
Rotate the player with animation 2 Answers