Page 1 of 1

Making an enemey follow the player

Posted: Tue Mar 11, 2025 1:55 pm
by warmthunder51
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.

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
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.

Re: Making an enemey follow the player

Posted: Thu Mar 13, 2025 1:15 pm
by dusoft
That's very naive approach.

I didn't check your full code, but I would recommend something like this instead - grid pathfinding:
https://github.com/GlorifiedPig/Luafinding

Also more on details of ghost movement in Pacman:
https://www.todayifoundout.com/index.ph ... -man-work/

Re: Making an enemey follow the player

Posted: Thu Mar 13, 2025 6:05 pm
by darkfrei

Code: Select all

elseif distY  < 0 then
   ghost.x = ghost.x - 1
X or y?

Re: Making an enemey follow the player

Posted: Fri Mar 14, 2025 3:45 pm
by warmthunder51
darkfrei wrote: Thu Mar 13, 2025 6:05 pm

Code: Select all

elseif distY  < 0 then
   ghost.x = ghost.x - 1
X or y?
your right, it was just that, because i had 2 statments changing the x and no y, it cancelled the movements for left and there was no upwards movement, thank you!!