Skip to content

Instantly share code, notes, and snippets.

@NovemberDev
Created October 22, 2021 18:53
Show Gist options
  • Save NovemberDev/6d78a07c9274ee25a6e9c16c9aad1fb9 to your computer and use it in GitHub Desktop.
Save NovemberDev/6d78a07c9274ee25a6e9c16c9aad1fb9 to your computer and use it in GitHub Desktop.
Smoothly look_at in 2D for Godot
extends Node2D
# These helper functions smoothly look at any object in 2D space.
# To avoid wrong rotations over +180 degrees, lerp_angle is used.
# Everything happens in global space, so the rotations should be applied properly.
func _process(delta):
$icon.global_rotation = look_at_position($icon, get_global_mouse_position(), 5.0 * delta)
func look_at_position(source: Node2D, target: Vector2, delta: float):
var direction = target - source.global_position
return lerp_angle(source.global_rotation, direction.angle(), delta)
func look_at_node(source: Node2D, target: Node2D, delta: float):
var direction = target.global_position - source.global_position
return lerp_angle(source.global_rotation, direction.angle(), delta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment