Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

reusing animation clips?

Discussion in 'Animation' started by DarkMarmot, Nov 27, 2013.

  1. DarkMarmot

    DarkMarmot

    Joined:
    Nov 13, 2013
    Posts:
    22
    Is there any way to reuse animation clips with multiple game objects?
    For instance, I'd like a 'fadeOut' animation that I could use with multiple prefabs.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,734
    If you're using legacy animation, the properties animated by the clip's curves just need to be present in those game objects, and they need to be named exactly as defined in the clip, including the full path from the root of the object (e.g., "Hips/Spine/Neck/Head"). If they meet those requirements, then you can add the clip to the objects' Animation component and use them.

    If you're talking humanoid animation in Mecanim, they automatically retarget to any valid rig.
     
    ArachnidAnimal likes this.
  3. DarkMarmot

    DarkMarmot

    Joined:
    Nov 13, 2013
    Posts:
    22
    I'm sorry, I talking about the new 2d sprite animation -- I should've been more specific :(
     
  4. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    I want to know this too -- say with mecanim, for example, if I want to move a generic ugui panel to a new position, can I somehow reuse that movement clip to move other panels to a new position too?

    What if I want the new one to fade and rotate using another clip also? Can I somehow attach these 2 animation clips to the other gameobject?
     
    Last edited: Feb 8, 2015
  5. funshark

    funshark

    Joined:
    Mar 24, 2009
    Posts:
    225
    Yep you can, assuming your object has the same properties/elements.

    You just need to create a new animator for each object and drag the animation inside.
     
  6. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    What if I want the other gameobject to also fade and rotate using two other clips also (in addition to moving to a new position)? Can I somehow attach these 2 animation clips to the other gameobject (perhaps through code or something?)

    I tried adding 2 Animators to the same gameobject, but it gives me an error -- it doesn't make sense; if I want 2 animators + controllers on a mecanim object, I should be able to use them, right??

    Is there no way at all to modularize my animation assets so that I can combine them (in any way I want to) at will?
     
  7. funshark

    funshark

    Joined:
    Mar 24, 2009
    Posts:
    225
    You can't attach two animators.
    If you want to fade and rotate simultaneously ( assuming these are 2 animation files ), you may use the layers.
    Each layer can Add its animations to the base layer ( and you can decide how much of these animations you want to add, from 0 to 100% )
     
  8. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Nice. Thank you. I had completely overlooked the Animation Layers feature because, due to the manual's description of the feature, I thought this was an avatar-only setup.

    With that said, I'm having trouble figuring out the settings required to sync the state machine to the other layer's so that the new layer's animation will blend with the old animation. What I have is this:

    Base Layer + Blend Layer

    Blend Layer:

    weight = 1.0
    mask = None
    blending = Additive
    sync = true >> to >> Base Layer

    Nothing seems to work to get both animations to play simultaneously for the different properties. I've tried Override for Blending as well, but no dice. Can you confirm you can do this successfully? I'm trying to animate a uGUI panel with this method (i.e. position on the Base Layer and rotation on the Blend Layer for example), but it's not working at all -- only the Base Layer animation is playing (I'm using Unity 4.6.2.p1, if it helps.)
     
  9. funshark

    funshark

    Joined:
    Mar 24, 2009
    Posts:
    225
    Yup, just tested on a GUI image with a translate and a rotate animation and it worked as expected.
    Can you take a screencap of what you have in your animator?
     
  10. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    @funshark
    Thanks for confirming that -- I'll admit, I'm animating these windows in a strange way, so I've included my animation window to show what's happening just in case. I have a feeling I must be implementing this the wrong way...

    Essentially, I have 2 animations from 2 gameobjects (both are panels in different heirarchies) that I'm trying to apply to only one of them. Each panel animates different properties. The "Login Menu" panel gameobject animates scale of the entire hierarchy (on the Base Layer) and, fyi, this gameobject is where I'm trying to put both animation clips on. The "Blend Layer" on this gameobject has the animation clip from the "Main Menu" panel gameobject (whose animation clip I stole to put on the "Blend Layer" of the "Login Menu") and is now meant to be used to animate the pivot/anchor of the "Login Menu" panel while the animation clip from the "Login Menu" animates its scale.

    I have a feeling I've animated these clips improperly or something, so any feedback on the right way to animate for this 'general' purpose would be great (I'm very confused by what animates what, when and where you click or modify values for heirarchies, as it seems Unity is trying to be very specific when I'm trying to animate generally) --- but here's what I have right now:

    Base Layer
    Base Layer.png

    Blend Layer
    Blend Layer.png
     
  11. funshark

    funshark

    Joined:
    Mar 24, 2009
    Posts:
    225
    Yeah, i'm very confused too since I didn't understand what you're trying to achieve ahah :D
    Seems you're overcomplicating things.

    btw, the yellow color in your animation view means that nothing is attached to these properties so you can't see anything moving actually :)

    Could you do make a drawing that would explain what you want to do clearly? It will be easier for me to understand and explain what to do ( at least one way to do it ).
    Maybe the issue is just the yellow lines...
     
  12. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Sorry about that -- what I'm doing involves 4 things, nothing more:

    - The Login Menu panel gameobject
    - The "Login Menu_Open" animation clip (assigned to the Base Layer of the Login Menu panel gameobject)
    - The "Main Menu_Open" animation clip (from an unrelated panel gameobject called "Main Menu")
    - The Base and Blend Layers of the Login Menu panel gameobject (which is the only gameobject we're working with)

    What I'm essentially trying to do is simple:

    I'm trying to steal the animation clip from another unrelated panel gameobject (in this case, the Main Menu panel's animation clip) and drop this clip on the Login Menu panel's Blend Layer (in this case, the Login Menu is the panel I'm working with in my attempt to get the two animation clips to play simultaneously). The Login Menu panel already has a clip playing on its Base Layer, so I just sync the state-machine in the Blend Layer with the Base Layer so I could use a different clip on the Blend Layer to play alongside the clip playing on the Base Layer at the same time to animate different properties together.

    To play two clips simultaneously, I need the Base and Blend Layers to do this. Since the "Login Menu_Open" clip is already assigned to the Base Layer, I just made a new layer calling it "Blend Layer" and assigned the "Main Menu_Open" animation clip to its Open/Closed states. Since this new clip supposedly animates anchors/position/size/etc on the Blend Layer, the "Login Menu_Open" animation clip on the Base Layer (assigned to the Login Menu panel Base Layer -- again, the Login Menu panel is the one we're working with) shouldn't conflict with the Blend Layer because it's just changing the Login Menu's scale with the "Login Menu_Open" animation clip.

    The two panels have exactly the same properties and hierarchies, and the new animation clip on the Blend Layer doesn't touch any of the properties animated by the Base Layer's clip assigned to the Open/Closed states (the two states simply play the same clip either forward or in reverse, so no weirdness there). The animation would ultimately become a panel that is simultaneously scaled up/down + made to slide up/down just offscreen whenever it is opened/closed. Or at least that was the idea. :S

    ==============
    My take on this:
    ==============
    It should work. My only guess for why it's not working is that it has to do with those yellow properties in the clip itself -- something of which I don't know how to avoid in the creation of my animation.

    As said before, if I knew how to create an adaptable set of generic animations for those properties (that would work on any game object the clip is attached to), I would be all set. Any advice on the reliable creation of this sort of animation fit to work on any generic object would be highly appreciated.

    The reason for the weird properties is that I needed to make the panel go automatically just offscreen exactly far enough that it would work for any screen size/shape, so I had to animate the pivot offsets to put it in the correct position (which modified the anchors and all that jazz automatically) and this probably resulted in the broken links to that Main Menu panel object's anchors/etc. --- but I don't know what the heck to do to fix this when making the animation since the offscreen animation functionality is absolutely necessary for my project (and thus is the sole reason I used it to test layered animations). D:
     
    Last edited: Feb 10, 2015
  13. funshark

    funshark

    Joined:
    Mar 24, 2009
    Posts:
    225
    Ok so we're focusing on the yellow thingies.

    Basically, you can use an animation on different gameObjects only if these gameObjects share the same properties.

    To assign an existing animation on a gameObject, you have to make sure it has an animator on it, open the animator, and drag&drop the animation in. If the animated properties exist on the object, they will be automatically renamed; if not, they will change to yellow.
    Same thing with hierarchy : if you have created some animations on a hierarchy, you can't use them ( unless you're only interested by the root animation ) on an gameObject with a different hierarchy.
     
    awesomedata likes this.
  14. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    So, as it turns out, due to your comments above, I decided that, since both hierarchies are shaped somewhat the same, at least from the top-level stuff down (minus some specific details further down the heirarchy), I thought maybe it might be the names. So I simply renamed the "Login Panel" in the Login Menu's hierarchy to "Menu Panel" (the same name as the same-level panel in the Main Menu's hierarchy) and suddenly the yellow disappeared! The animation then immediately worked as intended! So apparently the hierarchies don't have to be identical -- just close enough in shape and name to be recognizable by Unity.

    Therefore, I just wanted to say thank you so much for saving me more weeks of struggle with this! Your help not only solved a problem I've suffered with for months, but made the system I had in mind actually possible! So, once again, thank you very much dude!

    As great as Unity is, its documentation has led to many headaches for me. I'm glad people like you are out there to help people like me through these obscurities.

    No idea where you learned these obscure rules about the animator from, or if I just somehow overlooked them, but I'd like to know where to learn this sort of stuff from if you wouldn't mind telling me! I didn't see it mentioned in the docs or in any videos I've seen about animation in Unity, and yet I'd like to pay it forward one day -- so I hope you'll tell me. It'd be cool to help out others in my position like that one day!
     
  15. funshark

    funshark

    Joined:
    Mar 24, 2009
    Posts:
    225
    Glad it helped :)
    Yes, the names are also something Unity is looking at and this is troublesome. I'm not really in love with the actual Animation editor :/

    About the hierarchy stuff, I haven't really tried myself but since it worked fine with a single object ( and different names ), I thought it would be the same with a hierarchy ( as far it's the same one ).

    I learned that just from testing things directly :)
    I also wanted to use only one animation on different gameObjects ( like before ) and so I tried to understand the logic behind animator and animations... Didn't find anything useful on forums so... :)
    Same story with the layers; I knew it would work with a mecanim character and I wanted to have a more flexible animation system so I just gave a shot.. and hopefully, it worked :)
     
  16. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Haha! Nice one. :)

    Yeah, it's sad you have to discover stuff like this by trial and error and what is, effectively, reverse-engineering the system you're working with lol.

    Nothing wrong with that in and of itself -- but that sort of thing should be done for fun, not out of necessity.

    At least Unity supports this feature though -- it might have bothered me more if it didn't actually exist (which would mean waiting for a major rewrite of the system or never having it at all), so I can't complain too much. Coming from other way more limited engines, it's nice to have one with too many strangely integrated and/or documented features rather than too few features to begin with. :)