- Home /
Rotation math problem
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);
}
Answer by maccabbe · Jun 17, 2015 at 12:03 AM
It seems like the simplest solution would be to use Quaternion multiplication. It's what Unity (probably) uses for child-parent rotations. In your case you would do something like
Quaternion tiltRotation=(Whatever you want it to be);
Quaternion circleRotation=Quaternion.Euler(0, 0, zRotation+90f);
transfrom.rotation=circleRotation*tiltRotation;
http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Trouble with Camera Rotation Math 2 Answers
Rotation using exponential functions 1 Answer
Projectile motion arc 2d 1 Answer
How to Reverse an equation.. (c#) 2 Answers