I thought I would share some tips and techniques I have come up with over several projects on some helpful ways to use puppet style animation rigs in Unity using just the tools and features in Unity. There certainly are great extensions (Smooth Moves), and third party apps (like Spine), and those will work very well depending on your needs. My needs were to have something native and leveraging built in features. While given some level of control for animators, and the ability to essentially retarget and control certain aspects externally. Here are some of tips and methods I use to do that: Setting up rig that allows for flexibility and a type of retargeting. Typically when you set up your animation, you structure things hierarchically and animate those pieces directly. That becomes a problem if you need to change the pieces or access/modify them dynamically. Since this particular character/animation will need part modification and access, I have structured with empty Transforms for the parts I will be animating. Like this: I try to use descriptive and generic names, this helps keep things clean and non specific. (and easily reusable) I have also placed an empty object as a child of each called “—sprite”, in that folder is where I will place the actual sprites. The sprite folders are centered on the parent. Additionally I used gizmo lines to simply draw lines to the parent. These aren’t really important, but they can be helpful to visualize things. Also, I added a custom icon to each parent container, you can do that from the inspector. This is very useful. Icons are selectable, so you can click on them to select the proper node while animating. This helps prevent clicking on the wrong elements or the nested sprites. Additionally, since it is on a gameObject (not a sprite), when animating you can use the normal transform widgets. Adding the sprites. Typically, will use full image of the character behind it for alignment. I will then go through and place all the elements in the sprite nodes of each object. Doing it this way means that you can simply align them the way you want. Since the pivot point is actually the node and not the sprite, you don’t have to go through and adjust the pivot point on all your sprites. ... adding the rest. Note about names: Since I am going to be doing skin switching, it is helpful to have common named elements for each of the skin elements. For the base, all the elements are prepended with “BASE_”. More on this later. After all the elements are placed and set up, I will then adjust the z-depths. I do it this for better control. If I need to put this character in front or behind another character, I change the layers or order on all the parts via script and the whole character will be in front or behind other elements via sorting order. Continued....
Next I will add the other skin elements in the same way. Now because I named them all with specific names, I can use the Unity search to show specific parts like this: and this... I will continue to do this with all the parts until I have everything in place. Continued...
Animating I will go ahead and turn off the other skins using the search and unchecking them in the inspector. image Now I will animate the character as normal, taking care to only animate the top level parts, but clicking on the icons. Skins via layers Once I have my animations complete, I will use layers to handle the skins. I will create a new animation for each skin, including the base. In the animation, I will only be animating the game object active state. I will need to set it for every piece. Again, using the search feature, I will search for each skin by name and activate or reactive them. I will then do this for each for each of the “skin” animations, I will repeat this process. In the case of this character, only some of the parts are replaced or some just overlay, so I will set them as needed. Making sure that every part is accounted for. If some aren’t they will bleed over the normal animations. Continued...
Creating the "skin" layers Now that I have my four skin layers, I set up the layers in the animator. (If they have already been placed in the default layer, delete them from there. ) Create a new layer, in this case I called it Base, and add the skin animation (gear_base). It should be the default. In the control widget, set it to “override” and since this is the default, I will set the weight to 1. Create a layer for each, and for those ones set the weight to zero. Now you have layers that can be used as skins. Programmatically, you will set them by index and weight (in this case either 1 or 0). The index number will be order they appear in the animator. The first layer with your animations on it is 0, so your skins start at one. The trick here is that you will need to only have one layer set to 1, the others at 0. So to change from the Base to the Archer, I will do this: Code (csharp): void TurnOnArcher() { anim.SetLayerWeight(1, 0); anim.SetLayerWeight(2, 1); anim.SetLayerWeight(3, 0); anim.SetLayerWeight(4, 0); } Though more typically, I will have the skins stored in an array elsewhere and do something like this. Code (csharp): void SetSkinLayerWeight(int i) { int layer_state; for (int n = 1; n < skins.Length+1; n += 1) { val =((n)==i)?1:0; anim.SetLayerWeight(n, val); } } Or however you want to do it, those are just simple example to illustrate the functions to be used. Dynamic weapons. The skins were done with layers, they could also be done programmatically, which is how I have set up the weapons in this example. When I set up the rig, nested in each of the hands, I placed an empty object to hold weapons. I am going to place all the weapons I need in advance in this holder and then turn them off. This certainly can be done dynamically, but for this example I have built them in. It allows me to accurately place them. Now they can be directly turned on or off via code with SetActive(). Everything inherits its parent properties, but it isn’t directly controlled by the animator, so there are no problems with fighting the animator. Continued...
Final: This is just one example. It isn’t a great fit for every solution, and it makes the animation a little heavy because of the all the parts. I probably wouldn't use this if there were tons of characters on the screen but a dozen or so are fine. You can also take it further by not having all the parts in place at first and just adding them as needed programatically. Hopefully this was useful, and here is a quick web player to show some of the switching, and there is button (Menace) for one of the animations, showing how the layer masking only affects the active states and can be used across all animations. https://dl.dropboxusercontent.com/u/28107/unity/RigExample/index.html Feel free to ask questions, and will try to answer them as best as I can. Cheers, ZG [Any updates or corrections I will add to this post]
Terrific tutorial! Thanks for putting this together. Can you further explain how'd you handle multiple characters z sorting? You said you'd put them on different layers but that's not really practical once you have many characters and/or spawning them programatically... What would you recommend for those situations?
I would only use layers if there few characters on screen. That example was for fighting game, so there were really only two on the screen at a time. Normally what would do is just use layer order instead. since all the z-depth are set already, each character only needs one number for the ordering. I will loop through all the children, find the renderers and apply the sorting order. Something like this: Code (csharp): private int _sortOrder public int sortOrder { get { return _sortOrder; } set { if(value!=_sortOrder) { _sortOrder = value; SpriteRenderer[] sprite_renderers = gameObject.GetComponentsInChildren<SpriteRenderer>(); foreach (SpriteRenderer spr in sprite_renderers) { spr.sortingOrder = _sortOrder; } } } } If I am going to do it a lot, I will cache the results of the find instead of looking for them each time.
I actually use this method for animated films in maya. I would love to try in unity. Thank you for the post. You should def make a video tutorial. This is the link to the film that I made using puppet rigging method. https://www.youtube.com/watch?v=B6iN5q6LlWs
You mean the lines that show the connections to the various parts? For that I use the Gizmos.DrawLine and draw lines from each child to the parent. In a bit more of an advanced version of the rigging/animating tool, I use Handles.DrawSolidRectangleWithOutline Using handles provides a little more flexibility for display.
oic... so it's for advanced users i think I never code the editor.. and when I tried to follow some tutorials I think it needs some basic understanding about the editor and it would waste my time as I'm currently working in a project. Maybe I'll try learning that when I finished this one. and also the Anim Rig component, i never know that the inspector could be that adorable
Not at all. No editor scripts or scripting are required. I do have a larger system in place for controlling and manipulating puppet rigged animations, but the above steps/method doesn't require any of them. It is just a base method to allow for flexibility and more control over the sprites that that make up the rig. To add connection lines for the nodes, one can simply add the following script to the main nodes: Code (CSharp): using UnityEngine; using System.Collections; public class RigNode : MonoBehaviour { public Color lineColor = Color.red; void OnDrawGizmos() { Gizmos.color = lineColor; Vector3 parentPos = transform.parent.position; if(transform.parent != null) // ignore if no parent (stage top level) { Gizmos.DrawLine(tranform.position,parentPos); } } }
Great tips, I used the same way to parent the sprites, now mi problem is to find some tutorials for organic animation with this inside tools.
hi newbie in this Unity. just want to know how to put keys on every part? i tried to animate the puppet but i can only animate those parts that are folder or group how or you call it. Also its possible to change sprite in specific timeframe. example i have open hand in frame1 and i wanted to change close hand in frame 10. how to do it? tried to change the parts by clicking sprite thumbnails in inspector but all frame was change.
You can animate/key any under the animator. If you want to animate a part in a folder/object, just animate on the timeline. Remember that because it is a child, it will inherit transformations. The idea behind this structure is to solve the challenge of wanting to be able to swap or modify sprites separate from the animation (or programmatically). So if you just want to animate sprites on the timeline, this setup will just be a bunch of extra steps you don't need. To swap a part on the time line, it is similar to weapons or close. Just put both images in the hand folder, and turn the closed hand off. On frame 10, turn closed on, and open off. This type of rig is good for that type of thing. (or if you want to control it programatically.)
I have a few questions regarding why you chose to do some things that you did with this set up - why not use the sprite texture attribute to simply swap to another texture inside the atlas, instead of placing every "like" object inside the scene? the pivot system still would allow you to register each weapon/attachment accordingly...that's exactly why the system was made. - why are you sorting your z depth manually when 'sorting layer' still works with this set up? From my vantage point right now the best thing about this set up is that it bypasses Unity's 2D system short coming of not giving you the ability to scale the sprite without effecting the children. By placing them all off outside of the skeletal hierarchy, you can scale them without fear of scaling the children. While this "doubles up" the amount of nodes, the animator has to keep track off, this seems to be the only way to get around this. But thank you for taking the time to share your knowledge and process! Very useful stuff here!
Thanks! And great questions. First off, just to be clear, I didn't intend this as a general solution. In fact it is unnecessarily heavy if you have an well planned pipeline. It is primarily designed for max flexibility. This is good for either A) early in process where the art pipeline is heavily in flux as it is useful for iterating quickly, or B) the other end where you have a live op situation where it is flexibly of source content outweighs the hit of having additional transforms in a rig(s). The second is what actually inspired structure, though first case is where I use it more often. A few reasons, first the goal of this set up was to ensure that art (final sprite), wasn't directly controlled by the animator at all. Swapping the sprite on the timeline removes that, and disallows for that control programmaticaly. Secondly, in the skeleton example, the weapons all have different pivots, but more importantly alignments. (so just setting the pivot won't work) The alignments are different because the weapons come from other atlases for characters. (mobile being the target, memory and texture space being a more critical concern). And frankly, the current method of setting pivots in Unity is a pain. You are correct, swapping sprites is definitely a good solution. In fact, its a better solution if you have a solid design and locked down/planned pipeline. A few reasons: Conservation of layers: I wouldn't use layers for the parts because of the limited number of layers, (though I assume you probably mean order in layers). Ease of change: Say you need to move one character in front of or behind another. You could have a method that switches all the layers or order. (Often this is what I do). Not a huge deal, but with the z-sort, you can simply move the parent's z to reorder the whole rig. I can have one layer called "characters" only occupied by the combatants (in this case). Change the z on the characters quickly moves them back and forth in that layer, and not needing to create pivot or managing values for an order offset for all the parts. I may even use order/layer for the whole character, but use the same value for all the parts, letting the z determine part order. But mainly, Timeline sort: Sometimes, depending on the animation type/needs, I may need swap depths of a part as part of the animation. (like swinging a sword or similar) If I set the order or layer, those supersede z, so I wouldn't be able to change that on the timeline. Not using using (different) order/layer on the individual parts, means I can still control them from the timeline. But just to reiterate, this is not intended as an optimal or best solution for every situation. It works really well for maximum flexibility, ease of retargeting and swapping. It is a little bulky for most situations, since every part will have two additional transforms. Exactly. In fact, the situation you describe is what started this. I had a some characters that were cartoon style that I wanted to do stretch/squash on the torso, but since the limbs were children, they were also affected. Not parenting them was pain to animate, as was inverting the scaling to match the torso. Though my goal was to make things easier and more flexible for my needs. I hope that most of this will become completely unnecessary as the Unity native tools grow and improve. Retargeting, improved usability of the animation tools, and hopefully some sort of grouping for rendering layers/ordering (or being able to apply a layer/order to a whole animation or something along those lines) are things I hope to see in future updates. (Though I believe retargeting was already announced) Cheers, ZG
Wow! This is a very nice post. Thanks for this info zombiegorilla. And thanks for making this post. I'm glad I found this.
Hey @zombiegorilla , thanks a lot for sharing your workflow and setup. It's very informative and beneficial to anybody creating 2D animations inside Unity, especially beginners of the 2D animation process like myself. Can you please clarify your original post regarding " the ability to essentially retarget and control certain aspects externally" If I may take a less than educated guess - to retarget you would simply swap out new sprites of different characters onto the animated rig you already created? I'm unsure what you are referring to when you say "control certain aspects externally". Thanks again for the information and clarification.
@zombiegorilla Great post and thanks for sharing. I use something very similar, sans the gizmo visuals, very nice touch! As my rig has gotten more complex I'm finding the animation editor to be unseasonably slow e.g. several seconds to rotate / translate a character's foot or leg. I've distilled it down to the number of properties I have in the animation. For a rig like you illustrate, which is of similar complexity, I easily have 20-30 properties keyframed across 8 key poses for say a run or attack animation. Not every pose has every property keyframed, but the beginning and end always do, and the other ones use 50% of the total. However, if I start an animation from scratch with zero properties and begin manipulating its properties (thus generating keyframes) things are fine, running as one would expect. Nice and smooth, immediately responsive. I've tried pulling things into an empty scene, starting from a totally blank project, etc, etc. Nothing seems to work, their seems to be a perf cliff with the animation editor that the complexity of my characters is pushing past. 1, Have you seen something similar? Am I right in concluding that this is an animation property / scaling issue? Or, is there something else causing it? 2, Any thots on how to work around it? TIA!
Hi, I using Puppet 2D in my game, and i found some issue when two monster walk together.How to make a monster overlap another monster? Any solution?
Sure, just z-sort the main container behind or in front. That is one one of the advantatges of building it heiariclly, is that controlling the top will also handle the children. Sorry, I missed this reply. Honestly, I have noticed this recently as well. It also happens with complex UI animations, and characters as well. There is definitely a point where the animation timeline becomes sluggish to the point of unusable. For UI we can get around it (to a degree), by animating parts separately and manually combining them. That isn't really a great solution for characters, it can be done, but it really reduces flexibility. So I don't really have a solid answer for you other than to hope that Unity puts some love back into the animation system. Given the recent releases, I am not going to bank on it.
I believe 2Ton reported both of these as bugs and Unity has confirmed they are still working on them. I'm unsure if the animation editor slow down was fixed in any update in 5.2 yet.
Hi zombiegorilla, I try changing z-axis to 1 & 0, but the result still the same, do you have other solution? Thanks
That's good to hear. I haven't noticed any improvement in 5.2 yet. I've downloaded 5.3, I'll take a peek at it this weekend.
yes each of the part etc: body,hand, leg, have their value in "order in layer" where sorting layer is default.
You could either switch the layer for one of the characters or remove the order and rely on z. I give some info here: http://forum.unity3d.com/threads/2d-puppet-rigging-tips-tricks.245564/#post-1624356 And some info toward the end of the very first post in this thread.
The last week I upload a character to the Asset Store using this technique. Here is a tutorial explaining how it works and how to change the colors. I hope you like it.
@zombiegorilla 1 note, and 2 questions... Note. I was using Patch 5.1.2p3 and it helped with performance a bit, but caused other frustrating issues, like not being able to save when in animation record mode . I'm currently working with 5.2.2f1... seems okay, but I haven't gotten to any big animations yet. Question 1. How do you deal with pose changes? For instance, when my hero runs I want him to use a side profile, rather than the default 3/4 standing / idle pose, like you have for your skele above. I've found pose changes particularly annoying to transition between as limbs can get out of sync between animations. Alternatively, sprite swapping via the animation timeline works better for transitions, but then I've baked sprite selection into animations which is what we're trying to avoid. FWIW, I recently learned calls in LateUpdate() will override animation, but it's still a pain. Question 2. Aside from the above, do you have a general strategy for dealing with transitions between animations, for instance from an idle --> run start --> run --> run end / turn around? I'm currently considering building one big timeline and then using Play() to begin and loop animations appropriately. This allows me to automatically update my idle --> run start transition as it's all part of one timeline rather than broken up between 4 separate animations. Any thots on this? Cheers!
Amazing work, guys!!!!!! -------------------------------------- coque samsung galaxy a9 etui samsung galaxy a9
So I had the sorting layer bug/issue whenever I had 2 of the same types of enemies overlap each other. No amount of z value change would ever fix the sorting issue. If both enemies used the same sorting layer...then their pieces would sort within each other. And I've fixed this...however I'm not sure if the fix is very performance efficient... After Before The solve was not to use sorting layers at all, but instead to just change the z value of the joint/sprite itself. (.0001, .0002...etc) I used a super small value as to not have to deal with alien parts floating drastically far from each other. And this works great on an orthographic camera. This seems to have shored up the layering issue, however you cannot have your pieces in any kind of hierarchy after the root. Now, since I've been using flash for the past 2 years and have avoided their ik system I've grown very accustom to animating without any kind of bone hierarchy...(means you have to animate each piece individually...) I'm also not sure how performance effective this technique is...or if it would even matter performance wise. Thoughts?
Hi all, I'm gushing the procedure of apparatus and invigorating a 2D supervisor character for my survival side-scroller amusement at this moment. I'm utilizing Unity to enliven and am experimenting with the SimpleCCD IK handle script surprisingly since quadrapeds are so confused. I've gushed right around 200 days of the improvement process over the previous year and this supervisor character is the last huge obstacle to completing a demo so I'm really energized. I stream every day and the kind of work changes frequently. Additionally I'm upbeat to help with any Unity/C# related inquiries if the current workload doesn't require an excess of focus. Don't hesitate to swing by at whatever time