How to do it...

To use randomized animations, follow these steps:

  1. Import your character with the animations to pick from. In our case, they are Hurray, Wave, and PickUp. We also use Idle and Walk animations (the Walk animation is used before the randomized action and the Idle animation is used after).
  2. Create a new Animator Controller (or edit an existing one).
  3. Create a new float parameter in the controller and call it Random.
  4. Create a new Blend Tree in the controller and name it RandomAction.
  5. In the Blend Tree settings, add Motion Fields for your randomized animations. In our example, we have three Motion Fields (for HurrayWave, and PickUp).
  6. Assign your animations to the Motion Fields.
  7. Choose the Random parameter as the Parameter of the Blend Tree.
  8. Uncheck the Automate Thresholds option.
  9. Set the Thresholds to integer numbers, starting from 0. In our example, they are 0, 1, and 2.
  10. Exit Blend Tree settings.
  11. Create transitions to and from the Blend Tree, if needed. In our example, we have following transitions:
    • WalkRandomAction with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.2 seconds. Our scene starts with the characters playing one cycle of the Walk animation and then transitioning to the RandomAction Blend Tree.
    • RandomActionIdle with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.2 seconds. Our scene ends with the characters transitioning from RandomAction to Idle animation.
  12. Place the character in the scene.
  13. Assign the controller to the character's Animator component.
  14. Create a new script and call it RandomAction.cs.
  15. In this script's Start() function, we create an integer random value and set it as the value of our float Random parameter in the controller:
        Animator anim = GetComponent<Animator>(); 
        int randomValue = Random.Range(0, numActions); 
        anim.SetFloat("Random", (float)randomValue); 
  1. In the preceding script, numActions is a public int variable that stores the total number of randomized actions in our Blend Tree (3 in our example).
  2. Assign the script to the character and play the game to see the effect. You may need to start the game multiple times or copy the character 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.191.181.144