• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

[Lập Trình Game] Lesson 10: Creating Enemies in Godot Programming – Part 1

AnonyViet by AnonyViet
February 2, 2023
in Tips
0

In this article, I will guide you to create enemies for our characters with Godot game programmer. Enemies will cause the character to lose blood or you have to fight the enemy. And this part has some parts that can be very difficult and unsuccessful, so be prepared.

Join the channel Telegram of the AnonyViet 👉 Link 👈

And let’s get started.

Create an enemy

First create a new Sense and include the 2D kinematicbody the child node is AnimatedSprite and CollsionShape2D then press Ctrl + WILL save.

Creating Enemies in Godot Programming

If you are using anonyviet’s assets, there are enemy assets at: asset-player > NPC > enemy assets

Asset download link

You can choose any one.

Create a new SpriteFrame

Creating Animation is a move

download asset create godot enemies

If the photo is blurry, you guys Import come back

Next set collision for it, here I set the lower part not the upper one so that the upper part can let the character jump and destroy it

set collision

Lock it and create a new script and this script will be named enemy

create enemy scripts

We will create the following variables

var Vitri = 1
var  vantoc = Vector2.ZERO //hoặc Vector2() đều giống như nhau
var  Tocdo = tùy bạn
var Trongluc = tùy bạn
var  FLOOR = Vector2(0,-1)

here const is the statement used to lock the number, that is, when you declare a variable with const, it will lock the number and never be able to modify its value.

code create enemy enemy godot engine

Next we will create a physical motion function

And will code like this

Assign gravity to it

vantoc.y  =  trongluc

Assign speed to it

vantoc.x  += tocdo * delta * vitri (Here * delta to reduce enemy speed)

Add move_and_slide statement to be able to move

vantoc = move_and_slide(vantoc)

Next you will instance it outside of our sence:

[Lập Trình Game]  Lesson 10: Creating Enemies in Godot Programming - Part 1 6

And instance it out

[Lập Trình Game]  Lesson 10: Creating Enemies in Godot Programming - Part 1 7

Run the game and you will see it move

[Lập Trình Game]  Lesson 10: Creating Enemies in Godot Programming - Part 1 8

Here, I set the collection mode to see it, so there’s nothing wrong

Next we will code so it can move left, and animation

And here I will have 1 command is:

if is_on_wall() : is_on_wall() is a statement that returns true if the Body is on the Wall, and it only executes when called with move_and_slide

And next below if is_on_wall() we will add one more line of code:

vitri = vitri * -1 and this line when touching something vitri is 1 then it will * -1 and it will move backwards

Oh and if you run the game and see that the character is running slowly, check if move_and_slide has FLOOR, if not, add it if it is but still slow, adjust the speed to about 20-30

And next we will code the animation for it

if vitri == 1:
$AnimatedSprite.flip_h = false
else:
$AnimatedSprite.flip_h = true

The above 2 lines are the 2 lines that will change the image of the enemy, flip_h = false the picture will flip to the right flip_h = true is to the left

if vitri == 1 : means that the position is equal to 1 but = 1, it will move to the right but our enemy moves to the right first, so flip_h will = false

And vice versa

Then please call animation move for me

$AnimatedSprite.play("Move")

how to create enemies

Mine is fine after running the game

And in the next post, I will guide you to create raycast so that the enemy can move on the tilemap as shown below without dropping.

enemy code in godot engine

The article achieved: 5/5 – (100 votes)

Tags: creatingEnemiesGameGodotlậpLessonPartProgrammingtrình
Previous Post

[Lập trình game] Lesson 11: Godot’s Raycast2D Enemy – Part 2

Next Post

Download AnyRecover 2.5 Full Key – Recover Deleted Data

AnonyViet

AnonyViet

Related Posts

Instructions on how to format text on the Windows 11 notepad
Tips

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
4 ways to fix bluetooth connectivity on Windows 11
Tips

4 ways to fix bluetooth connectivity on Windows 11

August 8, 2025
How to know the computer is tracked and processed by Keylogger
Tips

How to know the computer is tracked and processed by Keylogger

August 7, 2025
Opal: Create applications who do not need to write code
Tips

Opal: Create applications who do not need to write code

August 3, 2025
How to activate a new Start menu on Windows 11
Tips

How to activate a new Start menu on Windows 11

July 29, 2025
Intellgpt: AI tool for osint and data science
Tips

Intellgpt: AI tool for osint and data science

July 28, 2025
Next Post
Download AnyRecover 2.5 Full Key – Recover Deleted Data

Download AnyRecover 2.5 Full Key - Recover Deleted Data

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
Online driving exam preparation: Support theory and practice

Online driving exam preparation: Support theory and practice

August 15, 2025
How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
Online driving exam preparation: Support theory and practice

Online driving exam preparation: Support theory and practice

August 15, 2025
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply