• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

[Tạo 2D Platformer Game với Godot] Part 10: Player’s Attack

AnonyViet by AnonyViet
January 27, 2023
in Tips
0

In this part, I will show you how to make the character attack and program the enemy to identify the Player so that they can attack in the next part.

Join the channel Telegram of the AnonyViet 👉 Link 👈

Attack setting

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.5)
            animation.play("dungyen")

        if is_on_floor():
            if Input.is_action_just_pressed("ui_accept") :
                chuyendong.y = -250
        else:
            animation.play("nhay")
    if Input.is_key_pressed(KEY_J):
        animation.play("tancong")
    
    chuyendong.y += trongluc 
    chuyendong.normalized()
    chuyendong = move_and_slide(chuyendong,Vector2.UP)

On the above code, I have just added 2 basic lines so that you can run the Attack Animation and I guarantee many of you will think it works but it is actually ARE NOT the above code will not be able to work.

If you press the . key J to run the attack animation it won’t be possible because it’s animation currently being used and the animation using it is stand still.

var dangtancong = false
func _physics_process(delta):
    
    if !dangtancong:
        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.5)
            animation.play("dungyen")

        if is_on_floor():
            if Input.is_action_just_pressed("ui_accept") :
                chuyendong.y = -250
        else:
            animation.play("nhay")
    if Input.is_key_pressed(KEY_J) and is_on_floor():
        dangtancong = true
        chuyendong.x = 0
        animation.play("tancong")
    
    chuyendong.y += trongluc 
    chuyendong.normalized()
    chuyendong = move_and_slide(chuyendong,Vector2.UP)

And I will have the above code.

Here I create a new variable var dangtancong= false This variable is used to determine if you are performing an attack

I put all the moves in and jumped in if !dangtancong: is because I will check if I am not attacking, I can move normally, but if I am attacking, the code inside is not running and you will not be able to move or run while attacking.

At the line if Input.is_key_pressed(KEY_J) and is_on_floor(): :

+ I add is_on_floor() is to make sure I can’t attack while jumping.

+ I will set Dangtancong to true to signal that you are performing an anim attack.

+ I lethuyendong.x = 0 is to stop and perform attack anim, you cannot run and attack at the same time.

When I came here, I was still missing one thing, which is to turn the variable Dangtancong about false when it’s over animation attack.

func _on_AnimatedSprite_animation_finished():
    if animation.animation == "tancong":
        dangtancong = false

[Tạo 2D Platformer Game với Godot]  Part 10: Attack [Tạo 2D Platformer Game với Godot]  Part 10: Player 5's AttackI will have one more function and this function is connected to the signal from the AnimatedSprite node

extends KinematicBody2D

var tocdo = 200
var trongluc = 10
var chuyendong = Vector2()
var huong_dichuyen = 1
onready var animation = $AnimatedSprite
var dangtancong = false
func _physics_process(delta):
    
    if !dangtancong:
        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.5)
            animation.play("dungyen")

        if is_on_floor():
            if Input.is_action_just_pressed("ui_accept") :
                chuyendong.y = -250
        else:
            animation.play("nhay")
    if Input.is_key_pressed(KEY_J) and is_on_floor():
        dangtancong = true
        chuyendong.x = 0
        animation.play("tancong")
    
    chuyendong.y += trongluc 
    chuyendong.normalized()
    chuyendong = move_and_slide(chuyendong,Vector2.UP)


func _on_AnimatedSprite_animation_finished():
    if animation.animation == "tancong":
        dangtancong = false


So I will have the entire code as above.[Tạo 2D Platformer Game với Godot] Part 10: Player’s Attack

Here is the final result

Summary

In the next part, I will show you how to set up attacks on enemies.

The article achieved: 5/5 – (100 votes)

Tags: AttackGameGodotPartPlatformerplayerstạovới
Previous Post

Instructions to adjust DNS to 1.1.1.1 to speed up web surfing

Next Post

How to Auto fish 100% success in Play Together

AnonyViet

AnonyViet

Related Posts

How to add sliders to Facebook Stories to easily rewind videos
Tips

How to add sliders to Facebook Stories to easily rewind videos

April 21, 2026
How to change the default font on Windows 10
Tips

How to change the default font on Windows 10

April 13, 2026
5 tips for using a Browser to replace an App (helps save RAM, time and money)
Tips

5 tips for using a Browser to replace an App (helps save RAM, time and money)

April 13, 2026
How to make funny MeMe photos without Photoshop within 10 seconds
Tips

How to make funny MeMe photos without Photoshop within 10 seconds

April 11, 2026
How to quickly design your own Logo without Photoshop
Tips

How to quickly design your own Logo without Photoshop

April 10, 2026
How to convert Website into App on Windows
Tips

How to convert Website into App on Windows

April 9, 2026
Next Post
How to Auto fish 100% success in Play Together

How to Auto fish 100% success in Play Together

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

How to turn off password suggestions on iPhone: Prevent Safari from auto-filling passwords

How to turn off password suggestions on iPhone: Prevent Safari from auto-filling passwords

April 27, 2026
Instructions for looking up prescriptions on VNeID instead of paper medical books

Instructions for looking up prescriptions on VNeID instead of paper medical books

April 26, 2026
Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

April 25, 2026
Compare Poco X8 Pro and Poco X7 Pro: A new step for the “king” of mid-range performance

Compare Poco X8 Pro and Poco X7 Pro: A new step for the “king” of mid-range performance

April 24, 2026
How to turn off password suggestions on iPhone: Prevent Safari from auto-filling passwords

How to turn off password suggestions on iPhone: Prevent Safari from auto-filling passwords

April 27, 2026
Instructions for looking up prescriptions on VNeID instead of paper medical books

Instructions for looking up prescriptions on VNeID instead of paper medical books

April 26, 2026
Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

April 25, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

How to turn off password suggestions on iPhone: Prevent Safari from auto-filling passwords

How to turn off password suggestions on iPhone: Prevent Safari from auto-filling passwords

April 27, 2026
Instructions for looking up prescriptions on VNeID instead of paper medical books

Instructions for looking up prescriptions on VNeID instead of paper medical books

April 26, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply