How to do it...

To create lightweight background characters with animation-driven behaviors, follow these steps:

  1. Import your character (a bird with Idle, StartFlying, FlyingInCircles, and Land animations) to Unity.
  2. Create a new Animator Controller.
  3. Drag and drop the IdleStartFlyingFlyingInCircles, and Land animations into the controller.
  4. Create a bool Fly parameter. We will use it later to make the birds start flying.
  5. Create four transitions:
    • IdleStart Flying with the conditions: Fly parameter should be set to true, Has Exit Time set to false, and Transition Duration set to 0.2 seconds.
    • StartFlyingFlyingInCircles with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.2 seconds.
    • FlyingInCirclesLand with one condition: Fly parameter should be set to false, Has Exit Time set to true, and Transition Duration set to 0.2 seconds. We need to set Has Exit Time to true in this case because our Land animation starts from the last frame of the FlyingInCircles animation.
    • LandIdle with no conditions: Has Exit Time set to true and Transition Duration set to 0.2 seconds.
  6. Place our character in the scene and assign the controller to its Animator component.
  7. Add a Bird tag to the character. We will use the tag in the following script.
  8. Create a new script and call it FlockDecoration.cs.
  9. In this script, we first find all the game objects with the Bird tag and store them in the GameObject[] birds array. We do it in the Start() function. In the Update() function, when the player presses the space bar, we set the bool Fly parameter in our controller to its inverted value (if it was true, we set it to false, and vice versa). We do it for every bird stored in the birds array:
        for (int i = 0; i < birds.Length; i++) 

        { 

            Animator anim = birds[i].GetComponent<Animator>(); 
            anim.SetBool("Fly", !anim.GetBool("Fly")); 
        }  
  1. Assign the script to an empty game object in the scene. Play the game and press the space bar to see the effect. You can also duplicate the Bird game object several times.
..................Content has been hidden....................

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