How to make it so that when a body enters an Area2D node and a button is pressed (for example, Enter), a function is performed

Godot Version

4.3

Question

How to make it so that when a body enters an Area2D node and a button is pressed (for example, Enter), a function is performed.
image

extends Area2D

@onready var arrow = load("res://Scenes/arrow.tscn")

func _on_body_entered(body: Node2D) -> void:
	if body.name == "Player":
		if Input.is_action_just_pressed("enter"):
			shoot()

func shoot():
	var instance_arrow = arrow.instantiate()
	add_child(instance_arrow)

Please help!

create a variable
var is_inside_area = false

change the state in the on_body_entered

listen input if it’s true. is that what you mean?

1 Like

It worked! Thank you! I only added :

func _on_body_exited(body: Node2D) -> void:
	if body.name == "Player":
		is_inside_area = false

and

func _process(delta: float) -> void:
	if Input.is_action_just_pressed("enter") and is_inside_area:
		shoot()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.