• 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 to control the character’s speed

AnonyViet by AnonyViet
February 2, 2023
in Tips
0

Continuing with the game programming series in this article, I will guide you to create friction to control speed and after moving. After you have code for the character to move, you need to control the speed of the character. In which friction will slow down the character

Join the channel Telegram of the AnonyViet 👉 Link 👈

Notes are used to comment on what line of code you will write about. This helps later when you read the code, you can understand the effect of that code.

When your line of code is small, it may be okay, but code that is a few hundred lines, without notes, it is really a nightmare :<

To take notes, you just need to add the # sign and then write the text out for example: #dichuyen, #jump,..

  Notes and Friction force control character speed

Code Friction force control character speed

In this part we will have to code quite a lot

You declare the entire variable for yourself

code Friction force control character speed–>

[Lập Trình Game]  Lesson 6: Notes and Friction to control the character's speed 11

Const: full is a function that anyone who learns basic programming will know already. const is a function that fixes the value of your variable, for example: const conga_trongchuong = 5 then you known conga_trongchuong this is always equal to 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 to let Var all out.

There are a few new variables, you can understand by looking at the name

Next we code the speed reducer

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 force control character speed 12–>

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

chuyendong.x = min(chuyendong.x+giatoc, tocdo_toida): ie: min and max always go together this you don’t need to care, just know min and max 2 this is when the right movement is min the left movement is max.

chuyendong.x is that you call the variable x-axis positive (positive is right, negative is left) + with giatoc being the beginning it will accelerate at 20 and tocdo_toida is the maximum speed when you move

chuyendong.x = max same as above but only reversed.

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

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

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

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

–>

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

Add your own code:

[Lập Trình Game]  Lesson 6: Notes and Friction force control character speed 17
[Lập Trình Game]  Lesson 6: Notes and Friction to control the character's speed 18

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 <0:
			$Player.play("Jump")
		else:
			$Player.play("Fall")
		if masat == true:
			chuyendong.x = lerp(chuyendong.x,0,0.4)
	chuyendong = move_and_slide(chuyendong,UP)

Code explanation:

if masat == true:

chuyendong.x = lerp(chuyendong.x,0,0.2) : if masat = true means if masat is true then it will execute the inner if masat = true.

In the previous post you have coded 3 parts to move if – elif – else where if = right, elif = left, else = stand still and we leave the function masat = true in else which means that when running masat will = false (maybe) understand true is 1 false is 2) if and elif so it won’t appear and when the character stops masat will = true because we declared in else:

lerp(chuyendong.x,0,0.2) :chuyendong.x call the x-axis transfer function (here there will be no right or left axis because right and left apply when you move left or right and this is stationary) at the end of the run action it will run the following line as 0.0.2 (0 is y axis assign it = 0)(0.2 is x axis you can assign it = whatever but the smaller the bigger the friction) it will push the player 0.2m more after moving.

If you find the speed slow, you can change it and note that you can adjust any parameter because it’s your game project, not yours and the owner, just showing you how to make a basic game for you to do.

And that’s it ^^ in the next lesson will be moving between one sence to another, which can be roughly understood as moving from one stage to another.

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

Tags: characterscontrolFrictionGamelậpLessonNotesSpeedtrình
Previous Post

REBO Biology – How to learn Biology with AR virtual reality technology

Next Post

Instructions to get someone else’s IP to find your location online

AnonyViet

AnonyViet

Related Posts

27 interesting tricks on CMD you may not know
Tips

27 interesting tricks on CMD you may not know

June 24, 2025
iMyfone anyrecover: Restore data deleted on Windows 100%
Tips

iMyfone anyrecover: Restore data deleted on Windows 100%

June 23, 2025
Learn how VPN protects you from hackers
Tips

Learn how VPN protects you from hackers

June 22, 2025
Download Ebook Ethical Hacking & Penetration Testing Guide
Tips

Download Ebook Ethical Hacking & Penetration Testing Guide

June 21, 2025
List of the best temporary virtual mail creation websites
Tips

List of the best temporary virtual mail creation websites

June 20, 2025
How to get email (gmail) a series from Facebook comments
Tips

How to get email (gmail) a series from Facebook comments

June 19, 2025
Next Post
Instructions to get someone else’s IP to find your location online

Instructions to get someone else's IP to find your location online

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

27 interesting tricks on CMD you may not know

27 interesting tricks on CMD you may not know

June 24, 2025
Ktimer: scheduled to turn off the phone, hibernate easily for busy people

Ktimer: scheduled to turn off the phone, hibernate easily for busy people

June 24, 2025
iMyfone anyrecover: Restore data deleted on Windows 100%

iMyfone anyrecover: Restore data deleted on Windows 100%

June 23, 2025
Things to know when buying Galaxy Ring

Things to know when buying Galaxy Ring

June 23, 2025
27 interesting tricks on CMD you may not know

27 interesting tricks on CMD you may not know

June 24, 2025
Ktimer: scheduled to turn off the phone, hibernate easily for busy people

Ktimer: scheduled to turn off the phone, hibernate easily for busy people

June 24, 2025
iMyfone anyrecover: Restore data deleted on Windows 100%

iMyfone anyrecover: Restore data deleted on Windows 100%

June 23, 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

27 interesting tricks on CMD you may not know

27 interesting tricks on CMD you may not know

June 24, 2025
Ktimer: scheduled to turn off the phone, hibernate easily for busy people

Ktimer: scheduled to turn off the phone, hibernate easily for busy people

June 24, 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í SHBET 23win https://kubet88.yoga/ KUBET BK8 Bj88 https://u888b.site/

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í SHBET 23win https://kubet88.yoga/ KUBET BK8 Bj88 https://u888b.site/

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