Making an enemey follow the player
Posted: Tue Mar 11, 2025 1:55 pm
Hello, I recently got into lua and love2d, I'm trying to make a very basic pac-man clone. Current I have a ghost as a circle that chases you but I cannot seem to figure out the code for it.
This is my current implementation, it works only for positive x and negative y, but not for negative x and positive y, i cant seem to figure out why it wont go in the other direction. Any help is greatly appreciated, also sorry if this is a stupid question or I missed something obvious.
Code: Select all
--movement for ghost
if distX > 0 then
ghost.x = ghost.x + 1
elseif distX < 0 then
ghost.x = ghost.x - 1
end
if distY > 0 then
ghost.y = ghost.y + 1
elseif distY < 0 then
ghost.x = ghost.x - 1
end