How can I interact with some objects?

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

Hello all, I want to interact with some objects in this scene, but dont know how to do that. Can someone tell me what I need to do?
enter image description here

:bust_in_silhouette: Reply From: johnygames

You need to place Area2D nodes on all the objects you wish to interact with. Then, when the character enters those areas, a signal is triggered letting Godot know that a particular area has been entered. You can combine Area2D signals with other code too. For example, you can have a door open whenever the character is within the door area AND a key is pressed at the same time.

Question: how do I implement this?I suppose I made a zone that the player enters, but when the key is pressed when the player is in the zone, nothing happens.My code:
extends Area

const CLOSED = 0
const OPEN = 1

var state = CLOSED

func _on_doorpost_and_door_area_entered(area):
if area.name == “Player_area” and Input.is_action_just_pressed(“ui_E”):
if state == CLOSED:
state = OPEN
$AnimationPlayer.play(“Open_door”)
$Sound_door_open.play()
else:
state = CLOSED
$AnimationPlayer.play_backwards(“Open_door”)
$Sound_door_closed.play()

deaverrer4 | 2020-04-26 16:19

Sorry for the late reply!

I suppose that using the area entered signal alone will not do the trick in your case, because it is only good for when you want to trigger actions immediately after the signal is emiited. What you might want to try instead is create a bool variable (let’s name it in_door_area for clarity’s sake), then have it set to true when the area_entered signal is emitted and have it set to false when the area_exited signal is emitted. Then you just check whether the in_door_area is true or false AND a certain key is pressed. I hope this helps!

johnygames | 2020-04-30 17:08