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
I 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.
Here is the final result
Summary
In the next part, I will show you how to set up attacks on enemies.