Trying to make a door work

Godot Version

Door:

Player

@onready var player = get_node(“res://Scenes/Player.tscn”)

Door.

@onready var animation = $AnimatedSprite2D
@onready var collision = $CollisionShape2D

var is_open = false

func _on_interact_collision_area_entered(area):
player = area.get_parent()
if player is Player and is_open == false:
collision.set_deferred(“disabled”, true)
animation.play(“Open”)
is_open = true

if player is Player and is_open == true:
	collision.set_deferred("disabled", false)
	animation.play("Close")
	is_open = false

Player:

Object interaction.

if Input.is_action_just_pressed("Interact"):
	interact_collison.set_deferred("disabled", false)
	animation.play("Interact")
else:
	interact_collison.set_deferred("disabled", true)

Question

I’m trying to make a door so that it changes it’s sprite to the open or close animation as well as the collision disabling or enabling when the interaction hitbox of the player touches the door interact hitbox.


This is my door scene tree.


This is my player scene tree.

Subscribe to the body_entered signal from your Area2D

area.body_entered.connect(open_door)

You explained what you expect to happen but it would help to know what actually happens as well.

Do you have other interactables working? Seems like this snippet makes your player’s interaction area seldom active


make sure to paste script with proper formatting

The player interaction does work, but when I use it on the door nothing happens. Though I did try this.

if player is Player:
		collision.set_deferred("disabled", true)
		animation.play("Open")
		is_open = true

And got rid of the close if statement, and it worked so it seems the is_open doesn’t work with it but i’m not sure what to do to check if it is open or not.

Aha you should be using a elif statement here, the two if statements happen in order, so is_open is set to true, then it checks if is_open is true and sets it back to false. elif means only one of the statements will run, not both.

if player is Player and is_open == false:
	collision.set_deferred("disabled", true)
	animation.play("Open")
	is_open = true

elif player is Player and is_open == true:
	collision.set_deferred("disabled", false)
	animation.play("Close")
	is_open = false

yep it worked, silly me.