In this article, I will guide you to create characters for Shooter game with Godot.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
CREATE CHARACTERS
First of all, download the art.
Links: https://opengameart.org/content/animated-top-down-survivor-player
AnimatedSprite
First you delete the Sprite and then add yourself the AnimatedSprite node.
Then Create New Frame
Then add me
3 animations are : idle – move – shoot
Handgun – Rifle – Shotgun you can get inside
Drag them all into 3
Set CollisionShape2D again.
Code:
Next is the code. We will test if the player moves, it will run the animation move and stand still and run the animation idle, if it shoots, it will run the animation shoot.
Next because this is a shooter we have to let the character rotate in the direction of the mouse, then we have the command: look_at(get_global_mouse_position())
Next set the camera, let’s look at it easily:
Add the camera2D node in Zoom to make it easier to see guys.
WOMAN SUNSHINE
Next we will create bullets to shoot piu piu
Download link: https://drive.google.com/file/d/1fr4xNtR-s5qS49G5N98in3oTVlVJyxAN/view?usp=sharing
Please download.
Done then create a new sense
Original button is Area 2D to check when bullet collides
CollisionShape2D
Sprite
You drag the bullet image into the Sprite
Then set collisionShape2D too
Then change the name to Bullet
Create New Script
Here I will add a function so that the bullet can spawn somewhere you want and Damage (damage)
func start_at(pos, dir): position = pos rotation = dir velocity = Vector2(speed, 0).rotated(dir)
position =pos : means the position of the bullet will reject the pos
rotation = rotation of the bullet
Next, we will create a function to assign speed to the bullet so that it can fly
func _physics_process(delta): position += velocity * delta
Oh, remember to turn 2 buttons Sprite and Collishape Move horizontally if you don’t want it to shoot vertically
Then next we have to check if the bullet leaves the screen, it will self-destruct so that it can’t accumulate again causing lag.
Then you add the button VisibilityNotifier2D then adjust the dog is fine. Then connect the signal screen_exit
(quit screen)
Then in that sentence we add the command: queue_free()
Then next we’ll shoot it.
Before that you need to have the bullet’s sence to be able to add it and use onready, it will be loaded when running.
Then check if key is pressed F it will shoot
get_parent().add_child(b)
: means it will take the parents and then add_child ie add the bullet button if there is a video, I will illustrate it easier.
b.start_at(global_position,rotation)
: global_position = position of the character, rotation is the angle of rotation.
When shooting you will see it appear in the middle of the player, we need to adjust it outside the gun. Then do the following.
You add a Position2D button and then drag it to the place where you want the bullet to appear
Next is simple, you just need to add $Position2D before the global_position to spawn in the above place.
And if you run it, you will have a situation like the picture
And the next post will have a fix
Code Player:
extends KinematicBody2D export var speed = 200 onready var bullet = preload("res://Canh/Bulet.tscn") func _physics_process(delta): look_at(get_global_mouse_position()) laydau_nhapvao(delta) func laydau_nhapvao(delta): var vector_dauvao = Vector2.ZERO vector_dauvao.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left") vector_dauvao.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up") vector_dauvao = vector_dauvao.normalized() if vector_dauvao != Vector2.ZERO: $AnimatedSprite.play("move") else: $AnimatedSprite.play("idle") var dichuyen = vector_dauvao * speed * delta if Input.is_key_pressed(KEY_F): shoot() dichuyen = move_and_collide(dichuyen) func shoot(): var b = bullet.instance() get_parent().add_child(b) b.start_at($Position2D.global_position,rotation)