This is the view from my spaceship. It can move around in 3d space and turn up, down, left and right.

The planet Earth on the top right is selected. Now I want to create the red arrow to always point to the direction of Earth even if my ship is facing away from it.

Wanted result this is my code so far

//These are euler angles
Vector3 wantedheading = SelectedObject.transform.position - Statics.Player.transform.position;
Vector3 curentheading = Statics.Player.transform.localRotation.eulerAngles;

Quaternion qwanted = Quaternion.Euler(wantedheading);
Quaternion qcurent = Quaternion.Euler(curentheading);
Quaternion qMove = Quaternion.FromToRotation(curentheading, wantedheading);

but now I have a 3d rotation... what do I do to make it into a 2d rotation always pointing to the shortest direction to the planet?


When you have a vector from your spaceship to the earth you can just calculate the projection of the vector onto your 'camera plane'. You can normalize this vector and you have the direction the arrow should point to.

So you can use what you already have, rotate a vector and project it onto the 'camera plane'.


You can create a 2D rotation from a 3D one by just ignoring an axis (usually Z).

Quaternion 3dRotation = Quaternion.Euler(myRotation);
Quaternion 2dRotation = Quaternion(3dRotation.x, 3dRotation.y, 0.0f, 3dRotation.w);

Using this method you can modify your rotation to ignore one or two axis.

Or, you could use the same aproach when working with the headings:

Quaternion 2dRotation = Quaternion.Euler(new Vector3(myRotation.eulerAngles.x, myRotation.eulerAngles.y, 0.0f));

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.