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,..
Code Friction force control character speed
In this part we will have to code quite a lot
You declare the entire variable for yourself
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)
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)
Next in the else: the movement you delete chuyendong.x = 0 replaced masat = true
–>
Add your own code:
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.
![[Lập Trình Game] Lesson 6: Notes and Friction to control the character’s speed [Lập Trình Game] Lesson 6: Notes and Friction to control the character’s speed](https://anonyviet.com/wp-content/uploads/2020/03/ximage001.png)
![[Lập Trình Game] Lesson 6: Notes and Friction force control character speed 16 code Friction force control character speed](https://anonyviet.com/wp-content/uploads/2020/03/ximage002.png)
![[Lập Trình Game] Lesson 6: Notes and Friction to control character speed 17 [Lập Trình Game] Lesson 6: Notes and Friction to control the character's speed 11](https://anonyviet.com/wp-content/uploads/2020/03/ximage003.png)
![[Lập Trình Game] Lesson 6: Notes and Friction to control the character's speed 18 [Lập Trình Game] Lesson 6: Notes and Friction force control character speed 12](https://anonyviet.com/wp-content/uploads/2020/03/ximage004.png)
![[Lập Trình Game] Lesson 6: Notes and Friction to control the character's speed 19 [Lập Trình Game] Lesson 6: Notes and Friction to control character speed 13](https://anonyviet.com/wp-content/uploads/2020/03/ximage005.png)
![[Lập Trình Game] Lesson 6: Notes and Friction force control character speed 20 [Lập Trình Game] Lesson 6: Notes and Friction force control character speed 14](https://anonyviet.com/wp-content/uploads/2020/03/ximage006.png)
![[Lập Trình Game] Lesson 6: Notes and Friction to control the character's speed 21 [Lập Trình Game] Lesson 6: Notes and Friction force control character speed 15](https://anonyviet.com/wp-content/uploads/2020/03/ximage007.png)
![[Lập Trình Game] Lesson 6: Notes and Friction force control character speed 22 [Lập Trình Game] Lesson 6: Notes and Friction force control character speed 16](https://anonyviet.com/wp-content/uploads/2020/03/ximage008.png)
![[Lập Trình Game] Lesson 6: Notes and Friction to control character speed 23 [Lập Trình Game] Lesson 6: Notes and Friction force control character speed 17](https://anonyviet.com/wp-content/uploads/2020/03/ximage009.png)
![[Lập Trình Game] Lesson 6: Notes and Friction force control character speed 24 [Lập Trình Game] Lesson 6: Notes and Friction to control the character's speed 18](https://anonyviet.com/wp-content/uploads/2020/03/ximage010.png)









