feat: add follow logic to the slime;

This commit is contained in:
2026-09-24 21:52:56 +02:00
parent 7b30283765
commit 2aad0915e3
5 changed files with 197 additions and 14 deletions
+32
View File
@@ -0,0 +1,32 @@
extends CharacterBody2D
@export var speed: int = 300
var target: Node2D
func _physics_process(delta):
if target:
animate_slime()
move_slime()
move_and_slide()
func move_slime():
var distance_to_player: Vector2
distance_to_player = target.global_position - global_position
var direction_normal: Vector2 = distance_to_player.normalized()
velocity = direction_normal * speed
func animate_slime():
var normal_velocity: Vector2 = velocity.normalized()
if normal_velocity.x > 0.707:
$AnimatedSprite2D.play("move_right")
elif normal_velocity.x < -0.707:
$AnimatedSprite2D.play("move_left")
if normal_velocity.y > 0.707:
$AnimatedSprite2D.play("move_down")
elif normal_velocity.y < -0.707:
$AnimatedSprite2D.play("move_up")
func _on_player_detect_area_body_entered(body):
if body is Player:
target = body