• 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

Instructions for cleaning LG TV with Antigravity to help it run faster
Tips

Instructions for cleaning LG TV with Antigravity to help it run faster

October 30, 2026
Quét lỗ hổng bảo mật cho Vide Coding bằng Security Audit Skill
Tips

Quét lỗ hổng bảo mật cho Vide Coding bằng Security Audit Skill

October 27, 2026
Instructions for registering for OxionAI Pro for 1 year with many powerful AI models
Tips

Instructions for registering for OxionAI Pro for 1 year with many powerful AI models

October 24, 2026
How are Windows 11 Pro and Windows 10 Pro different?
Tips

How are Windows 11 Pro and Windows 10 Pro different?

October 23, 2026
Instructions for using CleanYourTV: Remove ads on Android TV, Google TV
Tips

Instructions for using CleanYourTV: Remove ads on Android TV, Google TV

October 23, 2026
How to use Messenger without a Facebook account
Tips

How to use Messenger without a Facebook account

October 22, 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

Recent News

Instructions for receiving Avira Prime 3 months for free, no credit card

Instructions for receiving Avira Prime 3 months for free, no credit card

October 31, 2026
Instructions for cleaning LG TV with Antigravity to help it run faster

Instructions for cleaning LG TV with Antigravity to help it run faster

October 30, 2026
Cách hạn chế Synology NAS bị Ransomware .L0CK3D: Cách khắc phục

Cách hạn chế Synology NAS bị Ransomware .L0CK3D: Cách khắc phục

October 30, 2026
Instructions for installing CleanYourTv to remove Android TV ads

Instructions for installing CleanYourTv to remove Android TV ads

October 29, 2026
Instructions for receiving Avira Prime 3 months for free, no credit card

Instructions for receiving Avira Prime 3 months for free, no credit card

October 31, 2026
Instructions for cleaning LG TV with Antigravity to help it run faster

Instructions for cleaning LG TV with Antigravity to help it run faster

October 30, 2026
Cách hạn chế Synology NAS bị Ransomware .L0CK3D: Cách khắc phục

Cách hạn chế Synology NAS bị Ransomware .L0CK3D: Cách khắc phục

October 30, 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

Instructions for receiving Avira Prime 3 months for free, no credit card

Instructions for receiving Avira Prime 3 months for free, no credit card

October 31, 2026
Instructions for cleaning LG TV with Antigravity to help it run faster

Instructions for cleaning LG TV with Antigravity to help it run faster

October 30, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

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