In this section, I will show you how to combine animations in movement to create a complete character as well as show you how to make the character jump.
| Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Create Folder
![[Tạo 2D Platformer Game với Godot] Part 5: Character Design 10 [Tạo 2D Platformer Game với Godot] Part 5: Character Design](https://anonyviet.com/wp-content/uploads/2021/11/10-11-2021-08-41-36.png)
In the previous section, I forgot to guide you to create more Folders to be able to manage files neatly and cleanly. So you create yourself 2 new Folders in Scenes, Map and Player.
Map: Used to store the levels of the game.
Player: Used to contain Scenes related to Player.
F5
Oh, and I forgot to tell you that when you press F5 you will run the default scene and press F6 and you will run the selected scene.![[Tạo 2D Platformer Game với Godot] Part 5: Character Design 11 [Tạo 2D Platformer Game với Godot] Part 5: Character Design 7](https://anonyviet.com/wp-content/uploads/2021/11/10-11-2021-08-45-08.png)
You can change it in Project -> Project Settings
Animation
![[Tạo 2D Platformer Game với Godot] Part 5: Character Design 12 [Tạo 2D Platformer Game với Godot] Part 5: Character Design 8](https://anonyviet.com/wp-content/uploads/2021/11/10-11-2021-08-46-24.png)
In part 3, I showed you how to create Animation for characters and now I will show you how to apply it.
Code
onready var animation= $AnimatedSprite
To use Animations, you must have access to the AnimatedSprite node..
There are two ways to access it:
- Create a border and assign an AnimatedSprite node to it, when assigning a node to a variable you need to add onready before var.
- You can call it directly using the ” $ ” symbol (eg $AnimatedSprite.position) or using get_node() ( eg get_node(“AnimatedSprite”).position)
extends KinematicBody2D
var tocdo = 200
var trongluc = 7000
var chuyendong = Vector2()
var huong_dichuyen
onready var animation = $AnimatedSprite
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)
animation.play("chay")
scale.x = scale.y * huong_dichuyen
else:
chuyendong.x = lerp(chuyendong.x,0,0.1)
animation.play("dungyen")
chuyendong.normalized()
move_and_slide(chuyendong,Vector2.UP)
So, I will add a few lines in the above code to make the character move with animation.
Here I will explain:
Current animation.play() :
- That I will call the animation variable that the animation variable I have assigned is the AnimatedSprite node so I can access it and after accessing it I will use the function play() of the AnimatedSprite node to run the animation. The animation name is the name of the animation you created in the AnimateSprite node.
Current scale.x :
- This is the line that will allow you to rotate the character left and right.
- scale.y ( is 1) * Huong_dichuyen (left press is -1 right is 1 so if left press = -1 then scale.y is 1 * with Huong_dichuyen is -1 it will output -1).
- scale.x if it is -1, it will rotate left, but if it is equal to 1, it will rotate right. You can test in Transform -> Scale of any node.
- And I put it in the first if function so that when the character moves it rotates, otherwise it won’t rotate.
extends KinematicBody2D
var tocdo = 200
var trongluc = 10
var chuyendong = Vector2()
var huong_dichuyen
onready var animation = $AnimatedSprite
func _physics_process(delta):
huong_dichuyen = Input.get_axis("ui_left","ui_right")
if huong_dichuyen != 0:
chuyendong.x = lerp(chuyendong.x,huong_dichuyen * tocdo,0.5)
animation.play("chay")
scale.x = scale.y * huong_dichuyen
else:
chuyendong.x = lerp(chuyendong.x,0,0.1)
animation.play("dungyen")
if is_on_floor():
if Input.is_action_just_pressed("ui_accept") :
chuyendong.y = -250
else:
animation.play("nhay")
chuyendong.y += trongluc
chuyendong.normalized()
chuyendong = move_and_slide(chuyendong,Vector2.UP)
Above is the code that I have edited and optimized it.
Current if is_on_floor() :
- That I will check that the character it is standing on the floor.
- If I stand on the floor and I press the button Way then switchong.y = -250 ie the character will move up 1 bit and then the machine will read the code from above and see the line moving.y += in luc then it will move down again.
Still line else: :
- Is the opposite of is_on_floor() when I have jumped on it ie no longer on the floor, I will run the jumping animation.
![[Tạo 2D Platformer Game với Godot] Part 5: Character Design 13 [Tạo 2D Platformer Game với Godot] Part 5: Character Design 9](https://anonyviet.com/wp-content/uploads/2021/11/10-11-2021-09-48-45.png)
In the running animation, I found it to be quite slow and inconsistent, so go in and adjust the speed from 5 fps to 10 fps.
![[Tạo 2D Platformer Game với Godot] Part 5: Character Design [Tạo 2D Platformer Game với Godot] Part 5: Character Design](https://anonyviet.com/wp-content/uploads/2021/11/10-11-2021-09-51-03.png)
This is the final result.
summary
That’s it, in this part I have guided you to make the character jump and apply animation to the movement.










