Search Unity

Triggering animation when previous animation ends

Discussion in 'Animation' started by karina_mady, Apr 2, 2014.

  1. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
    I've been trying to trigger the animations in my blend tree automatically.
    So , in other words, when one animation ends, go to the next one.
    Now , I've tried to use this :

    private Animation animation;
    ...
    yield WaitForSeconds(animation["warmup2"].length);
    anim.SetBool ("NextWarmup", true);

    but it looks like it doesn't like the animation["warmup2"].length bit.
    Any idea why?

    I am using this several times with different animations.

    Thanks!
     
  2. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    It looks like you're using the Legacy animation system (Animation versus Animator). In Legacy, MonoBehaviours already have a property named animation, so don't redefine it in your script. That is, get rid of:
    Code (csharp):
    1. private Animation animation;
    2.  
    In Legacy, you can use Animation.PlayQueued() so you don't need to access the animation state's length property:
    Code (csharp):
    1. animation.Play("warmup2");
    2. animation.PlayQueued("NextWarmup");
    3.  
    Just make sure that the clips warmup2 and NextWarmup are on your Animation component.

    Now if you're using Mecanim instead of Legacy (as suggested by anim.SetBool()), then you need to get a reference to the Animator component:
    Code (csharp):
    1. private Animator animator;
    2.  
    3. void Awake() {
    4.     animator = GetComponent<Animator>();
    5. }
    6.  
    And just add a transition from the first animation to the second animation based on End Time, which is the default for transitions.

    Now if you're using Mecanim and you really have a blend tree, you can't blend on bools. You can blend on a float. In this case, you can change the value of the float at the end of each loop to change what clips the tree is blending on. Use Animator.GetCurrentAnimatorStateInfo() to get the length.
     
  4. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
    I have tried implementing the code that you mentioned, but it doesn't seem to work.
    What I am trying to do is have 2 paths to playing the animations:
    1. Play them automatically one after another, triggered just at the beginning with a key press.
    2. Play them only based on key press.

    Here is the blend tree:
    $Blend_tree.png

    and some key points about the transitions depending on whether they will play automatically or not:
    $Transitions.png

    Here is some of the script :
    $script.png
    $script2.png


    I have also attached the project to the link below, may be you can have a look around and make some suggestions.

    https://www.dropbox.com/s/dqvh6u066n8k1ve/Robot.rar

    Pleaaaase help me with this.

    Thanks,
    Karina
     
  5. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Hi,

    I'll take a look at your project today and let you know if I have any suggestions. Sorry for the delay. I was been busy with a big push to get version 1.2.1 of the Dialogue System out the door.
     
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Try these steps:

    1. Remove the line below. You don't need it.
    Code (csharp):
    1. private Animation animation;
    2. You're using anim.SetInteger("PathNext", #). The PathNext parameter should be an integer. Remove the current PathNext parameter, which is a Float. Add a new Int parameter named PathNext.

    3. Reassign all of your transitions. For example, the transition from Idle to Idle_jump should be "PathNext Equals 1."

    4. The automated transitions currently have the condition "Exit Time ###." On each of these transitions, click the "+" below Exit Time and add a second condition "Automate True". This means it will only use this transition when the Automate parameter is true.

    Good luck!
     
  8. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
    Hi,

    I have made all the changes you suggested, it was dumb for me to use a float when I was assigning an int, dunno what got into my head , really.
    Still, I think there is another problem with the project because once I play the scene, the character starts the idle animation, but plays it indefinitely, and doesn't transition no matter what I press.The transition arrow that is supposed to go Blue when you trigger an animation doesn't do anything, so maybe the code behind key presses is not working properly?

    I attached the project again with the changes that I made in the link below.
    https://www.dropbox.com/s/l839c7hjufxlcr6/Robot-updated.rar


    The BotControlScript has some lines commented out, because I assumed that if it works with RunOnKeyPress then it will run automatically as well. But feel free to make any changes, etc.

    This was the same problem as before, when I had the float variable instead of the int, so I am guessing that what's causing it is not related to the Animator, or the script, but the animation. Can you please check this? This is my final year project, deadline in 3 weeks, so I am pretty much desperate at this point :(

    Questions:
    1. I have another issue raised about the menu not working, if you can have a look at that as well would be great.
    2. I see that I cannot preview the animations anymore, for some reason, do you know why? This happened around the time when I created my own avatar definitions, but now I can't preview any of them, even the Mecanim Idle one.


    Thanks so much, I really appreciate your help and effort.
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Sorry, I've been occupied getting a deliverable ready at work. When I get a chance to take a look at this, I'll let you know if I see anything.
     
  10. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
  11. karina_mady

    karina_mady

    Joined:
    Mar 16, 2014
    Posts:
    18
    Hey TonyLi,

    Did you get the chance to take a look at the project?
    I need to fix this before my dissertation presentation next Tuesday, so please , if you have 10 spare minutes, take a look at it.
    This is the last stage of the project, so once this works, it's done!

    Thanks again,
    Karina

    Bump
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Hi Karina,

    Sorry, I've been busy with a big upcoming release. I'll make some time to take a look at your project today and let you know if I see anything.
     
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Hi Karina,

    To restore your ability to preview, drag the Preview bar up:
    $karina_preview.jpg

    You can view the state of the animator controller at runtime. To do this, make sure the Animator view is on a separate area from the Game view. When the scene is playing, click on Robot in the Hierarchy. You'll see a blue progress bar on the current state.

    To get the automatic mode to work, add this line to the bottom of the Start() method in BotControlScript.cs:
    Code (csharp):
    1.  
    2. anim.SetBool("Automate", true);
    3.  
    I don't see any problem with the transitions themselves. You can watch the parameters and states change at runtime to confirm this.

    To get the menu working using OnGUI, use:
    Code (csharp):
    1.  
    2. public class MenuScript : MonoBehaviour
    3. {
    4.  
    5.     public static bool automate;
    6.  
    7.     public GUISkin guiSkin;
    8.  
    9.     void OnGUI() {
    10.         if (guiSkin != null)
    11.         {
    12.             GUI.skin = guiSkin;
    13.         }
    14.         float midY = 0.5f * Screen.height;
    15.         GUI.Label(new Rect(0, midY - 40, Screen.width, 40), "Run Animation");
    16.         if (GUI.Button(new Rect(0.25f * Screen.width, midY, 0.5f * Screen.width, 40), "Automatically"))
    17.         {
    18.             automate = true;
    19.             Application.LoadLevel("Robot");
    20.         }
    21.         if (GUI.Button(new Rect(0.25f * Screen.width, midY + 40, 0.5f * Screen.width, 40), "On Key Press"))
    22.         {
    23.             automate = false;
    24.             Application.LoadLevel("Robot");
    25.         }
    26.     }
    27. }
    28.  
    To pretty it up, you'll want to create your own custom GUI skin. In the Project view, create a GUI skin and then assign it to the MenuScript's Gui Skin property. If nothing else, you'll want the label to be centered. So edit the GUI skin, expand the Label foldout, and set Alignment to Middle Center.