# Optional: Dead Animation

## Optional: hero\_dead animation clip (Advanced)

**IMPORTANT:  Note that this is not required for any projects in this course**&#x20;

### Configure loop-time

Since we don't want the hero\_dead animation clip to loop continuously, we need to set that configuration as part of the hero-dead animation clip asset. In the project assets panel, find and select the hero-dead animation clip. Once the animation-clip is selected, in the inspector panel, uncheck the Loop Time checkbox. Loop-time will remain checked for all other animation clips.

**Select hero\_dead Animation Clip in Project Assets** ![](https://1118975008-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-M0-KOTdBSCNJjpmpwrG%2F-M0-KiKQl4YK9uOkMXer%2FScreen%20Shot%202018-11-08%20at%209.23.39%20AM.png?generation=1581627421278182\&alt=media)

**Uncheck Loop Time in Inspector Panel for hero-dead Animation clip** ![](https://1118975008-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-M0-KOTdBSCNJjpmpwrG%2F-M0-KiKSmhPoR4UlPpCg%2FScreen%20Shot%202018-11-08%20at%209.23.55%20AM.png?generation=1581627421164477\&alt=media)

### Add Animation Event to hero\_dead Animation Clip

Finally, you may choose to add an animation-event to the hero\_dead Animation clip. An animation event allows for a function/method to be executed when at a specific animation keyframe is played. In this case, we'd like the hero\_dead animation to play completely before leaving the scene or reloading the scene. So, an animation event allows for some specially defined event to be triggered when a specific keyframe is executed. The animation event can be configured to execute any public method with the following syntax: `public void someMethod( )`, but this method must be defined within a script-component that's on the same gameObject as the animation clip that has the animation event defined.

### Steps to Create An Animation Event

* Select the player in the hierarchy.
* Open the Animation panel&#x20;
* From the Animation panel dropdown, select the hero\_dead animation clip
* Select the timeline section above the final keyframe
* Push the button: with icon: small white vertical rectangle, when you hover over this icon, it will say Animation Event  ![](https://1118975008-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-M0-KOTdBSCNJjpmpwrG%2F-M0-KiKUsX-JOuBHdS0N%2FScreen%20Shot%202018-11-08%20at%2011.32.38%20AM.png?generation=1581627422567078\&alt=media)
* In the inspector, select the Function from the dropdown, to be executed as the Animation Event.(See images below)
* If the ReloadScene( ) method hasn't been added to the player controller yet, see code below, it will need to be customized for your game details.
* You may want to add additonal keyframes, at a later keyframe time slice, if you want the dead animation to be displayed for a longer time before the scene is reloaded.

![](https://1118975008-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-M0-KOTdBSCNJjpmpwrG%2F-M0-KiKWIZU2g1uhW_qF%2FScreen%20Shot%202018-11-08%20at%2011.21.00%20AM.png?generation=1581627421210087\&alt=media)

As seen in the image below, once you've configured an animation-event, then when you hovering over the icon, it will display the message shown below, which is the name of the function / method you've configured to be executed.

![](https://1118975008-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-M0-KOTdBSCNJjpmpwrG%2F-M0-KiKYIUF2tcQH5csV%2FScreen%20Shot%202018-11-08%20at%2011.22.20%20AM.png?generation=1581627422618018\&alt=media)

```java
///Example Method in PlayerController.cs
///This method is executed from within the hero_dead animation, 
//when the keyframe is played with the corresponding animation event.

    public void ReloadScene(){
        if(GameData.instanceRef.Lives <= 0){
            //go to end scene if there are no more Lives left
            SceneManager.LoadScene("Scene5");  //actual scene name
            StateManager.instanceRef.SwitchState(new Scene5State());  //create new state, pass to StateManager

        }
        else //if there are still Lives left, reload the current scene 
        {
            //reload this current scene
            SceneManager.LoadScene("Scene4");  //actual scene name
            StateManager.instanceRef.SwitchState(new Scene4State());  //create new state, pass to StateManager
            //How should we reset score, health variables for the game?
        }

    }
```
