Okay, and welcome to the series Game programming with Godot Engine 3.2 And today I will guide you to move up, down, left, and right for your first character.
Join the channel Telegram belong to AnonyViet 👉 Link 👈 |
Create a character
First, let's open the software. Please pay attention in Section scene
This is where you will make your Node. View article What is Node??
Here you can choose Other Nodes Or 2D sense all ok. I chose this article Other Node
Once created, it will appear below, and I will change the name to World:
Next, press the check mark + or press combination Ctrl + A to create a new Node under World
And you look KinematicBody2D for myself:
Then stay KinematicBody2D select CollsionShape2D and Sprite Please
And here after creating it, you have it 1 Moving Character The Base can be anything (main character, enemy, etc.)
KinematicBody2D: Meaning: Kinetic Body (used to create an object like a character)
Sprites: Used to create images for your character
CollsionShape2D: Collision shape (used to determine where it collides with another object)
In section sprite Click and drag icon.png or any image you want to use as a character, drag it into the texture.
And here it appeared:
Next click CollsionShape 2D and stay Inspector select Shape > New RectangleShape2D
At the character's face interface, you Drag the dot on the blue line and the red dot on the purple line so that they are equal to the sprite.
Code for character movement
So that's it Completed character creation Next is Code.
You click KinematicBody2D and choose your own image to create script:
Script: Where you will code and that code will be applied to run the game and your character
- Language: Language used for programming
- Path: Path to save
- Load: Create script
And press Load.
After pressing Load, the Code writing page interface will appear Delete all content from lines 4 to 17
Declare variable
Type yourself a piece of code to declare the variable as follows:
Anyone who has programmed already knows how to declare variables ^^ It's basic and for those who don't know, I'll explain as follows:
var: Declare a variable to use it. For example: var tocdodichuyen, var result, var huongdichuyen
So var text = Vector2() where Vector2() is what?
Vector2(): is a variable used for mathematics in 2D and in mathematics, you know the x,y coordinate axis,…
in here:
- x = horizontal movement
- y= straight movement
- func _physics_process is the physical process meaning the movement process of kinematicbody2D
- func _ready is the ready process: when you declare a variable in ready it will be preloaded when the game loads
In physics_process, you also declare a variable as above, but it is loaded only when moving
pass: yes or no
Motion code
Code for me the following. Here is the code right and left movement
Here I will explain the passage if statement Please. If anyone has learned programming, the if statement is no longer difficult
if input.is_action_pressed(“ui_right”): is: when you press the right arrow key it will move to the right and vice versa elif is to the left and else. else: means it will stand still after moving.
move_and_slide (move and glide): helps your character move
Press F5 or F6 let's try it out.
F5: run the default scene
F6: Run the scene being edited. For example: worldnumberone is the default scene and worldnumbervietnam is the scene being edited. When pressing F5 even in scene worldnumbervietnam then it will run worldnumerone if press F6 it will run worldnumbervietnam without running worldnumerone )
If it is like this, please adjust it:
Click according to the picture:
We are like the picture:
When you press this it will help you Fix the components inside it.
And if you press the lock it will lock node on the screen, nothing can be moved or pressed
Then we drag it to the middle:
If it looks like this, press the right and left arrows to see it move:
If you want up and down movement then the code is as follows:
And I've already guided you through programming a basic character. The next lesson will be about: Friction after running, Gravity, Jumping, Ground.
Goodbye and see you again in the next post.
DeathGM!