Enemies or monsters are an indispensable thing in Platformer or Rpg games. So, in this part I will show you how to design an enemy and how the design will last 2 or 3 parts.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Art
As for the Art part, you can download it via the au link: Desert Enemy Pixel Art Game Characters
After downloading, in Assets create a new folder called KeThu and copy all the art into it.
Create enemies
Create a new scene
Like Player, you will create a kinematicbody and here I rename it Cao (that fox) and add 2 more child buttons, AnimatedSprt and CollisionShape2D.
You create new Frames in AnimatedSprite.
Here I add 5 animations corresponding to art.
This is after adding the animation and it shows up.
You add CollisioShape to it
Done then rotate CollisionShape2D to landscape.
Click on the toolbar to make adjustments.
Then adjust the AnimatedSprite to fit the CollisionShape2D.
Note: You always take the CollisionShape as the alignment point and don’t change the position or something of the CollisionShape .
Done then save the scene, I create a new folder in Scenes called Kethu and save it there.
More Scripts
Next is the code part.
Add a little script.
extends KinematicBody2D var tocdo = 60 var trongluc = 10 var chuyendong = Vector2() var huong_dichuyen = 1 onready var animation = $AnimatedSprite func _physics_process(delta): chuyendong.x = tocdo * huong_dichuyen chuyendong.y += trongluc chuyendong = move_and_slide(chuyendong,Vector2.UP) if is_on_wall(): huong_dichuyen = huong_dichuyen * -1
Here, it is no different from Player as it needs 4 basic variables to be able to move and 1 variable to be able to use animation.
Change direction_dichuyen because this is AI so you don’t have to press the button to move it or anything, so I’ll set it to 1 by default.
At the function _physics_process(delta):
- The first 3 lines are not far away
- Current if is_on_wall() is a function that allows you to check if an enemy child is on the move and with whom it touches.
- Below I will check if moving to the right meets a wall or something, it will turn to the left to move forward and vice versa.
Then you Instance the fox into the Map so you can see it has moved.
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
At the function _physics_process(): then I added 2 command lines to be able to run the animation and rotate the image left and right.
This is the result.
Sometimes the Sprite will be inverted and you can correct it by adjusting the Flip_H of the Sprite or AnimatedSprite.
Summary
So in this part you can already make the most basic enemy, in the next part I will show you how to make it move on the surface.