Introduce
Hello everyone, welcome to the next series about game programming with Godot Engine. Today I will guide you to program Top Down game with Godot. In this article I will guide you to move characters for top down game.
Join the channel Telegram belong to AnonyViet 👉 Link 👈 |
With the Top Down game, we don't need gravity like the first series, we just need to move in 4 directions. Usually, when you search YouTube for articles about basic top down games, you will see that people often use the if command to check Input and Moving and doing it like that takes a lot of time so I'll do it in a quick way :3.
Note: This will be lesson 1 for both seires
Create Project
So as usual we will create a new project.
Project Name: Project name
Project Path: Path
Renderer:
Choose OpenGL 3.0 if you want to make PC & Android games – Choose OpenGL 2.0 if you want to make web games.
After you have the path, click Create Folder and then click Create & Edit to start
Create Sence
Like the previous articles with Godot, we will create a new Sence that will contain characters and other things
You can use Black Node or 2D Node. I will use 2D Node and name it World then press Ctrl + S to save it. I will save it in the folder containing Scene.
Character Creation
Click on the + sign next to the Sence name to create a new sente
Then click Other Node or the + Sign under the word Sence to add a Node. You guys find yourself Node KinematicBody2D. This is a “Kinesthetic Body” button that helps us move and perform more easily.
Then you add 2 Nodes for yourself: “Sprite” and “CollisionShape2D“.
Sprite: Is the image node for the parent node, in short, it is the node that displays images for the KinematicBody2D.
CollisionShape2D: is the node that represents collision data in 2D space, that is, the shapes used for collision. Anyone who has ever been in contact with it will understand.
Generally in Godot it is: KinematicBody2D is the soul – Sprite is the outer shape – CollisionShape2D is the skeleton
I will change the name of KinematicBody2D to Player for easy identification.
For Sprite, I currently don't have an asset, so we'll temporarily use the icon.png available in godot's filesystem: v In the next post, I'll look for the asset.
Click on Sprite, then click on the Icon, then drag it over to the Sprite's Texture
Next we will set CollisionShape2D for it. Click on the collision node and then choose a shape for it. Here, I'll choose a square shape so it's square with the Sprite.
So we have created the basic character of the basic :').
After that, please save it
Next will be the Script part, click on the paper sign with the blue + sign to create the script. Remember to click on the Player button to create, not in Sprite or CollisionShape2D.
Migration Code
We will use it GDScript by Godot. This is Godot's own language based on Python and Lua.
As mentioned at the beginning of the article, I will guide you through advanced moves: 3.
Create a Function to Take Input Data
We will create a new function called laydau_nhapvao(): that is, the function will take all input information from the outside. For example, if you press the K key, it will attack, which is taking information from an external input.
So I will create the func function laydau_nhapvao():
In it, I will create a variable called vector_dauvao assigned = Vector2.ZERO or Vector2() (the two = each other).
vector_dauvao is the variable used to determine Vector2(x,y), which is the beginning x,y. If you press left, it will move left (Vectoc(-1,0)), and if you press right, it will move right (Vector2(1,0)), up then (Vector2(0,-1) not is Vector2(0,1) as in mathematics, in the game vice versa).
func laydau_nhapvao():
var vector_dauvao = Vector2.ZERO
vector_dauvao.x = Input.get_action_strength("ui_right")- Input.get_action_strength("ui_left")
vector_dauvao.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
move_and_collide(vector_dauvao)
Here:
vector_dauvao.x = Input.get_action_strength(“ui_right”) – Input.get_action_strength(“ui_left”): means Input(input).get_action_strength(“ui_right”) (getting action intensity can be understood as taking input when pressing right) then subtract Input.get_action_strength(“ui_left”) => ui_right(Vector(1,0)) and ui_left(Vector(-1,0)) when pressing right but not pressing left, the pressing action Right then Vector2 = Vector2(1,0) and since left side = 0 then 1-0 will = 1 (then when we move to the right Vector(1,0) the x = 1 then we don't move to the left so it = 0 when we move to the left it = -1 so 1-0 will = 1 and = 1 then it moves to the right and to the left vice versa, vector_dauvao.y is similar)
Apply migration
After that, I will add a command to apply movement to the character: move_and_collide (move and collide is different from move_and-slide (move and slide), it also has the effect of pushing other objects :)) yes yes If it is move_and_slide, it can only move and slide but cannot be pushed otherwise. If you use Rigdbody2D, both slide and collide can push it: 3 pretty cool features of Godot)
Call the function to get data
Then we have to call it into the physical process function (physical_process(delta)) or process (_process(delta)) you can see the basics here: Godot GDScript
Instance Node
Then go to the sense world and the player node instance to test
Remember to drag the Player to the middle.
Then you press F5 or F6 to run the game (F5 selects the scene to run, F6 runs the scene you are working on). When running the game, press the arrow button to move. But you notice that when moving straight horizontally and moving diagonally, the diagonal movement speed is faster and not as steady as the other two moves, so we have a command to normalize the speed as follows: .normalized().
So we'll type
vector_dauvao = vector_dauvao.normalized()
Speed and Delta
I will use the export var variable to export to the Inspector panel (this is a quite OK feature of Godot).
For example:
First we declare
export var speed = 100 and adjust it as much as you like
Then we will * it with the variable vector_dauvao and * with delta too
You can do this.
But for me, I created a new variable containing the above 3 things to make it easier to understand.
In laydau_nhapvao there is no delta, so I will create a delta right in func laydau_nhapvao():
Then in_physical_process(deta): I just need to call it in.
End
So we've finished lesson 1 in the game programming series with Godot, and see you again in the next lesson ;3