How to move npcs

Im making a platformer and I want my npc to go to the player when its in the area2d I got that working and set up but Im not sure how to make it move towards the player or move at all i’ll send a screen shot of the nodes and the code extends AnimatableBody2D

Called when the node enters the scene tree for the first time.

func _ready() → void:
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
$AnimatedSprite2D.play(“idle”)

func _on_area_2d_body_entered(body: Node2D) → void:
print(‘ah’)

Inside your _process function, you can modify the position of your AnimatableBody2D (Assuming thats your npc).

If you want the npc to move to your player in a straight line, you can simply do something like

position = position.lerp(TARGET_POSITION, delta * MOVE_SPEED)
1 Like

I tried referencing the player but it didnt work extends AnimatableBody2D

var MOVE_SPEED = 200
var TARGET_POSITION = $“.”.posistion

Called when the node enters the scene tree for the first time.

func _ready() → void:
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
$AnimatedSprite2D.play(“idle”)
position = position.lerp(TARGET_POSITION, delta * MOVE_SPEED)

func _on_area_2d_body_entered(body: Node2D) → void:
print(‘ah’)