unity-game-engineanimationanimator

Unity - Animator Controller - Store previous state


I have a locker with 4 drawers. Every drawer is an individual object. Every drawer has its own animation for opening it. I have a problem with transition. If I open the first drawer and then open the second (or third), the first drawer will automatically close.

The next picture shows the object after the first animation (ThirdDrawer_Open)

enter image description here

The next picture shows the object after the second animation (SecondDrawer_Open)

enter image description here

I want to store the previous state of animation so when I open the second drawer, the previous one doesn't close/end.


Solution

  • From the Animation States docs, there are various exposed properties on states that you can edit. One property is called Write Defaults. From the docs, it is described as

    Whether or not the AnimatorStates writes back the default values for properties that are not animated by its Motion.

    In your case, if this is enabled whenever you change your states for drawers, it will write the defaults back to the other drawers causing them to close. A solution would be to un-toggle this property so defaults are not written, or you can forcibly overwrite these values at runtime using WriteDefaultValues. I generally prefer the prior solution as WriteDefaults can be a headache to figure out and work with at times.