• 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

Windows 7/8.1/10 users will be upgraded to Windows 11 for free
Tips

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving ChatGPT Plus for 1 month for free in 2026
Tips

Instructions for receiving ChatGPT Plus for 1 month for free in 2026

July 28, 2026
Some reasons I don’t like Windows 11
Tips

Some reasons I don’t like Windows 11

July 27, 2026
Instructions on how to get free Facebook white ticks
Tips

Instructions on how to get free Facebook white ticks

July 27, 2026
Collection of Windows 11 wallpapers for computers and phones
Tips

Collection of Windows 11 wallpapers for computers and phones

July 26, 2026
Don’t install leaked Windows 11
Tips

Don’t install leaked Windows 11

July 25, 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

Recent News

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving the free Certified LLM Security Expert certificate

Instructions for receiving the free Certified LLM Security Expert certificate

July 30, 2026
MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

July 29, 2026
What is quantum resistant cryptography? Why must the Internet change?

What is quantum resistant cryptography? Why must the Internet change?

July 29, 2026
Windows 7/8.1/10 users will be upgraded to Windows 11 for free

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving the free Certified LLM Security Expert certificate

Instructions for receiving the free Certified LLM Security Expert certificate

July 30, 2026
MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

July 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

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving the free Certified LLM Security Expert certificate

Instructions for receiving the free Certified LLM Security Expert certificate

July 30, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

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

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

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