Search Unity

How can i switch and play all animations from Animator ?

Discussion in 'Scripting' started by Chocolade, Aug 28, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SwitchAnimations : MonoBehaviour
    7. {
    8.     private Animator animator;
    9.     private int index = 0;
    10.     public string[] animations = new string[] {"Grounded", "Aiming",
    11.      "Roll", "Use", "PickupObject", "Any State", "Death_A"};
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         animator = GetComponent<Animator>();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         if (Input.GetKeyDown(KeyCode.A))
    23.         {
    24.             if (++index == animations.Length)
    25.                 index = 0;
    26.             animator.Play(animations[index]);
    27.         }
    28.     }
    29. }
    30.  
    But i'm getting index -1 exception after pressing some times on A: Animator.GotoState: State could not be found and Invalid Layer Index '-1'

    And also two errors that i'm missing a receiver not sure how to fix it too.

    'Space_Soldier_A_LOD1' AnimationEvent 'CantRotate' has no receiver! Are you missing a component?

    So i wonder where is the problem and i found more animations inside a blend tree. This is the RifleActions layer:



    But then if i click for example double click on Grounded i'm getting into this area:



    What i want is to switch between all animations from the Animator by pressing on A.
     
  2. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    The solution is to add this exceptions to the script as methods. Even empty methods but they should be in the script since this animations having this events so there must be methods with this events names.

    Instead messing up with the animations and events the easiest way is to add empty methods. And later if you need you can use this methods for example in RollSound you can play audio clip. But the main idea to solve it is to add this methods. Here is the complete code:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEditor.Animations;
    6. using UnityEngine;
    7.  
    8. public class SwitchAnimations : MonoBehaviour
    9. {
    10.     private Animator animator;
    11.     private static UnityEditor.Animations.AnimatorController controller;
    12.     private AnimatorState[] states;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         animator = GetComponent<Animator>();
    18.         states = GetStateNames(animator);
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.A))
    25.         {
    26.             StartCoroutine(QueueAnim(states));
    27.         }
    28.     }
    29.  
    30.     private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
    31.     {
    32.         controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
    33.         return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
    34.     }
    35.  
    36.     IEnumerator QueueAnim(params AnimatorState[] anim)
    37.     {
    38.         int index = 0;
    39.  
    40.         while (index < anim.Length)
    41.         {
    42.             if (index == anim.Length)
    43.                 index = 0;
    44.  
    45.             animator.Play(anim[index].name);
    46.            
    47.             AnimatorStateInfo si = animator.GetCurrentAnimatorStateInfo(index);
    48.             yield return new WaitForSeconds(5);
    49.             index++;
    50.         }
    51.     }
    52.  
    53.     private void RollSound()
    54.     {
    55.  
    56.     }
    57.  
    58.     private void CantRotate()
    59.     {
    60.  
    61.     }
    62.  
    63.     private void EndRoll()
    64.     {
    65.  
    66.     }
    67.  
    68.     private void EndPickup()
    69.     {
    70.  
    71.     }
    72. }
    73.