How can I make a missile face the direction it's moving?

Hi,

I've been trying to work out what I think is a really simple problem - But I just can't solve it.

I have a missile that's being launched (a simply cylinder default model). The missile is given a Z and Y velocity, so it fires and arcs correctly under gravity.

I want the missile to face the direction it's moving, so it rotates depending on its velocity.

I've tried using lookat but it doesn't seem to work - There's an added problem that if it does work, the original cylinder model is 90 degrees out (i.e, the original model will look like a vertical pillar if fired forwards, rather than a horizontal, er..sausage..)

Any help is appreciated

Thanks

Here are 2 tricks:

  1. Create an empty parent for your rocket. Then rotate the rocket so that it is correctly orientated inside the parent. Then instantiate an instance of your parent when you fire the rocket. This will solve the 90 degree rotation problem.

  2. The easiest way to get a missile (parent gameObject) to arc is to have it face its velocity. So, assuming that your missile is like the majority in the world it will face up on the way up, and down on the way down.


    transform.rotation = Quaternion.LookRotation(rigidbody.velocity);

Make your meshes be centered on the world origin in your 3D app.

You can also move the center of mass with this function: Rigidbody.centerOfMass

Fix the 90 degree rotation: How do I fix the rotation of an imported model?

missile.transform.foward = missile.rigidbody.velocity;

Or better, use AddTorque to slowly (or quickly, depending on how much torque you add) to rotate the missile towards its velocity. To make it more realistic, make sure you scale the torque you add based on the off-axis velocity, as in:

float sideVelocity = missile.rigidbody.velocity - Vector3.Dot(missile.rigidbody.velocity, missile.transform.forward))

If you just rotate the missile directly, it will be affected too heavily by gravity.

It may also be a good idea to manually script drag in the direction perpendicular to the movement (sideVelocity *= (1 - dragSide); and then adjust the velocity itself accordingly).

Thanks for the quick responses!

I went for Peter G's solution - Worked perfectly, so thanks again.

I've really gotta get my head around all of the different lookat / LookRotation type function...