Hi everyone, I’m having trouble with the code, can someone help.I wanted to make sure that when a player enters the Area2D zone, the mob starts following him, but for some reason when I entered, he ran only in one direction and not in the one in which the player is.And if it’s difficult, could you give a little instruction on how to use it.Thanks
It depends on various factors.
Is the mob a characterBody2d? If yes, then you have access to its _physics_process() which makes you alter its velocity to move towards a target.
var player = null
func _physics_process(delta) -> void:
if not player: return
var direction = (player.transform.origin - self.transform.origin).normalized()
velocity = direction * speed * delta
move_and_slide()
func _on_area2d_body_entered(body) -> void:
# TODO: check if body is player
if true:
player = body
if your mob is just a sprite2d with no physics, then it’s much easier. Just create a tween to go to your player’s position
#get a reference to the player first, does the mob know who the player is? create_tween().tween_property(self, "position", player.position, 5.0)