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 my first character.
Join the channel Telegram belong to Anonyviet 👉 Link 👈 |
Create character
First we open the software. Please pay attention to the section scene
This is where you will do your node. View Post What is node?
Here you can choose Other Node Or 2D Sense All. This article I choose Other Node
When created, it will appear below, and I change my name World:
Next, press the mark + Or press the combination Ctrl + A To create new nodes under the world
And you find Kinematicbody2d for yourself:
Then stay Kinematicbody2d select Collsetshape2d and Sprite okay
And here after creating you have 1 character moving Basic can be anything (the main character, the enemy, ..)
Kinematicbody2d: Means dynamic body (used to create objects as 1 character)
Sprite: Used to create photos for your character
Collsetshape2d: The collision shape (used to determine the collision with another object)
Part Sprite You click and drag icon.png Or any image you want to make a character, dragging into the texture.
And here it has appeared:
Next click Collsetshape 2D and in Inspector select Shape> New Rectangles2d
At the character's face interface, you Pull the dot in the blue line and the red dot in the purple line so that it is equal to the sprite.
Code for character moving
So already Done the character creation Next is the code.
Click on Kinematicbody2d And choose for yourself as a photo to create a script:
Script: The place where you will code and code will apply to run your game and character
- Language: Language used for programming
- Path: Path to save
- Load: Create script
And press Load.
After clicking Load, the code interface will appear, ban Delete all content from line 4 to line 17
Declare variables
Get yourself a variable declaration code as follows:
Anyone who programmed then knows how to declare the variable ^^ Basically and whoever does not know, I explain the following:
Var: Declare a variable to use it. Example: Var Tocdodichuyen, Var Ketqua, Var huongdichuyen
So Var Chuyendong = Vector2 () in which Vector2 () is?
Vector2 (): A variable used for mathematics in 2D and in mathematics, you know the coordinate axis x, y, …
Here:
- x = horizontal motion
- y = straight motion
- FUNC _PHYSICS_PRocess is a physical process that means the movement of kinematicbody2d
- FUNC _READY is the process of ready: When you declare a variable in Ready it will be loaded when downloading the game
And in Physics_process, you also declare a variable as above, but when it moves it, it will be loaded
Pass: yes it is okay
Motion code
Code for me the following paragraph. This is the code Right motion, left
In here I will explain the paragraph IF statement okay. Whoever learned programming, the IF statement is not difficult
if input.is_ACTION_Prisheded (“UI_RIGHT”): is: When you press the right arrow key, it will move to the right and vice versa Elif is left and Else: It will stand still after moving.
Move_and_slide (move and surf): Help your character move
Press F5 or F6 To try it out.
F5: Run the default scene
F6: Running Scene is editing. Example: worldnumberone is the default scene and worldnumbervietnam Is scene being edited. When pressing F5 even in scene worldnumbervietnam Then it will run Worldnumeberone If pressed F6 it will run worldnumbervietnam without running Worldnumeberone )
If it is like this, you can adjust it:
Click as photos:
We are as photos:
When you press this it will help you Fix the ingredients inside it.
And if you press the lock, it will Node lock Again on the screen can not be transferred and cannot press
Then we pull it out between:
If this is like this, press the right arrow and the left to see it moves:
If you want Move up, down then the code is as follows:
And I have instructed you to program a basic character, then the next article will be about: Friction force after running, gravity, jump, ground.
Goodbye and see you again in the following article.
Deathgm!