In the previous part, I showed you how to add animation to the character and that post was quite long.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
In this part I will show you how to design the character, we will add collision and then code for it to move and then jump.
CollisionShape
First, you have to add yourself CollisionShape2D
You click on CollisionShape2D in the Inspector panel in the Shape line, select [empty] -> choose the one that suits the character, in this case the first one.
After selecting, you align them evenly.
Then you press Ctrl + S to save. You create yourself a new folder named Scenes to store scenes by right-clicking and selecting new folder.
As above.
Then proceed to add Script to click objects.
You choose the save path in the Scenes folder and then click Create.
And this is Godot’s Script interface.
I will declare 4 variables as:
var tocdo = 200 var trongluc = 7000 var chuyendong = Vector2() var huong_dichuyen
And 4 variables have the same use as the name implies.
Then I will code it more like this:
func _physics_process(delta): chuyendong.y = trongluc * delta huong_dichuyen = Input.get_axis("ui_left","ui_right") if huong_dichuyen != 0: chuyendong.x = lerp(chuyendong.x,huong_dichuyen * tocdo,0.5) else: chuyendong.x = lerp(chuyendong.x,0,0.1) chuyendong.normalized() move_and_slide(chuyendong,Vector2.UP)
Here I will explain this code for you to understand.
I use the function _physical_processs because it is a function that specializes in physics related problems.
The variable I have declared is Vector2() and Vector2() is a library and it works on ox and oy axes.
Current chuyendong.y += inluc * delta then:
- Chuyendong.y means I call the y-axis of the transform variable
- I will + it with the internal variable of 500 and the y axis is positive, it will move down.
- and * delta, this is the frame synchronization variable to make your game always synchronize the frames to avoid the phenomenon of frames showing fast or slow when the fps is low or something.
Current direction_dichuyen = Input.get_axis(“ui_left”,”ui_right”) :
- then huong_dichuyen is the variable I use to determine which key I am pressing and want my character to move in that direction
- Input.get_axis(“ui_left”,”ui_right”) is that I will call the Input Class then use the get_axis() function to determine that I am pressing the left (if left pressed it returns -1 ) good right? (if pressed to the right returns 1) ( Note: the get_axis() function is only available in Godot 3.4, if it is older, it must be used manually)
Current if huong_dichuyen != 0: :
- I will check if I am pressing a certain key ie I want it to move in the direction I press, it will execute the move command.
- chuyendong.x then chuyendong.x i.e. I call its x axis (because game Platformer but you only move left and right, not like Top Down, so use the x axis only)
- lerp(chuyendong.x,huong_dichuyen * tocdo,0.5) : lerp() is a function that returns the value from value this until maximum value that ( lerp(from,to,weight) weight is a value somewhere between 0 and 1 ). So that means I will call the first variable of lerp . function to be chuyendong.x because i want it to move from current speed until Max speed so i will let variable 2 is speed, also 3rd variable then you can set whatever you want in the range 0 to 1.
- and huong_dichuyen * tocdo is for me to know which direction to move)
Current else: :
- I’ll check if I don’t move, I’ll stay still
- I’m using lerp here because I want to create some friction before I stand still. If you don’t see any difference, you can change it chuyendong.x = lerp(conversion.x,0,0.1) Fort chuyendong.x = 0 to feel the difference.
Last two lines:
- transferong.normalized() I will normalize it.
- move_and_slide(movement,Vector2.UP) this is a function only in nodes Body and it allows you to move. The motion variable you always put in the 1st position, and the Vector2.UP called UP Direction and it is used to determine the location of wall, floor, ceiling. If you leave nothing, everything is a wall ( used for top down game) and if you let Vector2.UP then at the top you will be identified as bare and below is floor ( usually the platformer will add this so you can make the character jump).
Then you go out and add yourself a Camera2D variable.
And in Camera2D -> Inspector, turn on current, the new camera will work.
Next you will proceed to Instance scene player to the Map by clicking the button on the picture
Select Player.tscn then click Open.
So Player has been Instance to Scene Map
Then press F6 and press the . button arrow left, right to move and when running my game it will look like this.
If you think the camera is not good, you can adjust it in the Zoom section.
Summary
So that’s part 4 done, in this part I have guided you guys to make the character move and in the next lesson we will make it jump and apply animation to it.