I am working with my students to create a number of animations with their objects. Many want a door or an object to start and stop a looped animation depending on its current animation. Meaning:
- On activate
- If door is closed set looped animation to open
- If door is open set looped animation to closed
- Set to loop to happen continiously
We just were not sure how to do a check for its current animation loop to code the if/else statement.
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!