I have currently have a path that wraps around a Sphere from one side to another - this is basically a 3D highway in the sky type object that is a textured road. I have an Aircraft model that I can get to follow this path, however I'm having trouble getting it to rotate correctly so that it is parallel to the surface of the globe

Using the following code:

this.transform.forward = Vector3.RotateTowards(this.transform.forward, this.transform.position - TargetWaypointPos, Airspeed * Time.deltaTime, 0.0f);

this.transform.position = Vector3.MoveTowards(this.transform.position, TargetWaypointPos, Airspeed * Time.deltaTime);

This gets the plane to correctly face the next waypoint and follow the path, however I cannot get it to rotate correctly in the Z-space to be parallel to the path/globe. I can compute the correct angle by:

Vector3 yAxis = this.transform.position;
Vector3 zAxis = nextPoint - this.transform.position;
xAxis = Vector3.Cross(yAxis, zAxis);
float angleZ = Vector3.Angle(Vector3.right, xAxis);

However attempting to apply this to my model in any way appears to ruin the Vector3.RotateTowards logic. I must be missing something simple to combine these two.

enter image description here

Any ideas? I'm new to Unity, still figuring out all the available options.


This is easier than you think!

// Get the vector toward the next waypoint from here.
Vector3 forward =  TargetWaypointPos - transform.position;

// Get the local "up" vector pointing out of the planet to here.
Vector3 up = transform.position - SphereCenterPos;

// Form an orientation that points the z+ axis along "forward",
// and the y+ axis as close as possible to "up".
Quaternion targetRotation = Quaternion.LookRotation(forward, up);

// Rotate toward this target at a controlled rate.
transform.rotation = Quaternion.RotateTowards(
    Time.deltaTime * DegreesPerSecond
| improve this answer | |

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.