The title says it all with my question. I am firing a projectile, for sake of ease lets call it an arrow, I'd like it to rotate so that the arrowhead is always pointing in the direction of movement. So for example: if it's fired straight up it would be pointing up & when coming back down it would be facing down.
I have searched up this question & found many different solutions, none of which have worked for me. A big one I see a lot is:
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
and this does rotate the GameObject correctly, however has weird game-breaking consequences where the game object moves slowly, jitters a lot, teleports around, then breaks completely. I have tried putting this code in the Update() function, FixedUpdate() function & the LateUpdate() function all producing the same results.
https://youtu.be/L9LVM8AmjJM - link to a video showing the weird results using the code above.
CodePudding user response:
Try
rigidbody.MoveRotation(Quaternion.LookRotation(rigidbody.velocity));
within FixedUpdate
.
Going through transform
in most cases breaks the physics and MoveRotation
interpolates the rotation rather than hard jumping to it and still respects obstacles etc and interacts correctly with those.
What happens in your video though I think is that your velocity suddenly changes when hitting the ground -> you hard set the objects rotation -> it is kicked up into the air again because of the resulting collision forces.
This might of course still occure with the upper method => I would skip the rotation setting if colliding with something e.g.
int collisions;
private void OnCollisionEnter()
{
collisions ;
}
private void OnCollisionExit()
{
collisions--;
}
private void FixedUpdate()
{
if(collisions==0) rigidbody.MoveRotation(Quaternion.LookRotation(rigidbody.velocity));
}