I have a camera that rotates around a disk. When I create an empty gameobject at the center of the disk and child the camera to it, I can simply rotate the whole thing so that the camera keeps its x-axis rotation (tilted up).
How do I replicate the exact same behaviour without using Unitys child-parent rotation?
I already have it working that I get the correct camera position on the “unit circle” via the degrees I input, but next, I need to rotate the camera so that it always looks slightly away from the circumference. How can I fix the line marked with “PROBLEM”?
//center is 0,0,0
protected void RotateBy(float degrees)
{
_angle += degrees;
// Set new position
float x = Mathf.Cos(_angle * Mathf.Deg2Rad) * _altitude;
float y = Mathf.Sin(_angle * Mathf.Deg2Rad) * _altitude;
transform.position = new Vector3(x, y, transform.position.z);
}
///
void LookAtCenter()
{
Vector3 directionToCenter = Vector3.zero - transform.position;
float zRotation = Mathf.Atan2(directionToCenter.y, directionToCenter.x) * Mathf.Rad2Deg;
// Offset 90 degrees to make bottom face the center.
// PROBLEM: This only works if x and y are both 0
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, zRotation + 90f);
}