Hi everyone. I'm making 2D game and have little problem with very simple problem. I'm looking for way how to smoothly rotate 2D node towards Vector2 point (so point don't have to physically exist, it just must be defined). It must not rely on node-specific functions(like apply_impulse from RigidBody node) but rather use functions of Node or Node 2D, that is very general and universal. I'm trying figure this out for 2 days already, long enough to admit I need some help.

Have you tried angle_to or angle_to_point? I think you can make a Node2D look at any point (in global space) using code like the following: rotation += angle_to(other_node.global_position), though I don't really use the 2D side of Godot very often so it is quite possible I have forgotten how angle_to works.

I thought there was a demo for this in the demo repository, but it appears it was not updated past Godot 2.1. Here is the code for the constant mode in the demo. I changed a couple minor things so it should hopefully work with Godot 3:

var mpos = get_viewport().get_mouse_position() var ang = angle_to(mpos) var s = sign(ang) ang = abs(ang) rotate(min(ang, 1*delta)*s)

Hopefully this helps!

@TwistedTwigleg said: Have you tried angle_to or angle_to_point? I think you can make a Node2D look at any point (in global space) using code like the following: rotation += angle_to(other_node.global_position), though I don't really use the 2D side of Godot very often so it is quite possible I have forgotten how angle_to works.

I thought there was a demo for this in the demo repository, but it appears it was not updated past Godot 2.1. Here is the code for the constant mode in the demo. I changed a couple minor things so it should hopefully work with Godot 3:

var mpos = get_viewport().get_mouse_position() var ang = angle_to(mpos) var s = sign(ang) ang = abs(ang) rotate(min(ang, 1*delta)*s)

Hopefully this helps!

It rotates but not right, Nodes start spinning around target like planets around star. I'm able to get correct direction with code below.I just need to know how to smoothly interpolate between actual rotation and target rotation.

var vector_to_target = target_node.global_transform.origin - global_transform.origin var angle = rad2deg(atan2(vector_to_target.y, vector_to_target.x))

Note : I know i was solving this before, back in Unity3D, code above was translated from C#

@GarromOrcShaman said: It rotates but not right, Nodes start spinning around target like planets around star.

Yeah, I tested the code as well and it does not work... I guess things have changed (or, more likely, I forgot) :lol:

I'm able to get correct direction with code below.I just need to know how to smoothly interpolate between actual rotation and target rotation.

I tried your code and made a small change to smoothly rotate. It seems to work nicely, though I'm not sure if using lerp will reduce performance or not...

extends Sprite export (NodePath) var path_to_target; var target; const ROTATION_SPEED = 20; func _ready(): target = get_node(path_to_target); func _process(delta): var vector_towards_target = target.global_position - self.global_position; var angle = (atan2(vector_towards_target.y, vector_towards_target.x)); rotation = lerp(rotation, angle, delta * ROTATION_SPEED);

4 years later
2 years later

This can be more simply expressed as
extends Node2D func _process(delta): rotation = get_angle_to(Whatever node you want to track) + -PI/2