I tried to rotate the camera around an object in the game using trigonometry. But whenever the angle of any of the rotation coordinates goes above 90 degrees or below 270 the camera would quickly invert it’s position coordinates and instantly put its rotation back between 270 and 90.
I used the code below, I have no idea why it isn’t working.
void Update () {
camera_rotX = transform.rotation.x;
camera_rotY = transform.rotation.y;
camera_rotZ = transform.rotation.z;
findPos ();
transform.position = new Vector3 (camera_posX, camera_posY, camera_posZ);
}
void findPos(){
camera_posX = (Mathf.Sin(camera_rotX)) * camera_distance;
camera_posY = (Mathf.Sin(camera_rotY)) * camera_distance;
camera_posZ = (Mathf.Sin(camera_rotZ)) * camera_distance;
}
I have seen the “RotateAround” function being suggested, but wouldn’t a more mathematical solution allow better control over things like the distance between the object and camera?
I was hoping to use this code to make a camera setup similar to that of Homeworld, so the player can drag their mouse and pan the camera around the object they have it attached to.
Help! (And thanks!)