feat: add follow logic to the slime;
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user