0 votes

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

Godot version 3.2.3
in Engine by (14 points)

1 Answer

0 votes

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.

by (54 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.