How to do it...

To use Any State transitions, follow these steps:

  1. Follow the previous recipe to import the characters and place them on the scene.
  2. Create a new Animator Controller for the Spider (you can also edit the existing one).
  3. Create a new bool parameter in the controller and call it Dead.
  4. Create a new Trigger parameter in the controller and call it Hit.
  5. Drag and drop the IdleDeath, and HitReaction animations of the Spider into the controller. The Idle animation should be our default state.
  6. Find the blue Any State node in the controller. See the following screenshot for reference:
Transitions from Any State
  1. Right-click on it and create the following transitions:
    • Any StateHitReaction with one condition: Hit Trigger parameter. Has Exit Time should be set to falseCan Transition To Self set to true, and Transition Duration set to 0.2 seconds.
    • Any StateDeath with one condition: Dead bool parameter set to trueHas Exit Time should be set to falseCan Transition To Self set to false, and Transition Duration set to 0.2 seconds.
    • HitReactionIdle: For this one, you need to right-click on the HitReaction state first. It should have no conditions, Has Exit Time set to true, and Transition Duration set to 0.2 seconds.
  2. Assign the controller to the Spider's Animator component.
  3. Create a new script and call it EnemyWithHits.cs.
  4. Derive this script from the Enemy.cs script instead of MonoBehaviour. You can find the Enemy.cs script in the previous recipe.
  5. To make our hit reactions work, we need to override the Hit() and Die() functions. In the first one, we check if our character's isAlive flag. If our Spider is dead already, we don't attempt to play hit reactions. If it is alive, we play the hit reaction by setting the Hit Trigger parameter in the controller. Next we call the base implementation of the Hit() function:
        public override void Hit(int damage) 

        { 

            if (!isAlive) 
            { 
                return; 
            } 
            anim.SetTrigger("Hit"); 
            base.Hit(damage); 
        } 
  1. In the Die() function, we set the isAlive flag to false and call the base implementation of the Die() function:
        public override void Die() 
        { 
            isAlive = false; 
            base.Die(); 
        } 
  1. Assign the script to the Spider character and play the game 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
3.142.12.207