Okay, and welcome to the series Game programming with Godot Engine 3.2 and today I will guide you to move up, down, left, right for my first character.
Join the channel Telegram belong to AnonyViet 👉 Link 👈 |
Character creation
First we open the software. You pay attention in Section scene
This is where you will implement your Node. View Posts What are Nodes??
Here you can choose Other Node Or 2D sense all okay. This article I choose Other Node
Once created, it will appear below, and I renamed it World:
Next, press the sign + or press the combination Ctrl + A to create a new Node under World
And you find KinematicBody2D for myself:
Then stay KinematicBody2D select CollsionShape2D and Sprite Please
And here after creating, you have 1 Moving Characters Basic can be anything (main character, enemy,..)
KinematicBody2D: Means Kinetic Body (used to create object like a character)
Sprites: Use to create your character’s photo
CollsionShape2D: Collision shape (used to determine where to collide with another object)
In the sprite you click and drag icon.png or any other image you want to use as a character, drag into the texture.
And here it appeared:
Next click on 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 it is equal to the sprite.
Code for the character to move
So that’s it Finished character creation Next is Code.
You click KinematicBody2D and choose for yourself as the image to create the script:
Script: Where you will code and that code will apply to run the game and your character
- Language: The language used for programming
- Path: Path to save
- Load: Create scripts
And press Load.
After clicking Load, the interface of the Code writing page will appear delete all content from line 4 to line 17
Declare variable
Type yourself a code that declares a variable as follows:
Anyone who has programmed, knows how to declare a variable. ^^ It’s basic and for those who don’t know, I’ll explain as follows:
var: declare a variable to use it. VD: var tocdodichuyen, var ketqua, var huongdichuyen
So what is var conversion = Vector2() where Vector2() is ?
Vector2(): is a variable used for math in 2D and in math, you know the x, y coordinate axes, …
in here:
- x = horizontal movement
- y= straight motion
- func _physics_process is a physical process i.e. kinematicbody2D
- func _ready is process ready: 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 when it moves
pass: yes or no
Motion code
Code for me the following part. Here is the code right and left movement
Here I will explain the passage if . statement Please. Who has learned programming, the if statement is no longer difficult
if input.is_action_pressed(“ui_right”): is: when you press right arrow key it will move right And vice versa with elif is to the left and else: is that it will stay still after moving.
move_and_slide (move and glide): make your character move
Press F5 or F6 for a test run.
F5: run default scene
F6: run the scene being edited. VD: worldnumberone is the default scene and worldnumbervietnam is the scene being edited. When pressing F5 even in the scene worldnumbervietnam then it will run worldnumeberone if press F6 it will run worldnumbervietnam without running worldnumeberone )
If it is like this, you can edit it:
Click as picture:
We get as picture:
When you press this it will help you fix the components inside it.
And if you press the lock it will node lock back on the screen can’t move nor can’t press
Then we drag it to the middle:
Okay like this, you 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 have instructed you to program a basic character, then the next lesson will be about: Friction force after running, Gravity, Jumping, Ground.
Goodbye and see you in the next post.
DeathGM!