Rotate Node3D at a constant speed

Godot Version

4.3

Question

Heya! I’,m currently trying to make a ceiling-mounted turret that points at the player’s head. I have adapted a tutorial I found for 2d to achieve this.

However, this results in smooth motion that slows down as the target is reached due to how my implementation of interpolation works. I want the node to rotate at a constant speed, stopping near-instantly (or instantly if that’s easier :P) when it reaches its goal.

My current implementation can be seen below:

I wish to achieve something like the effect shown at 4:24 in the tutorial. If anyone knows how to go about this, please let me know! Any and all help would be much appreciated.

1 Like

I would use lerp instead of slerp, for linear interpolation.

I would also try to just calculate the desired_rotation_angle, using vector math (atan2). This is cheaper than using looking_at.

var direction = (target_position - position).normalized()
var desired_rotation_angle = atan2(direction.y, direction.x)

then you can just lerp the necks current rotation angle to the desired_rotation_angle

Heya, thanks for the advice! I’ve never used atan2 before though, and I’m struggling to figure out how to use it for the two axis I need so that the turret points at the player both left to right and up and down.

I’ve (poorly) illustrated what I mean below:

mspaint drawing so good

Solved using this approach:

2 Likes

Nice!

atan 2 takes the difference between 2 points, and calculates a direction in radians

var dx = x2 - x1
var dy = y2 - y1
var angle = atan2(dy, dx)

lerping the turrents rotation is slightly cheaper performance in 2D, because look_at handles the turrets matrice which is more computation than necessary

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.