Making an enemey follow the player

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
warmthunder51
Prole
Posts: 2
Joined: Tue Mar 11, 2025 1:50 pm

Making an enemey follow the player

Post 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.
Attachments
main.lua
(2.72 KiB) Downloaded 2 times
User avatar
dusoft
Party member
Posts: 754
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Making an enemey follow the player

Post 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/
User avatar
darkfrei
Party member
Posts: 1244
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making an enemey follow the player

Post by darkfrei »

Code: Select all

elseif distY  < 0 then
   ghost.x = ghost.x - 1
X or y?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
warmthunder51
Prole
Posts: 2
Joined: Tue Mar 11, 2025 1:50 pm

Re: Making an enemey follow the player

Post 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!!
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 10 guests