I’m attempting to use this code from a previous AskUnity question to rotate the nose (the right edge) of a rectangle(Rigidbody2d) towards the velocity it is currently travelling.
I’ve converted the current velocity of my Rigidbody from a vector2 to a vector 3, but because it rotates the X axis, and not the Z axis, as soon as it rotates in the 3D space the item disappears. How would i rotate the right edge towards the velocity it’s travelling?
Here is a bit of code that will rotate the object to face the direction of the velocity over time:
function Update () {
var dir = rigidbody2D.velocity;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
var q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, rotateSpeed * Time.deltaTime);
}
‘rotateSpeed’ is a variable you define and whose value is in degrees per second. If you want eased movement, replace ‘RotateTowards’ with ‘Slerp’ and adjust ‘rotateSpeed’ downwards a bunch.