I am making a Top-Down shooter in Godot 3.0 on Linux.
I would like to have my character rotate to face the mouse-pointer at a constant speed, and have rotation speed power-ups that allow the player turn faster.
So far, this is done by having a Position2D object called "Center" share the same position (but not be a child of) the player (below just called "Sprite"). The center always looks to the mouse, and the Sprite rotates with linear interpolation to eventually reach the same rotation as the "Center".
The weight of linear interpolation is calculated by dividing the difference in rotation (in radians) by an arbitrary constant speed.
Problem: The speed of rotation increases as rotation occurs, or more specifically, the time it takes to reach the same rotation (a rotational difference of zero) becomes shorter over time.
I have the following code:
extends Sprite
onready var center = get_parent().get_node("Center")
var current_time = 0
var total_time = 0
var start_rot = 0
var end_rot = 0
func _process(delta):
center.look_at(get_global_mouse_position())
var amt = 0
var theta = abs(self.rotation - center.rotation)
var speed = 0.05
total_time = theta/speed
start_rot = get_rotation()
end_rot = center.rotation
if not total_time == 0:
amt = current_time/total_time
current_time += delta
amt = clamp(amt,0,1)
var rot = lerp(start_rot,end_rot,amt)
center.rotation = fmod(rot,(PI*2))
set_rotation(fmod(rot,(PI*2)))
get_parent().get_node("Control/rotation").text = str(int(self.rotation_degrees))
get_parent().get_node("Control/theta").text = str(theta)
get_parent().get_node("Control/time").text = str(total_time)
What am I doing wrong?
set_rotation(get_rotation()+min(theta, speed))
, assuming speed is the maximum angle you can turn per frame? \$\endgroup\$