To create an enemy in the game, follow the steps below in order:
1. Create a new Scene by clicking the + sign on the right side of the "endzone" tab.
2. Since the "Enemy" Node will not enter any collide process, create a Node named "Node2D" by clicking directly on the "2D Scene" option.
3. Change the name "Node2D" to "Enemy" and save it as "enemy.tscn" with Ctrl-S.
4. To create an AnimatedSprite2D Node, while the "Enemy" Node is selected, click the + sign under the Scene tab or the Ctrl-A key combination to create a new Node.
5. In the Create New Node window that appears, search for AnimatedSprite2D in the Search line, select it, and click the "Create" button.
6. While AnimatedSprite2D is selected, under the Animation section of the Inspector tab, select the New SpriteFrames option from the drop-down box on the right side of the Sprite Frames line.
7. A window opens at the bottom of the Godot IDE. click on the Add frames from Sprite Sheet button in this window.
8. In the first window that opens, enter the assets/sprites directory and in the second window, open the slime_green.png file in the directory.
9. Make the following arrangements in the window that opens and click the "Add 4 Frame(s)" button.
10. While "AnimatedSprite2D" is selected and the image is selected in the scene, change its speed to 10 FPS. click on the "Autoplay on Load" button.
11. Since we created the "Enemy" Node in a reusable Scene, we can also use it for the "Enemy" Node. While the "Enemy" Node is selected, when we click the "Link" button under the "Scene" tab, select the "scenes\endzone.tscn" file from the window that appears and click the "Open" button to create a Node called "Endzone" under the "Enemy" Node.
12. To add a new Node, while the "Endzone" Node is selected, click the + sign under the Scene tab or the Ctrl-A key combination to create a new Node.
13. In the Create New Node window that appears, search for and select the expression CollisionShape2D in the Search line and click on the Create button.
13. While "CollisionShape2D" is selected, select "New RectangleShape2D" from the box to the right of the "Shape" line under the "Inspector" tab.
14. Arrange the rectangle in the scene to cover the enemy image.
15. Save the "enemy" Scene with Ctrl-S.
16. Select the "game" Scene and drag the "enemy.tscn" file under the "FileSystem" tab and drop it to the location shown in the image below in the scene.
17. When we run the game, the enemy moves in the place where it is. The game restarts when the player enters the enemy area.
18. To make the Enemy object move in the game, click on the "Attach a new or existing script to selected node" button while the "enemy" Scene and "enemy" Node are selected.
19. In the window that opens, click on the button at the far right of the "Path" line.
20. In the window that opens, go to the "scripts" directory, select the "enemy.gd" file, and click the "Open" button.
21. When we return to the previous window, click the "Create" button.
22. This process creates a script for the "Enemy" Node. There are 2 automatically created functions in the script. Delete _ready() function in script.
extends Node2D
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
When we run the game, the enemy continues on its way when it comes to the wall.
23. We will use the raycast Node to make the enemy return when it comes to the wall. With the "Enemy" Scene and "Enemy" Node selected, create a new Node by clicking the + sign under the Scene tab or by pressing the Ctrl-A key combination.
24. In the Create New Node window that appears, search for RayCast2 in the Search line and select it, and click the Create button.
25. Bring the starting point of the arrow placed on the stage to the center of the Enemy image. turn the tip of the arrow to the right.
26. Change the name of the "RayCast2D" Node to "RayCastRight".
27. While the "RayCasRight" Node is selected, create a second Node named "RayCastRight2" with the Ctrl-D keys. change the name of the Node named "RayCastRight2" to "RayCastLeft" and turn the arrow tip to the left.
28. Select and drag the "RayCastRight" and "RayCastLeft" Nodes and drop them to the line before the _process() function definition. hold down the "Ctrl" key before dropping. This process creates two variables.
extends Node2D
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
29. Thus, Enemy moves between two obstacles in the scene.
30. While the "enemy" Scene is selected, drag the "AnimatedSprite2D" Node and drop it on the line before the _process() function definition. hold down the "Ctrl" key before dropping. This process creates a variable.
extends Node2D
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
31. Edit the _process() function content as follows. In this case, Enemy changes its image according to the direction of travel while moving between two obstacles.
extends Node2D
const SPEED = 60
var direction = 1
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if ray_cast_right.is_colliding():
direction = -1
animated_sprite.flip_h = true
if ray_cast_left.is_colliding():
direction = 1
animated_sprite.flip_h = true
position.x += direction * SPEED * delta
32. In order for the game to end by falling down when the Player enters the Enemy area, open the Script with the second button from the right of the "Endzone" Node line while the "endzone" Scene and "Timer" Node are selected and edit it as follows.
extends Area2D
@onready var timer: Timer = $Timer
func _on_body_entered(body: Node2D) -> void:
Engine.time_scale = 0.5
body.get_node("CollisionShape2D").queue_free()
timer.start()
func _on_timer_timeout() -> void:
Engine.time_scale = 1.0
get_tree().reload_current_scene()
Thus, since all Collision operations are disabled when the Player enters the Enemy area, falls.
Save all the scenes with Ctrl-S.