Introduce
Hi guys, welcome to the next series on game programming with Godot Engine. Today I will guide you to program the Top Down game with Godot. In this article, I will guide you to move the character for the top down game.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
With the Top Down game, we don’t need gravity like the first series but just need to move in 4 directions. Usually if you search youtube for articles about basic top down game, you will see that people often use the if command to check Input and Moving, doing that takes a lot of time, so I’ll do one quickly :3.
Note: This will be song 1 for both seires
Create Project
Then 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 link, click Create Folder and then click Create & Edit to get started
Create Sense
Like the previous posts with Godot we will create a new Sense that will contain characters and other things
You can use either a Black Node or a 2D Node. I will use 2D Node and name it World then you press Ctrl + S to save it. I will save it in the folder containing Scenes (Sence).
Character Creation
You click the + sign next to the name of the Sense to create a new sence
Then click Other Node or + Under the word Sense to add Node. You guys find yourself Node KinematicBody2D. This is a “Kinematic Body” button that makes it easier for us to move and perform.
Then you guys add 2 Nodes as “Sprite” and “CollisionShape2D“.
Sprite: Is the image node for the parent node in short, the node that displays the image for the KinematicBody2D
CollisionShape2D: is the node representing the collision data in 2D space, ie the shapes used to collide. Anyone who has been in contact will understand.
Generally in Godot it is: KinematicBody2D is the soul – Sprite is the outer appearance – CollisionShape2D is the skeleton
I will rename the KinematicBody2D to Player for easy identification.
With Sprite, I don’t have an asset yet, so let’s temporarily use the icon.png available in godot’s filesystem :v next post I’ll find the asset.
You click on the Sprite and then click on the Icon, then drag over the Sprite’s Texture
Next we will set CollisionShape2D for it. Click on the collision node and then in the shape you choose a shape for it, it’s up to you here, I choose the square for it to be square with the Sprite
So we have created the basic character of the basic :’).
Then you can save it
Next will be the Script section, click on the paper with the blue + sign to create the script, remember to click on the Player node to create it, don’t create it in Sprite or CollisionShape2D.
Move Code
We will use 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 to advanced moves :3.
Create Function Take Input Data
We will create a new function called laydau_nhapvao(): ie the function will take all input from the outside. For example, if you press the K key, it will attack, that is to get information from the external input.
So I will create a func function laydau_nhapvao():
In it, I will create a variable that is vector_dauvao assigned = Vector2.ZERO or Vector2() (2 pieces = each other).
vector_dauvao is the variable used to define Vector2(x,y) ie the beginning of x,y. If left pressed, it will move left(Vectoc(-1,0)), and if pressed right will move right(Vector2(1,0)), up then (Vector2(0,-1) not is Vector2(0,1) as in math, in 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”): ie Input(input).get_action_strength(“ui_right”) (getting action strength can be understood as taking input as input). when right is pressed) then subtract Input.get_action_strength(“ui_left”) => ui_right(Vector(1,0)) and ui_left(Vector(-1,0)) when right pressing without left pressing the action of pressing that’s right Vector2 = Vector2(1,0) and since left side = 0 so 1-0 will = 1 (then when we move to the right Vector(1,0) the x it = 1 then we don’t move to the left so it = 0 when we move to the left it’s new = -1 so 1-0 will = 1 and = 1 then it moves to the right and to the left the opposite, vector_dauvao.y does the same)
Apply migration
Then I will add a command to apply movement to the character that is move_and_collide (moving and collision is different from move_and-slide (moving and gliding) it also has the function of pushing other objects :)) that’s right can push other objects if it is move_and_slide it only moves and glides but cannot be pushed otherwise. If you use Rigdbody2D, both slide and collide can push it :3 a nice feature of Godot )
Call the function to get data
Then we have to call it to 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 instance node player to test
Remember to drag the Player to the middle.
Then you press F5 or F6 to run the game (F5 selects the sence to run, F6 runs the sence you are working on) when running the game you press the arrow button to move. But you notice when moving horizontally with diagonal movement, the speed of cross movement is faster, not even like the other 2 moves, so we have 1 command to normalize it’s speed. .normalized().
So we’ll type
vector_dauvao = vector_dauvao.normalized()
Speed and Delta
I will use the export var variable to output to the Inspector panel (this is a pretty OK feature of Godot)
VD:
First we declare
export var speed = 100 so you can adjust as much as you like
Then we will * it in with vector_dauvao variable and * with delta always
You can do it like this.
But for me, I created a new variable containing the above 3 for easy understanding.
In laydau_nhapvao there is no delta so I will create 1 delta right on func laydau_nhapvao():
Then in_physical_process(deta): I just need to call it in.
Finish
So I finished lesson 1 in the series of game programming with Godot, then see you in the next post ;3