Optional: Dead Animation

Optional: hero_dead animation clip (Advanced)

IMPORTANT: Note that this is not required for any projects in this course

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.

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

  • From the Animation panel dropdown, select the hero_dead animation clip

  • Select the timeline section above the final keyframe

  • 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.

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.

///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?
        }

    }

Last updated