Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calling an animation from Mecanim controller

Discussion in 'Animation' started by Menatombo, Aug 5, 2015.

  1. Menatombo

    Menatombo

    Joined:
    Feb 6, 2014
    Posts:
    26
    Maybe I'm going about it wrong, but what I want to do is when my player model goes to a trigger to have them begin an animation.

    I have an animation and put it in to the Mecanim controller. I just have no clue how to call it from an object trigger. I tried the old legacy way, but it tells me there is no animation even though I have put one in the animations list and controller.

    Any help or insight would be appreciated.
     
  2. Menatombo

    Menatombo

    Joined:
    Feb 6, 2014
    Posts:
    26
    No one? Can this not be done in the mecanim system and only can be done in the legacy system? Mecanim seems great, but overly complicated. :( I like the idea of using animations for all of my characters, but I don't like the fact that I can't trigger an animation if say the character runs up to another character. There seems to be no way to script in that the character NPC is grabbed and thrown.
     
  3. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    698
    on the animation setup a event then in the "Edit Animation Event" popup box in the function box put in the function you want to call, then in your script write the function, something like callAnimation()

    Code (JavaScript):
    1. private var _animator : Animator;
    2.  
    3. function Start ()
    4. {
    5.     _animator = GetComponent.<Animator>();
    6. }
    7.  
    8. // called from animation event
    9. function callAnimation ()
    10. {
    11.     _animator.SetBool("runAnimation", true);
    12. }
    Screen Shot 2015-08-08 at 09.05.11.png
     
  4. Menatombo

    Menatombo

    Joined:
    Feb 6, 2014
    Posts:
    26
    Found it, but it has no function. I don't have anything on the character for the collision. I was trying to make it so if they hit the attack dummy. (Like OnCollision or OnTriggerEnter that they would start an attack animation.) I remember being able to call this function back with legacy, but when it comes to Mecanim I'm a little more clueless. Went through tons of tutorials and have a character controller and functions set up for the keyboard, but they didn't cover anything like this.

    A question, though. Can I put that script on the attack dummy or does it have to be on the player character? Just trying to figure out the Mecanim system. It's great but it doesn't seem like I can push much to it in the way of scripts that aren't on the Player character.
     
  5. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    698
    You can put the script where you like .. On the dummy you would do something like this ..

    Code (JavaScript):
    1. function OnTriggerEnter (other : Collider)
    2. {
    3.     if(other.gameObject.name == "Player")
    4.     {
    5.         other.gameObject.GetComponent.<Animator>().SetBool("runAnimation", true);
    6.     }
    7. }
     
  6. Menatombo

    Menatombo

    Joined:
    Feb 6, 2014
    Posts:
    26
    I put the script on. Added a bool and also added my animation to the mecanim by dropping it on to the Animator. I added in a debug.log to let me know if the player was entering the trigger area. It is, but the animation still isn't firing off. Do I need to add a transition to it? Or something else special? I love Mecanim and really want to use it, but it seems like it was easier to do with legacy. It'll probably seem easier when I understand how it talks to the components, but right now I'm not understanding why the animation isn't animating.

    Thanks in advance for all the help. This is a great adventure for me. :)
     
  7. Menatombo

    Menatombo

    Joined:
    Feb 6, 2014
    Posts:
    26
    Okay. I got it and it's working, but now once it triggers it won't stop. I tried to change direction and speed in the new transition. It still won't set it back to false, though. Good thing is that now it is working and I understand a bit more on how to make it work. (Which is really awesome!) But now I have to figure out how to make it stop.

    Thanks so much for your help, Griffo!!
     
  8. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    make sure that run animation isn't looped :)
     
  9. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    698
    If you set "runAnimation",true and don't set it false again it will continue to call that animation another way is to use a trigger instead, the trigger will call as long as the animation is not running, so try setting a trigger in the animation controller not a boolean ..

    Code (JavaScript):
    1. function OnTriggerEnter (other : Collider)
    2. {
    3.     if(other.gameObject.name == "Player")
    4.     {
    5.         other.gameObject.GetComponent.<Animator>().SetTrigger("runAnimation");
    6.     }
    7. }
    Ethos says, make sure that run animation isn't looped, sorry I'd have to disagree there because the way I use Walk, Run animations mine are looped so they keep walking, running once the boolean or trigger fires, then when I release the key, joystick I then set another trigger to stop the animation that would link to an idle animation or whatever you like ..

    Code (JavaScript):
    1. function OnTriggerEnter (other : Collider)
    2. {
    3.     if(other.gameObject.name == "Player")
    4.     {
    5.         other.gameObject.GetComponent.<Animator>().SetTrigger("runStop");
    6.     }
    7. }
    I use blend trees, much more flexible and powerful ..
     

    Attached Files:

    theANMATOR2b likes this.
  10. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    698
    So I really don't use booleans, triggers sometimes to call the death animation that I put on a higher level, I use floats to control the blend trees and I use increments of 1 and lerp from one value to another ...

    Code (JavaScript):
    1. function rndWalkPatern ()
    2. {
    3. // choose a random walk patern  
    4.     rndWalkChoice = Random.Range(0,5);
    5.  
    6.     lerpTime = 0.5;
    7.     incomingBlendTreesValue = 1;
    8.     incomingBlendIdleValue = 0;
    9.     incomingBlendLocomotionValue = 0;
    10.     incomingBlendWalkValue = rndWalkChoice;
    11.     incomingBlendRunValue = 0;
    12.     incomingBlendAttackValue = 0;
    13.     incomingBlendDeath = 0;
    14.     lerpBlendValue();
    15. }
    Code (JavaScript):
    1. function lerpBlendValue ()
    2. {
    3.     v = 0;
    4.  
    5.     blendTreesValue =_animator.GetFloat("BlendTree");
    6.     blendIdleValue =_animator.GetFloat("BlendIdle");
    7.     blendLocomotionValue =_animator.GetFloat("BlendLocomotion");
    8.     blendWalkValue =_animator.GetFloat("BlendWalk");
    9.     blendRunValue =_animator.GetFloat("BlendRun");
    10.     blendAttackValue =_animator.GetFloat("BlendAttack");
    11.     blendDeathValue =_animator.GetFloat("BlendDeath");
    12.  
    13.     while(v < 1)
    14.     {
    15.         // v goes towards 1
    16.         v += Time.deltaTime/lerpTime;
    17.         // keep v between 0 and 1
    18.         v = Mathf.Clamp(v, 0, 1);
    19.             _animator.SetFloat("BlendTree", Mathf.Lerp(blendTreesValue, incomingBlendTreesValue, v));
    20.             _animator.SetFloat("BlendIdle", Mathf.Lerp(blendIdleValue, incomingBlendIdleValue, v));
    21.             _animator.SetFloat("BlendLocomotion", Mathf.Lerp(blendLocomotionValue, incomingBlendLocomotionValue, v));
    22.             _animator.SetFloat("BlendWalk", Mathf.Lerp(blendWalkValue, incomingBlendWalkValue, v));
    23.             _animator.SetFloat("BlendRun", Mathf.Lerp(blendRunValue, incomingBlendRunValue, v));
    24.             _animator.SetFloat("BlendAttack", Mathf.Lerp(blendAttackValue, incomingBlendAttackValue, v));
    25.             _animator.SetFloat("BlendDeath", Mathf.Lerp(blendDeathValue, incomingBlendDeath, v));
    26.         yield;
    27.     }
    28. }
     
    Last edited: Aug 10, 2015
  11. Menatombo

    Menatombo

    Joined:
    Feb 6, 2014
    Posts:
    26
    I love that way. I'm gong to redo some of my code to incorporate blend trees. It would prove to be a much smoother animation that way. I didn't think that I could, but I'm still learning a lot about Mecanim, but so far I'm loving it. Although, my controller needs cleaned up a lot more. I should probably put the animations that are triggered in to a sub state to keep them out of the way.
     
    Griffo likes this.