# How to rotate to a direction

Attention Topic was automatically imported from the old Question2Answer platform.

I got my direction by

var dir = (target_position - position).normalized
but how would I go about rotating my sprite to dir?

Use Vector2.angle():

``````rotation = dir.angle()
``````

Alternatively, use angle_to_point():

``````rotation = target_position.angle_to_point(position)
``````

Thanks for the help, it works well, I am just wondering if there is a way to rotate gradually instead of instead? I tried rotation += dir.angle * delta. But it would not stop at the direction

lowpolygon | 2019-06-02 11:59

Take a look at my answer here. Or just this:

``````func lerp_angle(from, to, weight):
return from + short_angle_dist(from, to) * weight

func short_angle_dist(from, to):
var max_angle = PI * 2
var difference = fmod(to - from, max_angle)
return fmod(2 * difference, max_angle) - difference
``````

Dlean Jeans | 2019-06-02 12:39

Thanks,again. The codes works but rotation is off about 90 degrees. I am just wondering if sprite is meant to be pointing up? I was told that sprite facing should be right .

lowpolygon | 2019-06-03 06:03

Doesn’t matter any way. Just offset 90 degrees.

Dlean Jeans | 2019-06-03 16:48

yes, that’s what I did. It works really well. Thank you for the help

lowpolygon | 2019-06-04 01:45

#Edit
sorry my bad physics mass is creating a lag in movement which i thought rotation is creating it.,

your code is working., but i could not understand it well enough to speed up the rotation., i tried setting weight variable higher., but in vain. It creates a lag in rotation while moving in different direction, as the player moves to next position.,

i would be getting an array of position more like points to reach the destination., each time it moves towards the next point it has to rotate towards that point.,
Like below.,
fun move (_pos):
var dir = (_pos - global_position).normalized()
move_and_collide(dir * get_process_delta_time() * speed)
rotation = lerp_angle(rotation,dir.anlge(),0.5) #rotate call

could you explain it or am i implementing wrong.,

muthu0914 | 2020-01-07 09:15

In case you didn’t know we got `lerp_angle` built-in from 3.2. So if you still got my `lerp_angle` in your code just delete it.

What you mean by `lag in rotation`? Maybe a video(gif/mp4) would help.
Just looking at it, I can’t really see anything wrong.

Dlean Jeans | 2020-01-08 11:17

Hi~OP here…I believe he has already figure out it was not the rotation’s problem, it is his movement where he multiply speed with deltatime()

lowpolygon | 2020-01-08 13:55

2 Likes

Thank you. That definitely worked. Just need to add some simple math to adjust for the angle of the sprite (unless its a straight arrow aiming at 0 degrees)