Object pointing in direction of travel

Is there a good way to have a projectile rotate to point in the direction it is going? So if it is going up it points up, but when gravity takes over and pulls it down, it points down?
Thanks

You can use the objects velocity vector as its direction. That will allow you to have it point to where it is traveling.

something like…

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

:slight_smile:

That works quite nicely - thank you!

np :slight_smile:

Hey, the script from Seon above does work great but I was having some issues with the projectile rotating in unwanted ways so I want it to only rotate on the z axis, but with both of the following reversions of that script Unity tells me I can’t convert UnityEngine.Quaternion to a float.

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

Any ideas how to have the object only rotate along the z axis to follow the velocity of the z axis only?

you got to freeze the rotation on the object first:

rigidbody.freezeRotation = true;

then when you call this:

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

it will align with the direction of the current velocity.

also the Quaternion is looking for a vector3 rotation. So when you try to feed it a position vector like x only, then it throws an error.

cheers.

var offsetY = 0.5;
var offsetZ = 0.5;

function Start()
{
rigidbody.centerOfMass = Vector3 (0, offsetY, offsetZ);
}

maybe tweaking center of mass may help you too?

Good, that’s working how I want it now. Thanks guys!