How to do it...

To make the two objects play synchronized animations, follow these steps:

  1. Import the Character and Wheel objects into Unity.
  2. Set the Loop Time option in the WheelLoop animations for both the Wheel and the Character.
  3. Place the Character and the Wheel in the scene and match their position according to what was set in the 3D package.
  4. Create an Animator Controller for the Character.
  5. Insert the WheelStart and WheelLoop animations into the controller. Make sure the WheelStart animation is the default state.
  6. Create a PlayAnim bool parameter in the controller.
  7. Create two transitions:
    • WheelStartWheelLoop with one condition: PlayAnim parameter set to true parameter. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • WheelLoopWheelStart with one conditions: PlayAnim parameter set to false. Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
  8. Assign the controller to the Character game object's Animator component.
  9. Create an Animator Override Controller. Derive it from the Character's controller. Assign it to the Wheel game object's Animator component.
  10. Change the animations in the override controller to the Wheel's animations.
  11. Write a new script and call it PlayAndStop.cs. In that script's Update() function, we check if the player pressed the space bar. If so, we set the PlayAnim bool parameter to its inverted value in both the Character's and Wheel's animator controllers:
        if (Input.GetKeyDown(KeyCode.Space)) 
        { 
            play = !play; 
            characterAnimator.SetBool("PlayAnim", play); 
            objectAnimator.SetBool("PlayAnim", play); 
        } 
  1. In the preceding script, play is a helper member variable, characterAnimator is a reference to the Character game object's Animator component, and objectAnimator is the reference to the Wheel game object's Animator component.
  2. Assign the script to the Character game object.
  3. Drag and drop the Character game object to the Character Animator field in the script and the Wheel game object to the Object Animator field.
  4. Play the game and press the space bar to see the effect.
..................Content has been hidden....................

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