• 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.

Notes and Friction control the character's 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 view web access history in the anonymous mode (Incognito) of Chrome
Tips

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
How to graft the peach branch based on electric poles, family cycling, uncle
Tips

How to graft the peach branch based on electric poles, family cycling, uncle

August 21, 2025
Unlock checkpoint 72h often have photos already but hang nick
Tips

Unlock checkpoint 72h often have photos already but hang nick

August 19, 2025
GIF image creation tips with high quality snipping tool
Tips

GIF image creation tips with high quality snipping tool

August 19, 2025
How to unlock Facebook to download your latest photos
Tips

How to unlock Facebook to download your latest photos

August 18, 2025
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
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

Đá Gà Online Sonclub Đỉnh Cao Giải Trí 2025

August 22, 2025
How to view web access history in the anonymous mode (Incognito) of Chrome

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
How to automatically erase the web history after escaping to absolutely secure

How to automatically erase the web history after escaping to absolutely secure

August 22, 2025
Stainless steel flange price list at Asia Industry

Stainless steel flange price list at Asia Industry

August 21, 2025

Đá Gà Online Sonclub Đỉnh Cao Giải Trí 2025

August 22, 2025
How to view web access history in the anonymous mode (Incognito) of Chrome

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
How to automatically erase the web history after escaping to absolutely secure

How to automatically erase the web history after escaping to absolutely secure

August 22, 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

Đá Gà Online Sonclub Đỉnh Cao Giải Trí 2025

August 22, 2025
How to view web access history in the anonymous mode (Incognito) of Chrome

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 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