How can i make enemies target the player?

Hi- I am planning to make a survival game where the enemies chase the player, the problem is that i have no idea how to make them follow the player and collide with him/her. 

I am new to this so a bit dumbed down answers would be highly appreciated.

Thanks for advice in advance.

You’re going to have to write code to move your enemies and make them take actions.  

There are a wide range of movement types and actions so I can’t give you specific help on this.

That said, all of these cases involve:

  • timed actions using either timer.* or enterFrame events
  • vector math 
  • … more

I used to help out with a YouTube show for Corona called “Corona Geek”.

On the show I made many demo games including:

Other useful links:

Hey and welcome to the forums!

There are actually some easy ways of implementing this. Much of it depends on what type of game you are working on, i.e. is your survival game top down perspective, isometric, platform, etc.

For some general idea, let’s assume you have two variables: local player and local enemy. You could require the physics system and create a physics body for both the player and the enemy. This way you have access to physics collision detection. You could then, perhaps, set a timerwhere the enemy begins to move towards the player. This movement could be created using transitions or by applying linear velocity (see docs). For instance, you could state that every 250ms, the enemy begins to move to the x and y coordinates of the player, i.e. to player.x and player.y. So, if the player doesn’t move, the enemy will catch him. This might seem a bit rudimentary, but it’ll get you started. You can later on try to work out some “predictive system” that will try to move the enemy to where the player is headed to, so as to create the sense of the enemy trying to cut off the player’s movement path.

I see that roaminggamer also already posted a response. He is pretty much the go-to-guy with any Corona question or problem, so I am sure that you’ll find plenty of wisdom in his answer and GitHub archive :stuck_out_tongue:

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:

You’re going to have to write code to move your enemies and make them take actions.  

There are a wide range of movement types and actions so I can’t give you specific help on this.

That said, all of these cases involve:

  • timed actions using either timer.* or enterFrame events
  • vector math 
  • … more

I used to help out with a YouTube show for Corona called “Corona Geek”.

On the show I made many demo games including:

Other useful links:

Hey and welcome to the forums!

There are actually some easy ways of implementing this. Much of it depends on what type of game you are working on, i.e. is your survival game top down perspective, isometric, platform, etc.

For some general idea, let’s assume you have two variables: local player and local enemy. You could require the physics system and create a physics body for both the player and the enemy. This way you have access to physics collision detection. You could then, perhaps, set a timerwhere the enemy begins to move towards the player. This movement could be created using transitions or by applying linear velocity (see docs). For instance, you could state that every 250ms, the enemy begins to move to the x and y coordinates of the player, i.e. to player.x and player.y. So, if the player doesn’t move, the enemy will catch him. This might seem a bit rudimentary, but it’ll get you started. You can later on try to work out some “predictive system” that will try to move the enemy to where the player is headed to, so as to create the sense of the enemy trying to cut off the player’s movement path.

I see that roaminggamer also already posted a response. He is pretty much the go-to-guy with any Corona question or problem, so I am sure that you’ll find plenty of wisdom in his answer and GitHub archive :stuck_out_tongue: