I’m very new to Godot so i don’t know coding that much, but i need help with opening doors.
How do i make the doors open up when the player goes into the player detector and interacts to use the door, but can’t open it when the player isn’t in or has left the collision shape?
since for some reason it keeps opening even if they left the collision shape…
i have a “closed” “opening” and “open” animations for the door
have you tried making the collider smaller? a detection can happen when the player is barely touching the collider, so just trim the edges and make it a square.
How far away from the door can the player still interact with it? If it’s not touching the collision shape at all then it’s a problem with the code. Can I see that?
Use CharacterBody2D for the playable character, and add the following code to the character’s script.
extends CharacterBody2D
class_name Player
# When a player can interact with an object, add it to the array
var interactables := []
func _physics_process(delta: float) -> void:
# If the player presses the "interact" key, find the closest
# object from the array and activate it.
if Input.is_action_just_pressed("interact"):
var target = null
var distance := 9000000000000.0
for obj in interactables:
# I'm using the square of the distance for better performance.
var new_distance := position.distance_squared_to(obj.position)
if new_distance < distance:
distance = new_distance
target = obj
if target:
target.activate()
Then make sure the body_entered and body_exited signals of the player detector are connected to functions in the door script.
func _on_body_entered(body: PhysicsBody2D) -> void:
if body is Player:
body.interactables.append(self)
func _on_body_exited(body: PhysicsBody2D) -> void:
if body is Player:
body.interactables.erase(self)
Every interactable object also needs to have a function called “activate”.
func activate() -> void:
if state == CLOSED:
open_the_door_or_something()
# You can probably figure out the rest
This is probably it; because the function waits for the end of its animations to set player_entered to false, it could be that it’s triggered multiple times. And because it’s triggered multiple times the animation keeps resetting and doesn’t finish, which means player_entered doesn’t get set to false…