How can i make enemies target the player?

Thank you roaminggamer and XeduR @Spyric for your answers! This was really helpfull and I have now mostly formed a plan on how to do it. I will watch thorugh the links and see what fits my desired game funktion :smiley:

I also forgot to mention.  SSK2has a full suite of solutions for aiming, moving, etc.

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions2/

You’ll need to understand Corona basics before SSK2 will be of any use to you however.

This particular example might be interesting to you too:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/07/zombieFollow.zip

Alright, thank you I will look into those aswell :) Really appreciate the help. Although, it will have to wait untill I am done with some school prodjects and have time for more programming. 

How do you want the enemies to ‘chase’ the player? Always just directed at the player, and moving towards them in the shortest “straight” line possible? Or do they need to move around obstacles etc?

I was planning to have zombies chase the player, and therefore it would make the most sence if they moved directly to them in the shortest line possible. Due to the previous answers I have an ida on how to do this. If you have a simple method that would also be really helpfull :slight_smile:

Well, it seems like the math function library is your best friend:

math.atan2 (x, y)

Will give you the angle your enemy needs to rotate, to be pointing at your player, with x = (player.x - enemy.x) and y = (player.y - enemy.y)

This will give you the angle in radians. Use the math.deg() function to convert this to degrees. You can use this value to set the rotation angle of your enemy, so it points towards your player (make the sprite point RIGHT in the default 0 degrees rotation).

Update this rotation every frame, so it stays correct even if your player and enemy move.

To move your player, every frame move the enemy by setting these values:

enemy.x = enemy.x + math.cos(enemy.rotation)*enemySpeed

enemy.y = enemy.y + math.sin(enemy.rotation)*enemySpeed

Oh that is quite practical. Thank you for the advice, i will most definenly use this :slight_smile:

You’re welcome. I may have made a mistake in my code somewhere, but it should generally cover the concept.

Best is to just code a simple setup to test and make sure, and then put the right code in your project.

Alright will do:) but im still in the planning fase to see if i actually can manage to make this game with my current skills and limited time.

Hi everyone i’m new to corona. Thomas i was wondering how does your method work because i have tried applying it in my game but the actual movement of the enemy doesn’t work. Here’s my code to help me out please.

local Ennemi = {}

function Ennemi:new(spawnerX,spawnerX,vie,cible)

– ennemi = l’intance d’un ennemi

local ennemi = display.newCircle(0, 0, 15)

randomSpawn = math.random(1, #spawnLocation)

spawner = spawnLocation[randomSpawn]

spawnerX = spawner.x

spawnerY = spawner.y

local coteX = personnage.x - ennemi.x

local coteY = personnage.y - ennemi.x

local distance = math.sqrt(coteX^2 + coteY^2)

speed = 0.2

local transitionTime = (distance)/speed

angleRad = math.atan2(coteY,coteX)

angleDeg = math.deg(angleRad)

ennemiSpeed = 0.5

function ennemi:init()

– dans une methode (objet:function()) on peut utiliser ‘self’ pour parler de l’objet (dans notre cas: self = ennemi) : avantage: code pas mal plus réutilisable

self.x = spawnerX

self.y = spawnerY

self.vie=vie

camera:add(ennemi,1,false)

physics.addBody( self, { density=1.0, friction=0.3, bounce=0.2, radius=15 } )

self:setFillColor(1, 0, 0, 1)

self:addEventListener( “collision”, self )

–self:bouge()

function getAngle()

ennemi:rotate(angleDeg)

self:move()

end

timer.performWithDelay(300, getAngle, 1)

end

–local function rebouge()

–transition.moveTo( self, { x=personnage.x, y=personnage.y, time=transitionTime , onComplete=bouge} )

–ennemi:bouge()

–end

–function ennemi:bouge()

–transition.moveTo( self, { x=personnage.x, y=personnage.y, time=transitionTime, onComplete=rebouge } )

–end

function ennemi:move()

ennemi.x = ennemi.x + math.cos(ennemi.rotation)*ennemiSpeed

ennemi.y = ennemi.y + math.sin(ennemi.rotation)*ennemiSpeed

end

function ennemi:collision(event)

if(event.other==bullet) then

self:removeSelf()

display.remove(bullet)

scoreBase = scoreBase+1

score.text = scoreBase

elseif (event.other==personnage)then

resetScore()

end

end

sceneGroup:insert(ennemi)

ennemi:init()

– une fois qu’on a fabriqué l’ennemi, on le ‘return’

return ennemi

end

Hi Benoit,

Try the code in this ZIP file.

Forgot to say this: you can drag the “hero” dot around, and the enemies will keep on tracking you.

all right thanks a lot i will take a look:)

You’re welcome Benoit!

You’ll see that the code is both an example of the “rotate and follow” code for the enemies, and an example of the way I structure my code, as separate modules. If you have any questions about either, let me know. Et Bienvenu au forum! :wink: