Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How can i make enemies target the player?
Started by BulkOlav Oct 03 2018 06:51 AM

12 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

BulkOlav

[GLOBAL: userInfoPane.html]
BulkOlav
  • Observer

  • 15 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

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:



[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 361 posts
  • Corona SDK

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 timer where 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 :P



[TOPIC: post.html]
#4

BulkOlav

[GLOBAL: userInfoPane.html]
BulkOlav
  • Observer

  • 15 posts
  • Corona SDK

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 :D



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

I also forgot to mention.  SSK2 has 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.



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

This particular example might be interesting to you too:

 

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



[TOPIC: post.html]
#7

BulkOlav

[GLOBAL: userInfoPane.html]
BulkOlav
  • Observer

  • 15 posts
  • Corona SDK

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. 



[TOPIC: post.html]
#8

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 827 posts
  • Corona SDK

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?



[TOPIC: post.html]
#9

BulkOlav

[GLOBAL: userInfoPane.html]
BulkOlav
  • Observer

  • 15 posts
  • Corona SDK

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 :)



[TOPIC: post.html]
#10

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 827 posts
  • Corona SDK

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



[TOPIC: post.html]
#11

BulkOlav

[GLOBAL: userInfoPane.html]
BulkOlav
  • Observer

  • 15 posts
  • Corona SDK

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



[TOPIC: post.html]
#12

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 827 posts
  • Corona SDK

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.



[TOPIC: post.html]
#13

BulkOlav

[GLOBAL: userInfoPane.html]
BulkOlav
  • Observer

  • 15 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]