Open and closing animations misbehaving

Godot Version

4.5.1

Question

I been using this approach to make a door open and close in gdscript, what do you think? this code lets you open the door but it only lets you close it if you spam the “Interact button” after it opened/played the animation in certain momentums. Basically something is not behaving properly and it only plays backwards when you press the interact button repetively after a few taps after pressing the interact button for the first time.

Is this confusing? what am trying to put in here? What could I do to refine this to make it just open and close normally?

There is no animation blending just an AnimationPlayer. This is inside a raycast3d in a node for a door.

If you share code, please wrap it inside three backticks or replace the code in the next block:

if Input.is_action_just_pressed("Interact"):
			animation_player2.play("open")
			sound.play()
			await animation_player.animation_finished
		
		if animation_player2.animation_finished && Input.is_action_just_pressed("Interact"):		
			animation_player2.play_backwards("open")
			sound.play()
			await animation_player.animation_finished
1 Like

Where do you call this code? Please share the full script.

1 Like

I think you should use booleans, what this should do is if the door is interacted with it will check if the animation is already playing, if it isn’t it should play, same for it closing. Hope this helps!

var opened = false
if Input.is_action_just_pressed("Interact"):
	if animation_player2.current_animation != “open”:
    opened = opened
    if !opened:
        animation_player2.play_backwards(“open”)
    if opened:
       animation_player2.play(“open”)
1 Like

extends RayCast3D

@export var animation_player2:AnimationPlayer
@export var sound:AudioStreamPlayer

#Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _physics_process(delta: float) → void:

if is_colliding():
	
	
	#if hit != null:
		
	
			
			
		



	if Input.is_action_just_pressed("Interact"):
		animation_player2.play("open")
		sound.play()
		await animation_player2.animation_finished

	if animation_player2.animation_finished && Input.is_action_just_pressed("Interact"):		
		animation_player2.play_backwards("open")
		sound.play()
		await animation_player2.animation_finished
1 Like

Thanks for your help! I tried your way but it doesn’t look to work as intended. In your approach the door plays backwards only and doesn’t finish? perhaps both try to play, keeping it open always. Kind of like “open” overrides .play_backwards “open.” Maybe am missing something or not setting up something correctly?

Edit: In the the code that you provided if we change the conditions to this:

var opened = false
if Input.is_action_just_pressed(“Interact”):
if animation_player2.current_animation != “open”:
opened = !opened # !when this is changed!
if !opened:
animation_player2.play_backwards(“open”)
if opened:
animation_player2.play(“open”)

changing the line , “ opened = !opened // !when this is changed!” then the door can only be closed/played backwards before either animation is finished. So technically if you are fast enough you can close it before either animation finishes.

1 Like

There was a small issue with the @d.lNh1tpMe proposed code. Here’s a fixed version that should work:

var opened = false
if Input.is_action_just_pressed("Interact"):
	if animation_player2.current_animation != “open”:
        opened = not opened
        if opened:
            animation_player2.play_backwards(“open”)
        else:
            animation_player2.play(“open”)
1 Like

I Appreciate your help! sadly, this adjustment does not change the actual function. This code executes the same behaviors on the animations as @d.lNh1tpMe.

1 Like

Try changing _physics_process() to _process() and see if this’ll help.

2 Likes

Can you share the project files on github if possible?