• 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 write Shell Script in Linux/Unix
Tips

How to write Shell Script in Linux/Unix

December 6, 2025
How to completely uninstall WSL on Windows 11?
Tips

How to completely uninstall WSL on Windows 11?

December 6, 2025
Kinh nghiệm mua Laptop cho sinh viên nhóm ngành kế toán, kinh tế, xã hội…
Tips

Kinh nghiệm mua Laptop cho sinh viên nhóm ngành kế toán, kinh tế, xã hội…

December 5, 2025
8 tips for more successful PowerPoint presentations
Tips

8 tips for more successful PowerPoint presentations

December 4, 2025
Chrome extensions to improve productivity in 2021
Tips

Chrome extensions to improve productivity in 2021

December 2, 2025
How to create photos that run KPIs while crying using Gemini for the 12 zodiac animals
Tips

How to create photos that run KPIs while crying using Gemini for the 12 zodiac animals

December 1, 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

How to transfer music from other services to Apple Music, no app required

How to transfer music from other services to Apple Music, no app required

December 8, 2025
How to create Mind Map and Flowchart using ChatGPT is super simple

How to create Mind Map and Flowchart using ChatGPT is super simple

December 7, 2025
Free Gemini Google certification exam guide and Answers

Free Gemini Google certification exam guide and Answers

December 6, 2025
How to write Shell Script in Linux/Unix

How to write Shell Script in Linux/Unix

December 6, 2025
How to transfer music from other services to Apple Music, no app required

How to transfer music from other services to Apple Music, no app required

December 8, 2025
How to create Mind Map and Flowchart using ChatGPT is super simple

How to create Mind Map and Flowchart using ChatGPT is super simple

December 7, 2025
Free Gemini Google certification exam guide and Answers

Free Gemini Google certification exam guide and Answers

December 6, 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

How to transfer music from other services to Apple Music, no app required

How to transfer music from other services to Apple Music, no app required

December 8, 2025
How to create Mind Map and Flowchart using ChatGPT is super simple

How to create Mind Map and Flowchart using ChatGPT is super simple

December 7, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

trang chủ f168 nhà cái 78win https://www.qq8827.com/

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

trang chủ f168 nhà cái 78win https://www.qq8827.com/

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