Add a New Script to the Enemy Node
Add a New Script to the Enemy Node
Save it as Enemy.gd
Save it as Enemy.gd
Enemies will patrol back and forth between two points.
Enemies will patrol back and forth between two points.
We will need a starting position.
We will need a starting position.
Then move a certain distance
Then move a certain distance
Then turn around and repeat.
Then turn around and repeat.
Add two new variables
Add two new variables
move_speed and move_dir.
move_speed and move_dir.
Note the @export annotation
Note the @export annotation
Now we can change the values in the inspector and give different values for different enemies
Now we can change the values in the inspector and give different values for different enemies
We also need non-exposed variables for
We also need non-exposed variables for
the Starting position and the
the Starting position and the
Ending position
Ending position
When the level loads
When the level loads
We can calcuate the two positions:
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.
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.
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.
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.
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.
Try changing the values of the enemy and test the game.
You will notice that the enemies rise but then stop when
You will notice that the enemies rise but then stop when
they have reached the target position.
they have reached the target position.
If we reset the target position to the starting
If we reset the target position to the starting
position we create sine wave motion.
position we create sine wave motion.
This code waits until the enemy’s position is the target position,
This code waits until the enemy’s position is the target position,
then it reverses the direction.
then it reverses the direction.
Change Enemy Colour
Change Enemy Colour
On the enemy model
On the enemy model
Locate the Surface Material Override
Locate the Surface Material Override
Add a new StandardMaterial3D for index 0 (The spikes)
Add a new StandardMaterial3D for index 0 (The spikes)
Click on the Material you have just added.
Click on the Material you have just added.
Change the settings as you wish.
Change the settings as you wish.
Add another StandardSurfaceMaterial3D to index 1
Add another StandardSurfaceMaterial3D to index 1
This is the blue block.
This is the blue block.
Change the colour to red
Change the colour to red
Up the metallic and the Specular
Up the metallic and the Specular
Now the enemies look more dangerous!
Now the enemies look more dangerous!
GROUPS (Tags)
GROUPS (Tags)
We need a way to identify the player when it hits the enemy.
We need a way to identify the player when it hits the enemy.
Click on the Player Node then open the Groups tab.
Click on the Player Node then open the Groups tab.
This is found next to the Inspector tab.
This is found next to the Inspector tab.
Type Player and click on Add
Type Player and click on Add
Now click on your Enemy Node
Now click on your Enemy Node
Go to the Node Tab
Go to the Node Tab
Select Signals
Select Signals
And choose body_entered
And choose body_entered
Double-click the body_entered signal
Double-click the body_entered signal
Connect from the Enemy node.
Connect from the Enemy node.
This will add an _on_body_entered function to your Enemy script.
This will add an _on_body_entered function to your Enemy script.
When a body enters the Area3Ds space:
When a body enters the Area3Ds space:
Check if that body is part of the Player group.
Check if that body is part of the Player group.
If it is, call the game_over method of the Player.
If it is, call the game_over method of the Player.