Rotate Point A on Sphere to Point B on Sphere

Imagine a globe (sphere). I have Point A (Miami FL) and Point B (Portland OR). I would like to rotate the globe so that:

  1. The position of the globe and main camera do not move
  2. Point A ends up where Point B was
  3. The globe rotates along the an axis that runs through the center of the sphere and is normal to the plane created by Point A, Point B, and the center of the sphere.

I have been using Quaternion.FromToRotation but I have not been able to come up with a good solution.

Any suggestions?

Thanks, Mike


Vector3 pointA;
Vector3 pointB;
GameObject sphere;

//What should I put here?

sphere.transform.rotation = resultRotation
more ▼

asked Jul 09 '10 at 07:43 PM

Mike Miller gravatar image

Mike Miller
4 5 5 10

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

I hope i understand it right. If so i had a similar problem yesterday.

I used this:

Quaternion rotation = Quaternion.LookRotation(targetVector);
Vector3 point = new Vector(x,y,z);
point = rotation  * point ;

Also instead of using a Vector you rotate it should work if you use the rotation on a transform.rotation of a GameObject.


So i played around a little bit. I added one sphere in the middle of my scene, and two cubes.

    // the cubes
    public GameObject pointA;
    public GameObject pointB;
    // the sphere
    public GameObject globe;

    Quaternion rotationA;
    Quaternion rotationB;

void Start()
     // get the start rotation of the points
     rotationA = Quaternion.LookRotation(pointA.transform.position);
     rotationB = Quaternion.LookRotation(pointB.transform.position);

void Update()
    // here the sphere rotate from point a to b
    // to make the cubes follow the sphere, you have to calculat there position, 
    // or put the gameobjects from the cbes inside the sphere
    globe.transform.rotation = Quaternion.Slerp(rotationA, rotationB, Time.time * 0.1f);    


was the function which helped me, it worked for me but im not sure if this is the best way to do it

more ▼

answered Jul 09 '10 at 07:49 PM

Case23 gravatar image

471 18 21 30

I am not quite sure how to implement your suggestion. The output seems to be a point and I think I am looking for a rotation.

Jul 09 '10 at 08:06 PM Mike Miller
(comments are locked)
10|3000 characters needed characters left

I think you are on the right track with FromToRotation.

for the following to work your sphere would have to be at global (0,0,0)

Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.rotation *= rot; //Multiply rotations to get the resultant rotation.

if your sphere is not at global (0,0,0) you need to make sure that pointA and pointB relative to the sphere coord system and then use the following:

Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.localRotation *= rot; //Multiply rotations to get the resultant rotation.
more ▼

answered Jul 09 '10 at 11:11 PM

cncguy gravatar image

1k 21 25 36

Thank you. This worked.

Jul 13 '10 at 09:42 PM Mike Miller

I posted the full code I used below.

Jul 13 '10 at 09:52 PM Mike Miller
(comments are locked)
10|3000 characters needed characters left

cnc guy helped answer my question. This is the code I ended up with. It allows a user to grab a point on a sphere and rotate the sphere from that point.

using UnityEngine; 
using System.Collections; 

public class EarthTouchMovement : MonoBehaviour { 

    bool hasGrabbedPoint = false;
    Vector3 grabbedPoint;

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

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

        return transform.InverseTransformPoint(hit.point);
more ▼

answered Jul 13 '10 at 09:46 PM

Mike Miller gravatar image

Mike Miller
4 5 5 10

(comments are locked)
10|3000 characters needed characters left

Is there a simple way to modify this solution to constrain the globe to stay right side up? Meaning, I want to keep the North Pole at the top instead of it ending up getting spun on its side. I guess it would spin on it's Y and X axis but not Z. Thanks.

more ▼

answered Oct 11 '13 at 02:28 PM

TotallyAtRandom gravatar image


Oct 11 '13 at 03:33 PM Marsupilami
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jul 09 '10 at 07:43 PM

Seen: 5643 times

Last Updated: Oct 11 '13 at 03:33 PM