Next with the series of game programming with godot today I will guide you to the next lesson that is Gravity, Moving Surface and Jumping.
Join the channel Telegram belong to AnonyViet 👉 Link 👈 |
Well before you do, if you want to reopen the sence you’re working on now, look under the FileSystem section and double click on the sence you saved before and yours is saved here. World.tscn
Moving Surface: The moving surface is the part that we let our character move on it can be said to be like the road surface for us humans to walk on.
To create a surface, you do as I do and this is just the basics of the basics, if you want to create a moving surface like other games, it’s called tilemap and tileset I will guide you in the following article:
Create Node StaticBody2D under the authority of World
LIVE StaticBody I will change my name back to floor (double click to change the name) and I changed the name too KinematicBody Fort Player Please.
And also in Node Player i will create 2 Nodes To be Sprite: the photo for floor and Collsion
Collsion It is very difficult to explain here, so I will briefly explain it for you to understand. VD:
When you create a sprite without collsion then it’s just an image, a painted image. If you create 1 sprite + collection then it means it’s like if you press a bag of balls against the wall with your hand, it will collapse, that’s when there is no collsion and when there is collsion it is like inside the ball bag there is a brick then when you press the ball bag there is a brick you cannot squeeze it because the brick it is hard. Generally speaking sprite is the skin still collsion is bone in that skin.
Or you can understand collection is the frame to prevent the inner part for the sprite to work
Here StaticBody2D used for physical movement if you want to build a trap, bridge, something stationary or moving as an obstacle, use StaticBody2D.
Then you do the same Lesson 1 with Node player like this:
I will not give specific instructions, but you have to remember each post to go up.
On the floor, click on the sprite to notice the right part below the CanvasItem, then click on it Visiable > select Modulate and change the color to any color.
Here I change to black
And as the photo above you can see that sprite changed to blackn but what collsion it’s on top again sprite then you change positions collsion go up sprite or click the eye on the right collsion to hide it and when hidden you don’t need to worry because it just hides and doesn’t do anything.
And you lock it up like Node Player:
Well at the end of the post I will leave the zoom buttons with the keyboard for those of you who use a laptop without a mouse or a damaged mouse scroll button like me.
And choose floor You guys notice for me the 2 parts I’ve circled in the picture
The side dark white fruit is that it displays the grid (squares like pixels).
This grid is a bit small here, so I’ll zoom in
To zoom in, press the 3 dots next to it:
Choose the last one Configure Snaps. Change Grid Step Fort 32-32
And it looks like the picture:
You can move the floor but you can see that it is not in the correct box so Godot gave us one more function that is move in pixels.
On the one next to the one turn on pixels you click on floor and then turn it on:
Then our floor will move in pixels
And you put it down to make the floor move
Below the picture, if you pay close attention, you will see a blue umbrella, that umbrella is our screen, remember to put the floor inside this box.
Here I create more 1 the Node name is wall and there contains floor and floor I changed to 01
You set the location for floor then click on the 01 press combination Ctrl + EASY it will be duplicated and in the place of floor 01, you can pull it out if you don’t understand, look at the picture:
Doubled
Now pull out
It will come out 2
So with 2 you can do it continuously to create a longer moving floor
But before creating the floor, you need to check if the collsion is equal or not to check if you turn on the collsion of 01 and 02.
Like mine, it’s not very tight but the player still moves well. If yours is skewed, adjust it to be flat.
Now create multiple ones,
I have created it:
Run the program and you will see something like this:
Does it look pretty bad? But don’t worry you only learn the basics to do it and this is just a tutorial, but the first lesson will guide you to create 1 player and a complete tilemap and background, enemy too. .
You can move it down and it will be blocked by the floor
And that’s it, you have created the Moving Surface, followed by Gravity.
Gravitation:
When it comes to gravity, who doesn’t know 😀 the thing that helps us move easily.
To create gravity, you go to the player’s script.
Declare variable:
Declare 1 more gravity variable set it equal to 20
In physics code for me as follows
Chuyendong is what helps us move along the x, y coordinate axes: Y is down-up, X is right-left.
here chuyendong.y that is, use the variable to call the y-axis because when we declare chuyendong = Vector2()
mean change allowed to use x, y axes and here we call y-axis moving down by transition| chuyendong.y += tocdo means chuyendong.y = chuyendong + inluc
and it means when change y + axis with inluc is 20 then it will move down with 20 times gravity.
And run the game:
We will see it drop and its Floor Collsion is an obstacle for our player to prevent the player from falling.
If your migration is like mine:
Then you won’t be able to move up when you press the up arrow
And next is dancing.
Dance
To Code the dance part, you will code like me.
Delete section if input up and if input down Go
And change
if input ui_left thành > elif input.is_action ui_left
And the code adds an else part:
else:
The x-axis motion is 0|| = 0 rather than += (I will write it like this for you to understand and code it and also improve your coding ability)
After the code you press F6 to run the game will see it can move right to left and after stopping moving will stand still.
Next comes the jumping part.
Declare yourself a variable as:
Nhaycao and assign it with -500
Why assign it with -500 simply because when you create the gravity variable, our player will fall along the y-axis right? So here we assign -500 so that it will move upwards against gravity when jumping to a certain height because inluc is always set to 20, so after jumping it will force your player down.
You declare yourself 1 more variable UP = Vector2(0, -1)
Variable UP This is a variable that allows you to jump to 0, -1 || 0 is the x-axis, -1 is the y-axis
Next, give me the code for the part like the picture:
if is_on_floor
which means when you are on the floor (the floor we created) and only when on the floor it can do it.
if input ui_up
Well, if you read the previous posts, you already know
chuyendong.y += nhaycao
To be: change call y-axis and assign it with variable haycao when you press the up arrow key And When assigning it jumps why? Then I explained in the declaration of the variable nhaycao
Ah in the move_and_slide() more for me UP
Note:
Some notes when coding:
In the if statement you can notice the character >| The blur to the right of the command is what sorts the commands
That is, the if statement that you enter the line to code without >| then it will not execute eg this is not done
>| if
>|Chuyendong = 0
Then it failed to execute and reported an error
And if like this:
>| if
>| >| chuyendong = 2
Then it will work
How to move the sence and zoom keys for anyone who has a broken mouse wheel or uses a laptop:
- Ctrl + – (the – sign): zoom out
- Ctrl + + (the + sign there): zoom in
Middle click to move around or right click
Here you will see 3 white ones
- 1st one (4 those arrows): move Sprite
- 2nd one: Rotate the Sprite or press Ctrl to rotate
- 3rd one: Zoom in or out of the Sprite
That’s it, the next lesson will be About Tilemaps and Tilesets, and then the character’s movement will be.
DeathGM!