- Home /
Align an axis to a plane
I'm making a simple ICBM simulation. I want my "rocket" (more of a sphere) to move forwards along its forward axis (z) and face some point on a scale Earth with its y axis facing away from the target.
I figure I should be able to align the y axis to a plane (created from the center of the Earth, the launch point, and the target point). After that I can get it facing the way I want (in this case, away from the target).
Ideally I would then use its local x axis for pitch, and its local y axis for heading. In practice, I'd rotate locally around my x axis to nose down in an arc towards the target and rotate locally around my y axis to do course correction as I go.
On the image, if you were to rotate about 135 degrees around the z axis you'd get close to what I want (with green facing away from the viewer, and red facing to the left corner of the image).
The Green pin is the launch site, and the Red pin is the target.
// Determine where our next point is
Vector3 nextPosition = transform.position + velocity;
// Look at our new position. This gets our Forward vector pointing the right way
transform.LookAt(nextPosition);
// Turn so our YZ plane intersects the target, with Y facing away
// <-- HERE?
// Move to the new position
transform.position = nextPosition;