2025-01-18 13:09:17 +11:00

24 lines
492 B
GDScript

extends CharacterBody2D
const MAGNET_FORCE = 100
func _ready():
add_to_group("magnets")
max_slides = 1
func _physics_process(delta):
var acceleration: Vector2 = Vector2()
#calc direction
for iter in get_tree().get_nodes_in_group("magnets"):
if iter == self:
continue
acceleration += (iter.position - position).normalized() * MAGNET_FORCE
#apply to velocity
velocity += acceleration * delta
var collided: bool = move_and_slide()
if collided:
velocity = Vector2.ZERO