In the previous section, I showed you how to create the most basic enemy, in this part I will show you how to make the enemy move on a floating surface.
| Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Enemy
![Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 12 Creating 2D Platformer Game with Godot]Part 8: Enemy Design](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-02-29.png)
With the surface like above, I guarantee that when your enemy moves to one of the two outer corners it will drop but usually in platformet games when it moves to the corner it will return like and hit the wall in the front part.
So in this section I will show you how to do this.
Add node
First, you add yourself a new node, RayCast2D (this is the node used to query for targets that collide with rays).![Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 13 Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 9](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-07-14.png)
This is a raycast and I’m going to use it to check if I’ve reached the corner I’ll call the enemy to turn around.![Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 14 Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 10](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-14-53.png)
I’ll make it shorter and turn it on.
![Creating 2D Platformer Game with Godot]Part 8: Enemy Design 15 Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 11](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-14-39.png)
func _physics_process(delta):
chuyendong.x = tocdo * huong_dichuyen
chuyendong.y += trongluc
chuyendong = move_and_slide(chuyendong,Vector2.UP)
animation.play("dichuyen")
if is_on_wall():
huong_dichuyen = huong_dichuyen * -1
scale.x = scale.y * huong_dichuyen
if $RayCast2D.is_colliding() == false:
huong_dichuyen = huong_dichuyen * -1
scale.x = scale.y * huong_dichuyen
Here, I only added 3 lines of code, not much more.
First, the if line:
- I’m going to call the raycast node and check if it’s colliding with something, if not then I’ve reached the corner then come back.
- The remaining 2 lines are used to rotate the enemy.
- the line scale.x = … actually you can leave the if outside, but not in the function, but I don’t because to optimize it, I put it in the if statement, so it only runs when it happens. If you leave it out, it always runs -> what the result is, you can guess.
![Creating 2D Platformer Game with Godot]Part 8: Enemy Design Creating 2D Platformer Game with Godot]Part 8: Enemy Design](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-20-21.png)
This is the result after I run the game.
How it works
If you do not understand, I will explain in more detail here.![Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 17 Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 13](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-21-19.png)
Here is the enemy when it’s not out of the corner, and the Raycast it is colliding with the tilemap as shown in the image.
![Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 18 Creating a 2D Platformer Game with Godot]Part 8: Enemy Design 14](https://anonyviet.com/wp-content/uploads/2021/11/12-11-2021-12-22-19.png)
And here is the enemy, when it reaches the corner, you see that the raycast has come out of the tilemap (the raycast collides at the sharp corner of the arrow), when it goes out, it will not collide with anything Also, in the script, I added an if to check if it doesn’t collide, then go back.
Summary
So I have guided you through how to create the most basic character, in the next part will be moving between levels, killing enemies.










