Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Additive Animation (Override?)

Discussion in 'Scripting' started by Littlenorwegian, Mar 30, 2016.

  1. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Right, so, I am using the ThirdPersonController.js script on my character. Which works fine.

    I have, however, another script that does an animation called 'Bark'.
    Now, the problem, it appears, is that the ThirdPersonController.js script sort of has a stranglehold on the animations the character is playing. It only plays the idleanimation.

    What I want to do is have the Bark animation (Which only affects the head) be additive.
    The script functions fine if ThirdPersonController.js is deactivated, but that's no good.
    Because that disables the character's movements.


    So, is there a way to force an animation to play (I haven't found it) or something?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    I'd suggest using additive animations!

    You'll have to set up an avatar mask and an additional layer in the animator controller. Start reading here, it's pretty straightforward. Feel free to ask questions if something's not working.
     
  3. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    I'm a little overwhelmed.
    A problem here is two scripts interacting, but for now, how do I in C# code make it change from Base Layer to this New Layer?

    (Love Teslagrad, btw)
     
    Last edited: Mar 30, 2016
  4. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    And it seems I can't select the animations I have attached to the character under 'Motion' in the Animator tab

    My model's rig is stored as 'Legacy'

    I think, if I can figure that out, I can make this work. Maybe.
     
  5. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Sorry for tripple-posting, but I am closer.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BarkLickSniff : MonoBehaviour {
    5.  
    6.     public bool Busy = false;
    7.     public GameObject Player;
    8.  
    9.     void Start() {
    10.         anim = Player.GetComponent<Animation>();
    11.     }
    12.  
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (Input.GetButtonDown("Fire1") && Busy == false )
    17.         {
    18.             StartCoroutine(Bark());
    19.    }
    20. }
    21.  
    22.     IEnumerator Bark() {
    23.         Busy = true;
    24.         Player.GetComponent<Animation>()["Scraps_Bark"].layer = 1;
    25.         Player.GetComponent<Animation>().Blend("Scraps_Bark", 2.5f );
    26.         yield return new WaitForSeconds(Player.GetComponent<Animation>()["Scraps_Bark"].length);
    27.         Busy = false;
    28.         print("Barking now. Arf!");
    29. }
    30.     }
    Which does blend the animation, but it's jerky. Like it's 50-50% between the two. Meaning the character stutters if he runs and barks at the same time. And the barking is not at full force.

    While what I'd like is for it to just play the bark animation of the head at full force.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    If you look on the left side of the animator window you have there, you've got a little tab with all of your triggers and booleans and whatnot. On the same tab, you can switch to "layers", which gives you a set of layers to work with.

    Each of the layers run at the same time, meaning that any animation in any layer is played.

    Each layer that's not the base layer (the default one) can either be set to "override" or "additive". In override mode, that layer's animation is shown instead of the base layer's animation, based on the weight-sider; a layer with a weight of 1 will override all animation from the base layer. A layer with a weight of .5 will override half the animation of the base layer - so it will be a 50/50 blend. A layer with a weight of 0 won't do anything. You can set the weight from code.

    In additive mode, the animation in the layer is instead added on top of the animation from the lower layer. So if you have a base layer with a character walking, and an additive layer on top of that with the character clapping, the character will walk and clap.

    An important point about additive mode is that it uses the first frame of the default animation in the layer (I believe, I might be wrong) as the reference pose for all of the other animations. So if a bone is rotated to 70 degrees around the x-axis in that pose, and you play an animation where it's rotated to 120 degrees, that will add 50 degrees of rotation to the animation in the base layer.

    In addition to this, each layer can have an avatar mask. This is simply a list of bones that will be animated by that layer. You can look up how to create them, it's pretty straight forward.
     
  7. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    I think I get your logic, and I appreciate it, but I have a problem with the whole animator system.
    As I said, I cannot pick the motion that comes with the imported character. And I don't know why :(

     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    That should work. I usually drag-and-drop the animation clip from the hierarchy into the Motion field - they should be available as a fold-out under your model.

    What import settings do you have on the model? It's under Rig on the import settings, and it should be Generic as long as you're not working with a humanoid model.
     
  9. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    I had 'Legacy'.
    I changed it to Generic now and got a different problem.

    Even though, it's still there.

    The character is now just 'frozen' in an animation state.

    (Thanks for the super fast reply)
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    How are you trying to play the animation?
     
  11. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    I'm using a modified version of the thirdpersoncontroller
    https://dl.dropboxusercontent.com/u/11238180/ThirdPersonController.cs
    Which seems incompatible with Generic, but not with Legacy? I am super confused at this point now. Who knew making the head bark would be this hard/confusing.

    And if I make the character rig Generic, then I can click the animation in the Animator Panel, but as I mentioned, now this script is broken. :(


    EDIT: https://dl.dropboxusercontent.com/u/11238180/Show.mp4 (This is using legacy) just to perfectly illustrate why I need additive animation

    And just what happens when I try to add one of my animations in the Animator panel.
    https://dl.dropboxusercontent.com/u/11238180/Showing.mp4
     
    Last edited: Apr 4, 2016
  12. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Get 2 errors
     
  13. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    It seems like your thirdpersoncontroller is trying to use to old Animation system rather than the Animator.

    Are you sure you've grabbed the latest version of the ThirdPersonController? I'd be surprised if Unity's still using the Animation internally. It might be that they've just not updated it yet.
     
  14. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Well, I just had myself a little baptism of fire, heh.
    So, I had to first try and understand the framework of the ThirdPersonController.
    Rip out most of the existing animation guts and references.
    Replace them and learn the system, while using some of the values for animation purposes (like, speed values)

    But finally, I got it working.
    https://dl.dropboxusercontent.com/u/11238180/HotDiggity.mp4
    And can refine it further now. And you can see the barking is additive and I got just what I wanted.

    BIG thanks, @Baste. Really, this was a convoluted scenario but I got there in the end. :)