In 2D, or in 3D?
In 2D you need to convert your velocity into a normalized vector, and then use cos
and sin
to convert the X and Y values into rotation. The code below should make a 2D node look towards it's velocity (I have not tested the code, so I'm not 100% sure it will work):
var normal_velocity = velocity.normalized()
rotation = cos(normal_velocity.x) + sin(normal_velocity.y)
In 3D it is a little easier if you are okay using look_at
. What you do is you use look_at
and for the position argument you add the velocity so that it looks towards where it is looking. The code below should make a 3D model look towards it's velocity (as with the 2D code, I have not tested the code):
look_at(global_transform.origin + velocity, Vector3(0, 1, 0))