Add a New Script to the Enemy Node

Save it as Enemy.gd

Enemies will patrol back and forth between two points.

We will need a starting position.

Then move a certain distance

Then turn around and repeat.

Add two new variables 

move_speed and move_dir.

Note the @export annotation

Now we can change the values in the inspector and give different values for different enemies

We also need non-exposed variables for

the Starting position  and the

Ending position

When the level loads

We can calcuate the two positions:

start_pos  is our enemy’s global_position, which is the position in the scene where we placed them. 

Add the move_dir vector to create our target_pos value. 

If our move_dir was (0, 5, 0), that means we will move 5 units upward along the y-axis. 

Now, in the _process method, we will add the code to move the enemy between the points.

Try changing the values of the enemy and test the game.

You will notice that the enemies rise  but then stop when

they have reached the target position.

If we reset the target position to the starting 

position we create sine wave motion.

This code waits until the enemy’s position is the target position, 

then it reverses the direction.

Change Enemy Colour

On the enemy model

Locate the Surface Material Override


Add a new StandardMaterial3D for index 0 (The spikes)

Click on the Material you have just added.

Change the settings as you wish.

Add another StandardSurfaceMaterial3D to index 1

This is the blue block.

Change the colour to red

Up the metallic and the Specular

Now the enemies look more dangerous!

GROUPS (Tags)

We need a way to identify the player when it hits the enemy.

Click on the Player Node then open the Groups tab.

This is found next to the Inspector tab.

Type Player and click on Add

Now click on your Enemy Node

Go to the Node Tab

Select Signals

And choose body_entered

Double-click the body_entered signal

Connect from the Enemy node.

This will add an _on_body_entered function to your Enemy script.

When a body enters the Area3Ds space:

Check if that body is part of the Player group.

If it is, call the game_over method of the Player.