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.
Code Friction controls character speed
In this part we will have to code quite a lot
You declare everything for yourself
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)
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)
Next in the else: movement you delete chuyendong.x = 0
replaced masat = true
–>
Continue the code for yourself:
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.yGiả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