How to spawn enemies over time and approach the player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ReeFond

How do I spawn enemies and make them approach the center of the screen?
The player will try to avoid the enemy to survive.
I am making a game that looks something like this

:bust_in_silhouette: Reply From: Rickers

To make a basic enemy like the kind you want there are three things it needs to do:

Spawn at a random location.
Go towards Center.
Handle Collision.

So first you need to create an enemy scene, and give it the behaviour of “No matter what point you are at, go towards the center of the screen”.

To spawn the enemy in you could generate a random location in a number of ways, but the easiest way would be to create a pool of Vector2() coordinates which are then selected at random to determine the initial position. Then you need to make the enemy go from one point to another.

You could do this through a few methods, such as pathfinding, using a Path2D PathFollow (very strange method, but doable), but for the sake of time and simplicity you could use a tween that triggers in the ready() function. You’ll need a Tween Node to be a child node in the scene.

Then you need to reference the tween node in your enemy script and use the Interpolate Property function. The arguments put into the tween would be the starting coordinates (a vector2) of the and then the center point of the screen (there are lots of ways to find this).

some code like $Tween.interpolate_property("position", self.position,Vector2(central coordinates),time_you_want_tween_to_take,Tween.TRANS_LINEAR,Tween.TRANS_CUBIC $Tween.start()

You can alter most of the arguments or randomize the inputs to get some enemy vareity.

You’ll then need to give collision detection to the AI (or the player) by giving them a Collision Polygon.

Then you need to decide how to handle collision, i’d recommend using a Area2D entered signal in a the player script, which will then trigger whenever the enemy hits the collision box of the player.

If this has been confusing, sorry, but otherwise I hope this gives you a starting point.