# Rotating camera around sphere along longitude

In my project I want to rotate a camera along a longitude line of a sphere.

The sphere is located at (0,0,0), the camera at (0,10,0) with rotation according to inspector of (88,0,0).

This is my update function:

``````void Update()
{
Vector3 cross = Vector3.Cross(cam.transform.forward, sphere.transform.up).normalized;
cam.transform.RotateAround(sphere.transform.position, cross, 3.1f);
}
``````

It’s obvious, that cross is Vector3.zero at the north or south pole, but I can “workaround” this problem by choosing suitable start (88) and step (3.1).
When running the script, the angle in inspector goes to lower values, until it reaches approximately -90. There it jumps between -88.5 and -91.5 - always half the step width around -90.

When I start with rotation (92,0,0), the shown rotation angle goes down as well but at some point it switches to 270 and then jumps around this (about 268.5 to 271.5 I guess).

In my project there are other rotations as well (along latitude for instance) and around cam.transform.forward and others, so it won’t help to change the rotation completely, since my start rotation is always the result of a couple of previous rotations of the camera around the sphere.

How can I solve this, such that I can go around the globe along a longitude always in the same direction?

Thanks!

Jeff

You want to just set up a single float variable as your “where I am around the globe on this longitude” value.

Once you have that, just steadily increase it every frame, then use that value you drive the rotation of an invisible object located at the center of the sphere. The camera would then be parented to this invisible object, and hence when the invisible object rotates, the camera appears to orbit around that point according to the rotation.

``````private float myAngle;  // where we are around the globe
private const float MySpeed = 50.0f;  // degrees per second
``````

and each frame:

``````myAngle += MySpeed * Time.deltaTime;

InvisibleObject.transform.rotation = Quaternion.Euler( myAngle, 0, 0);
``````

That rotates around the 0 longitude line. You can multiply the Quaternion.Euler() above by another Quaternion.Euler() featuring constant values to cause it to orbit at another longitude angle.

``````// chose 30 degrees west longitude: (and 150 deg east on the far side)
InvisibleObject.transform.rotation = Quaternion.Euler(0, 30, 0) * Quaternion.Euler(myAngle, 0, 0);
``````