# Need help with rotating a sphere through a point to camera

Hi guys,
seems like i am stuck with this. Scenario: I have a sphere (planet) and can position objects, lets say citys or whatever on it via latitude longtitude coordinates. in the moment i use basic spheres as placeholders for those “cities”. I parent them underneath the planetsphere, so can rotate with it.

Now i have some Gui buttons which are supposed to rotate the planet in a way that a selected city is in front, thus facing to the camera. Means i somehow need to find the rotation of the planet through the city to the camera. But i am lost.

i played around with this, which is connected to the planet, where PointB is a citytransform:

``````Quaternion autorot = Quaternion.RotateTowards (transform.localRotation, Quaternion.LookRotation(pointB.localPosition), Time.time * 0.2f);

transform.localRotation = autorot;
``````

If somebody finds the time and has a quick hint, i would appreciate it!

I haven’t tried this at all, so it could be wrong, but it works in my head

``````Vector3 planetPos;
Vector3 cityPos;
Vector3 cameraPos;

Vector3 planetToCity = (cityPos - planetPos).normalized; //vector that points from planet to city
Vector3 planetToCamera = (cameraPos - planetPos).normalized; //vector that points from planet to camera

transform.rotation = Quaternion.LookRotation(planetToCamera) * Quaternion.LookRotation(planetToCity);
``````

Thanks Dude,
you were almost there. I still got wrong rotations but tried as long until i finally got it.

Script follows. For anybody who wants to try, you should have the following setup:

1. An empty Gameobject at position 0,0,0 as a Container.
2. inside this again at 0,0,0 your sphere or planet model.
3. inside the Container some transforms, which you can place manually around the globe. (Well, i did this with scripts, but whatever)
4. a camera of course, mine is at 0,0,-20 with zero rotation (i just write this because i did not test if this works when objects are somewhere else)
5. connect the script to the container
6. You need one more script that sets PointB to one of your city transforms
``````using UnityEngine;
using System.Collections;

public class EarthTouchMovement : MonoBehaviour {

bool hasGrabbedPoint = false;
Vector3 grabbedPoint;
public Transform pointB;
public Transform camTr;

private Transform tr ;

void Awake() {
tr = transform;
}

void Start() {
Camera cam = Camera.main;
camTr = cam.transform;
}

void Update ()   {
if (Input.GetMouseButton(0))  {
if(!hasGrabbedPoint) {
hasGrabbedPoint = true;
grabbedPoint = getTouchedPoint();
pointB = null;
} else {
Vector3 targetPoint = getTouchedPoint();
Quaternion rot = Quaternion.FromToRotation (grabbedPoint, targetPoint);
transform.localRotation *= rot;
}
} else {
hasGrabbedPoint = false;
if (pointB!= null) {

Vector3 planetToCity = (pointB.localPosition - Vector3.zero).normalized; //vector that points from planet to city
Vector3 planetToCamera = (camTr.position - tr.position).normalized; //vector that points from planet to camera

Quaternion a = Quaternion.LookRotation(planetToCamera);
Quaternion b = Quaternion.LookRotation(planetToCity);

b = Quaternion.Inverse (b);

Quaternion newRotation = a * b;
tr.rotation = Quaternion.Slerp(tr.rotation, newRotation, 0.1f);

}
}
}

Vector3 getTouchedPoint() {
RaycastHit hit;
Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit);

return transform.InverseTransformPoint(hit.point);
}
}
``````
1 Like

Cool. It’s possible that I got the order of quaternion rotations wrong - quaternion multiplication isn’t commutative so flipping them around might fix the problem, but I’m glad you got it working.

Me too. As i actually don´t understand why this is necessary. But i saw some unanswered questions like this here and there and hope this helps others in the future.