0
\$\begingroup\$

I'm trying to make a world map in Unity that contains a rotating globe with landing sites on the perimeter. What I want is that when a player clicks on the landing site the globe rotates so that the landing site is centered. I've been trying to find the right way to use the Quaternion class but not much luck.

Here is my latest attempt which doesn't work. The landing sites are parented to the globe but the camera and globe are separate.

        void Update()
    {
        if (_selectedLandingSite != null)
        {
            var rotation = Quaternion.FromToRotation(_selectedLandingSite.transform.localPosition, MainPlanet.transform.forward);

            var trans = Game.Game.Instance.MainCamera.gameObject.transform;
            var lookDirection = new Vector3(trans.position.x, trans.position.y, trans.position.z);
            lookDirection = rotation * lookDirection;

            var _lookRotation = Quaternion.LookRotation(_selectedLandingSite.transform.localPosition);


            //rotate us over time according to speed until we are in the required rotation
            MainPlanet.transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * 1);

            //MainPlanet.transform.rotation =
            //  Quaternion.RotateTowards(MainPlanet.transform.rotation, _targetRotation, 1);
        }
    }
\$\endgroup\$
1
\$\begingroup\$
var toCamera = Quaternion.LookRotation(camera.position - globe.position);

Gives a rotation that spins the z+ axis to point from the globe to the camera.

var toSite = Quaternion.LookRotation(site.localPosition);

Gives a rotation that spins the z+ axis to point to the landing site in the globe's local space.

var fromSite = Quaternion.Inverse(toSite);

Gives a rotation that spins the site so that it sits along the local z+ axis.

Chaining it together:

globe.rotation = toCamera * fromSite;

Is a rotation that will that will spin the site so that it faces the camera.

\$\endgroup\$
1
  • \$\begingroup\$ Works perfectly! Thanks, been struggling with this for days. \$\endgroup\$ – Greg Petersen Mar 14 '18 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.