Nicole Erasga
Nicole Erasga

Reputation: 31

Reset back to default state after animation

So here is what I have done after the animation it should be automatically back to the default state :)) sorry newbie in animation hehe here is my code

public GameObject bookLayout;
public GameObject panel;

public void openBook()
{
    if (bookLayout != null)
    {
        Animator animator = bookLayout.GetComponent<Animator>();
        if (animator != null)
        {
            bool isOpen = animator.GetBool("open");
            animator.SetBool("open", !isOpen);
        }
    }
}

problem is it only goes back to the default state after I click it ^_^

Upvotes: 0

Views: 1308

Answers (1)

Sean Carey
Sean Carey

Reputation: 807

What you're describing is called an 'Animation Event', and it's actually not that hard to implement.

First, if there isn't already a MonoBehavior script attached to your bookLayout object, add one to it. Then add a public function inside of that script whose sole responsibility is to set the state back to default. Let's call this function 'SetStateDefault'. Then make sure bookLayout is selected in the hierarchy and go to Windows -> Animation -> and open up the Animation tab.

Uncollapse the sprites that make up your animation so that you can see them in the timeline. enter image description here

Click on the button that will take you to the end of the animation enter image description here

In this example, the end of the animation is at frame 4. Now click on the 'Add Event' button

enter image description here

Then in the Inspector, go to the 'Function' dropdown menu and select that public function that we created earlier, the one called 'SetStateDefault'

enter image description here

Assuming that your 'SetStateDefault' function does what it's supposed to, that should be it. Please let me know if there's anything you weren't clear on.

enter image description here

Upvotes: 1

Related Questions