Making a platform start falling once stepped-on using a Trigger to move animation from one state to another

In many cases we don't wish an animation to begin until some condition has been met, or some event occurred. In these cases a good way to organize the Animator Controller is to have two animation states (clips) and a Trigger on the Transition between the clips. We use code to detect when we wish the animation to start playing, and at that time we send the Trigger message to the Animation Controller, causing the transition to start.

In this recipe we'll create a water platform block in our 2D platform game; such blocks will begin to slowly fall down the screen as soon as they have been stepped on, and so the player must keep on moving otherwise they'll fall down the screen with the blocks too! It looks as shown in the following screenshot:

Making a platform start falling once stepped-on using a Trigger to move animation from one state to another

Getting ready

This recipe builds on the previous one, so make a copy of that project, and work on the copy for this recipe.

How to do it...

To construct an animation that only plays once a Trigger has been received, follow these steps:

  1. In the Hierarchy create an Empty GameObject named water-block-container, positioned at (2.5, -4, 0). This empty GameObject will allow us to make duplicates of animated Water Blocks that will animate relative to their parent GameObject position.
  2. Drag an instance of the sprite Water Block from the Project | Sprites folder into the scene and child it to GameObject water-block-container. Ensure the position of your new child GameObject Water Block is (0, 0, 0), so that it appears neatly to right of the wall blocks platform, as shown in the following screenshot:
    How to do it...
  3. Add a Box Collider 2D component to child GameObject Water Block, and set the layer of this GameObject to Ground, so that the player's character can stand and jump on this water block platform.
  4. Ensuring child GameObject Water Block is selected in the Hierarchy, open an Animation panel, then create a new clip named platform-water-up. saving it in your Animations folder.
  5. Click button Add Curve, and chose Transform and Position.
  6. Delete the second keyframe at time 1:00. You have now completed the creation of the water block up animation clip.
  7. Create a second Animation clip, named platform-water-down. Again, click button Add Curve, and chose Transform and Position, and delete the second keyframe at time 1:00.
  8. With the first keyframe at time 0:00 selected, set the Y-value of the GameObjects Transform Position to -5. You have now completed the creation of the water block down animation clip, so you can click the red record button to stop recording.
  9. You may have noticed that as well as the up/down Animation Clips that you created, another file was created in your Animations folder, an Animator Controller named Water Block. Select this file and open the Animator panel, to see and edit the State Machine diagram:
    How to do it...
  10. Currently, although we created 2 animation clips (states), only the Up state is ever active. This is because when the scene begins (Entry) the object will immediately go in state platform-water-up, but since there are no transition arrows from this state to platform-water-down, then at present the Water Block GameObject will always be in its Up state.
  11. Ensure state platform-water-up is selected (it will have a blue border around it), and create a Transition (arrow) to state platform-water-down, by choosing Make Transition from the mouse-right-click menu.
  12. If you run the scene now, the default Transition settings are that after 0.9 seconds the Water Blocks will transition into their Down state. We don't want this – we only want them to animate downwards after the player has walked onto them. So create a Trigger named Fall, by choosing the Parameters tab in the Animator panel, clicking the plus '+' button and selecting Trigger, and then selecting Fall.
  13. Do the following to create our Trigger:
    • In the Animator panel select the Transition
    • In the Inspector panel uncheck the Has Exit Time option
    • In the Inspector panel drag the Transition end time to 2:00 seconds (so the Water Block slowly Transitions to its Down state over a period of 2 seconds)
    • In the Inspector panel click the plus '+' button to add a Condition, which should automatically suggest the only possible condition parameter, which is our Trigger Fall.
    How to do it...
  14. We now need to add a collider trigger just above the Water Block, and add C# script behavior to send the Animator Controller Trigger when the player enters the collider. Ensuring child GameObject Water Block is selected, add a (second) 2D Box Collider, with a Y-Offset of 1, and tick its Is Trigger checkbox:
    How to do it...
  15. Add an instance of C# script class WaterBlock as a component to your Water Block child GameObject:
    using UnityEngine;
    using System.Collections;
    
    public class WaterBlock : MonoBehaviour {
      private Animator animatorController;
    
      void Start(){
        animatorController = GetComponent<Animator>();
      }
    
      void OnTriggerEnter2D(Collider2D hit){
        if(hit.CompareTag("Player")){
          animatorController.SetTrigger("Fall");
        }
      }
    }
  16. Make 6 more copies of GameObject water-block-container, with X positions increasing by 1 each time, that is, 3.5, 4.5, 5.5, and so on.
  17. Run the scene, and as the player's character runs across each water block they will start falling down, so he had better keep running!

How it works...

You created a two-state Animator Controller state machine. Each state was an Animation Clip. You created a Transition from the Water Block Up state to its Down state that will take place when the Animator Controller received a Fall Trigger message. You created a Box Collider 2D with a Trigger, so that scripted component WaterBlock could be detected when the player (tagged Player) enters its collider, and at that point send the Fall Trigger message to make the Water Block GameObject start gently Transitioning into its Down state further down the screen.

Learn more about the Animator Controllers on the Unity Manual web pages at http://docs.unity3d.com/Manual/class-AnimatorController.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.63