To track your door states, you can use variables to determine the next animation state the door should be in. Here’s how it could work.
Code in action
How it works
We create the variable called
nextDoorState and set it to “open” at the start of the scene. When the door is clicked, we check the variable’s name (open, close, looping) to check what animation should be played. When the animation is played, we store the next door state the door should be in. For example,
nextDoorState should be “open” when the door was closed.
To make the door loop opening and closing, we use a third state called “looping”. When the state is set (here: by the red cube), we start the “doorLoop” function that will open and close the door as long as
nextDoorState is set to “looping”.
An interesting challenge here is: how to stop the door from opening and closing once the loop started? The
nextDoorState variable needs to be changed from outside, just like it was done by the red cube.
Hope this helps!