Chest open/close animation when accessing

Godot Version

4.3 stable

Question

I’m trying to have a simple chest animation were it opens when you interact (UI displays) and then closes when you interact again with the same input. I was obviously able to get the open to work fine but have failed at getting it to play the closed animation.

here’s what i have for the open part. I am very new this is my first project so the code speak goes over my head sometimes.

func player_interact() → void:
Input.is_action_just_pressed(“interact”)
toggle_inventory.emit(self)
animation_player.play(“chestopen”)

Your current code is slightly off but you have the right idea.

First you check for the interact input, then you need to check the state of the chest, which is either open or closed. Based on that you play the specific animation.

If you dont have a “chestclose” animation then you can play the “chestopen” animation backwards instead

Try something like so:

var chest_open: bool = false

func player_interact() → void:
    if Input.is_action_just_pressed(“interact”) and chest_open:
        toggle_inventory.emit(self)
        animation_player.play(“chestopen”)
        chest_open = true
    elif Input.is_action_just_pressed(“interact”) and not chest_open:
        toggle_inventory.emit(self)
        animation_player.play(“chest_close”)
        chest_open = false

every attempt I try and give it a state to check it just skips chestopen and goes straight to close. probably tried a dozen variants of this at least. i also tried getting rid of the close animation and playing open backwards. same result.

I see what I did wrong, I will copy the correct code below use that instead:

var chest_open: bool = false

func player_interact() → void:
    if Input.is_action_just_pressed(“interact”) and not chest_open:
        toggle_inventory.emit(self)
        animation_player.play(“chestopen”)
        chest_open = true
    elif Input.is_action_just_pressed(“interact”) and chest_open:
        toggle_inventory.emit(self)
        animation_player.play(“chest_close”)
        chest_open = false

I switched around the not chest_open and chest_open conditions, sorry for that.

that did the trick thanks for your time! ive got alot of learning to do.

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