• 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 6: Notes and Friction controls character speed

AnonyViet by AnonyViet
May 21, 2024
in Tips
0

Continuing with the game programming series in this article, I will guide you to create friction to control speed and after movement. After you have coded the character to move, you need to control the character's running speed. In which the friction force will decelerate the character

Join the channel Telegram belong to AnonyViet 👉 Link 👈

Notes are used to annotate the line of Code you will write about. This helps you read the code later and understand the effects of that code.

When your lines of code are few, it may be okay, but if your code is about a few hundred lines and has no notes, it's a real nightmare:

To take notes, just add the # sign and write the text out, for example: #dichuyen, #jump, etc.

[Lập Trình Game] Lesson 6: Notes and Friction controls character speed

Code Friction controls character speed

In this part we will have to code quite a lot

You declare everything for yourself

code Friction controls character speed–>

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed

Const: full is a function that anyone who learns basic programming will know. const is a function that fixes the value in your variable, for example: const conga_trongchuong = 5 then known conga_trongchuong This is always equal 5 cannot be changed, if you want to change the version, you must convert from const to var. And in the photo above, it's okay if you leave Var out.

There are a few new variables, you'll probably understand by looking at the names

Next we code the speed reduction part

Current:

chuyendong.x += tocdo -> chuyendong.x = min(chuyendong.x+giatoc, tocdo_toida)

chuyendong.y-= tocdo -> chuyendong.x = max(chuyendong.x-giatoc, -tocdo_toida)

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed–>

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed

chuyendong.x = min(chuyendong.x+giatoc, tocdo_toida): means: min and max always go together. You don't need to care about this. You just need to know min and max. These 2 things mean when moving right, it's min. When moving left, it's max.

chuyendong.x That is, you call the variable moving along the positive x axis (positive is the right, negative is the left) + with acceleration being the start it will accelerate at a speed of 20 and tocdo_toida being the maximum speed when you move

chuyendong.x = max Same as above but just reversed.

Declare yourself below process_physical a variable as masat assign it = false (bool type)

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed

Next in the else: movement you delete chuyendong.x = 0 replaced masat = true

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed

–>

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed

Continue the code for yourself:

[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed
[Lập Trình Game]  Lesson 6: Notes and Friction controls character speed

View Full Code:

extends KinematicBody2D

var chuyendong = Vector2()
const UP = Vector2(0,-1)
const tocdo_toida = 100
const giatoc = 50
const trongluc = 20
const nhaycao = -500
func _physics_process(delta):
    chuyendong.y += trongluc
	var masat = false
	# di chuyen cua nhan vat
	if Input.is_action_pressed("ui_right"):
		chuyendong.x = min(chuyendong.x+giatoc, tocdo_toida)
		$Player.play("Run")
		$Player.flip_h = false
	elif Input.is_action_pressed("ui_left"):
		chuyendong.x = max(chuyendong.x-giatoc,-tocdo_toida)
		$Player.play("Run")
		$Player.flip_h = true
	else:
		$Player.play("Idle")
		masat = true
	#nhan vat nhay
	if is_on_floor():
		if Input.is_action_just_pressed("ui_up"):
			chuyendong.y += nhaycao
		if masat == true:
			chuyendong.x = lerp(chuyendong.x,0,0.2)
	else:
		if chuyendong.y 

Giải thích Code:

if masat == true:

chuyendong.x = lerp(chuyendong.x,0,0.2) : if masat = true nghĩa là nếu masat bằng true thì nó sẽ thực hiện cái bên trong if masat = true.

Trong bài trước bạn đã code 3 phần di chuyển if – elif – else trong đó if = phải, elif = trái , else = đứng im và chúng ta để hàm masat = true ở else nghĩa là khi chạy masat sẽ = false (có thể hiểu true là 1 false là 2) if và elif nên nó sẽ không có gì xuất hiện và khi nhân vật dừng lại masat sẽ = true vì chúng ta đã khai báo ở else:

lerp(chuyendong.x,0,0.2) :chuyendong.x gọi hàm chuyendong trục x(ở đây sẽ ko có trục phải hay trái vì phải và trái áp dụng khi bạn di chuyển trái hoặc phải còn đây là đứng yên) khi kết thúc hành động chạy nó sẽ chạy đoạn đằng sau là 0,0.2 (0 là trục y gán nó =0)(0.2 là trục x bạn có thể gán nó = bao nhiêu cũng được nhưng càng nhỏ ma sát càng lớn) nó sẽ đẩy player thêm 0.2m sau khi di chuyển.

Nếu thấy tốc độ chậm thì bạn có thể thay đổi và lưu ý rằng bạn chỉnh thong số gì cũng được vì đó là project game của bạn không phải của mình và mình chủ, chỉ bạn cách làm game cơ bản để các bạn làm.

Và thế là xong rồi ^^ trong bài tiếp theo sẽ là di chuyển giữa sence này sang sence khác có thể hiểu nôm na là di chuyển từ màn này sang màn khác

Previous Post

How to use Gemini 1.5 Pro for free on Google AI Studio

Next Post

7 best ways to protect your PC on public WiFi

AnonyViet

AnonyViet

Related Posts

How to add sliders to Facebook Stories to easily rewind videos
Tips

How to add sliders to Facebook Stories to easily rewind videos

April 21, 2026
How to change the default font on Windows 10
Tips

How to change the default font on Windows 10

April 13, 2026
5 tips for using a Browser to replace an App (helps save RAM, time and money)
Tips

5 tips for using a Browser to replace an App (helps save RAM, time and money)

April 13, 2026
How to make funny MeMe photos without Photoshop within 10 seconds
Tips

How to make funny MeMe photos without Photoshop within 10 seconds

April 11, 2026
How to quickly design your own Logo without Photoshop
Tips

How to quickly design your own Logo without Photoshop

April 10, 2026
How to convert Website into App on Windows
Tips

How to convert Website into App on Windows

April 9, 2026
Next Post
7 best ways to protect your PC on public WiFi

7 best ways to protect your PC on public WiFi

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Create an avatar to celebrate April 30 with a beautiful red flag shirt with yellow stars

Create an avatar to celebrate April 30 with a beautiful red flag shirt with yellow stars

April 30, 2026
How to get 2 months of Super Duolingo for free worth 300k

How to get 2 months of Super Duolingo for free worth 300k

April 30, 2026
How to create a Face Sticker Collection using ChatGPT

How to create a Face Sticker Collection using ChatGPT

April 29, 2026
How to install the cute Bongo Cat mouse pointer for Windows

How to install the cute Bongo Cat mouse pointer for Windows

April 29, 2026
Create an avatar to celebrate April 30 with a beautiful red flag shirt with yellow stars

Create an avatar to celebrate April 30 with a beautiful red flag shirt with yellow stars

April 30, 2026
How to get 2 months of Super Duolingo for free worth 300k

How to get 2 months of Super Duolingo for free worth 300k

April 30, 2026
How to create a Face Sticker Collection using ChatGPT

How to create a Face Sticker Collection using ChatGPT

April 29, 2026
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

Create an avatar to celebrate April 30 with a beautiful red flag shirt with yellow stars

Create an avatar to celebrate April 30 with a beautiful red flag shirt with yellow stars

April 30, 2026
How to get 2 months of Super Duolingo for free worth 300k

How to get 2 months of Super Duolingo for free worth 300k

April 30, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

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