I'm making a system where once the player enters within the radius of the Enemy, the enemy chases them and attacks them.
Upon running this code, instead of chasing them, the Enemy moves to a spot diagonal of the player and moves along with the player's movement.
How come they aren't actually chasing the player? I suspect that it has to do something with the velocity of the enemy, though, I'm not sure what the root cause of this is.
const initialX = 150
const destinationX = 650
var velocity = Vector2()
var speed = 200
var wentToFinalPos = false
var detectedPlayer = false
var playerPosition = Vector2()
onready var player = get_parent().get_node("Player")
playerPosition = player.global_position
if (detectedPlayer == false):
if (wentToFinalPos == false):
velocity = Vector2(1, 0)
if (floor($".".position.x) >= destinationX):
wentToFinalPos = true
velocity = Vector2(-1, 0)
if (floor($".".position.x) <= initialX):
wentToFinalPos = false
velocity = (playerPosition - position)
velocity = move_and_slide(velocity.normalized() * speed)
if (body == player):
detectedPlayer = true