Kill an enemy when a key is pressed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dany_pitre

Hi ! I’m having an issue…
I try to make my player kill an enemy if it enters the enemy’s zone and press space bar, input named “kill”. the player can kill the enemy only if it’s the same “type”. Everything works before I code to make the player kill the enemy only if the player hot the kill input.

Here is my code without the need of an input:

func _on_enemy_white_check() → void:
if type == 0 :
emit_signal(“kill”)
score += score
Here, everything works perfectly. the enemy dies and the score rises.

Here is my code with the need of an input:
func _on_enemy_white_check() → void:
if type == 0 and Input.is_action_pressed(“kill”):
emit_signal(“kill”)
score += score

what is the issue… ?

Thanks a lot !!

Hold the space bar down and then enter the zone and see if that kills the enemy.

timothybrentwood | 2021-04-30 00:00

Yes it does ! I dont understand why…

dany_pitre | 2021-04-30 00:01

When is on_enemy_white_check called?

exuin | 2021-04-30 00:02

I have an Area2D on my enemy. When the player enters it:

func _on_Area2D_body_entered(body: Node) -> void:
     emit_signal("white_check")

dany_pitre | 2021-04-30 00:04

So that function only runs once when the player enters the area? You need to have the function run every time the player presses the kill button instead.

So instead of checking if the player is pressing the kill button when they enter the white area, you should check if the player is in the white area when they press the kill button. Use the _unhandled_input() function for this.

exuin | 2021-04-30 00:06

Oh god, that’s brillant. I’ll try this, thanks ! I’ll watch a tutorial on this function, i’ve never use it before.

dany_pitre | 2021-04-30 00:10

I definitivly don’t understant why to use this method haha…

dany_pitre | 2021-04-30 00:20

:bust_in_silhouette: Reply From: magicalogic

Have you defined the action in the project settings?
You also need to use Input.is_action_just_pressed(“kill”) because this happens just once for each enemy.