Unity 2D Swappable Rig!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so hello again another exciting unity tutorial with me Romi fuzzy and in this video I'm going try to explain how to create a 2d rig but with a swappable body parts so we can create a custom or different characters with just three character sprites and the setup this time will be slightly different and then our last video where I explain how to create 2d rigs and it's a bit more complicated since it's involving creating the a sprite library and I'm going to explain this in a bit and the sprite slices each of the body parts needs to be sliced by itself so we cannot do a quick rig like the previous example so I'm going to start set up the character sprite so here I have the first character which is character a let's just set this to multiple and then go to the sprite editor press apply okay so when we set this sprite to multiple we will have this sprite but we won't have slices so in order to slice this we can just press the slice button and then choose automatically and then press the slice button and it will create new slices and now we need to rename each of the body parts to make it clear so the first one I'm going to rename this to head character a head I'm going just to delete the number and then add the body part name and this will be the body and this should be the pelvis not out this how this and then this would be the right arm and this would be the left arm and this should be the left leg and this will be the right leg so after we rename all of this slices now we can go to the skinning editor and then press apply now inside the skinning editor we need to assign bone to each of the body parts so in order to do this let's just choose the create button the create bone button here and then pick the body part that we want to create a button just double-click on the sprite and then create and this one would be the head and we can only add we need to only add one bone here and double-click on the neck and then create a button sorry mother Pat them create a bones and then here for the body just create one bone for this example and then for the pelvis we can just create one button here not one metal sorry one bone and for all ready hand we need to create three button sorry I keep saying button it should be bone don't know why what's wrong with me today and here let's just create three bones also for the hand and for the leg let's just create three bones and we can adjust the proportion of the bone the length of the bone okay so now that we've created all of the bone here we need to rename those bone here I'm going just to adjust the leg bones so the knees line with each of the leg here and for the hand I think it's already good enough and now we need to rename the bone and I've already set up a naming here so let's just go back to I'm going to apply this first and enclose this and here I have note I'm just going to open this note here and this is the bone naming that I've created so far so let's just reopen and you'll see why I create a specific name because each of the character should have the same bone naming so I'm going to go back to the skinning editor double click here and this would be the head and this will be the neck and this will be the body and this would be the pelvis and here we have three bones so this should be B this should be the right upper arm and I'm not sure if we have an underscore or we have an underscore so I need to put the underscore here and then this would be the right forearm and this will be the right hand and the same goes with the left hand so let's just quick rename this ok so now we've done all of the bones here and we don't need to change the depth because this will be a separate sprites instance in the scene and now we need to set the geometry so let's just generate the geometry and this is the best setting that I've found for the sprite and this would be different depending on your sprite size the image for the image resolution size so let's just you can just play with the settings and see the best subdivision and now let's just uh uh as you can see here I have an issue so I'm going to go to the Edit bone here and I'm going to delete this bone here and I need to recreate the bone so let's just create a bone and double click on this art here on this pride slice and then create a new bone because apparently when I create this before it's still selecting this leg here so that bones have belonged to this pride here the previous one so I need to just rename this quick okay so now while this selected just go to the auto geometry and then generate for selected and it will generate and now I'm going to double click this and I'm going to generate for sector all of the sprite parts here we need to generate okay so now that we've already set up our character let's just press apply and now you see that I've already set up for the other two character here if I go to the sprite editor it's already been sliced and then you see it already been renamed and if I go to the skinning editor and then go to the edit bone here you'll see that it's already been generated the mesh is already the geometry is already been generated and also the bone is already been linked to those geometry so now in order to do this I'm going to create a new empty game object and this will be our character so let's just call character and I'm going to expand this and I'm going to put all of the sprite here we had the body the arm the left arm the left like the neck here the pelvis the right arm and the right leg okay so now we already put all of this art here let's just set this into position so already have and the neck I'm going to move this here and for the body I'm going to move it here and set the pelvis like so and for the arm I'm going to position this and for the left arm let's just sorry that this position the arm here and we are going to fix the position and the rotation okay so now you see we already set up the character and now it's it's a good time to create the ordering so I'm going to reposition the neck here and I'm going to hide that deleted arson light we don't need that so let's just put the neck in right position and put the head now that we have this rightly position we need to adjust the order so for the body I'm going to set this to zero and the pelvis to negative one so it stays behind the body and for the left leg it's going to be negative two and for the right leg it's going to be zero should be okay because pelvis is negative one so it will be in front of the pelvis and this will be thing this should be true and this should be negative three so it stays behind the pelvis the left leg and also the body and this is zero so this should be one like and the head should be two so I'm going to move the hand to the tree in the other layer so it stays in front of our face now that we have set this up here let's just select all of the rib here and then we want to create a add this tripe skin component and now we need to create the bones now once we've created buns you see that we have bones for each of the body part and now we need to parent this object to the parent bone here so if I expand the neck here I need to move B head to be the child of our neck so in order this way if we rotate the neck the head will follow the neck rotation and the same goes with our hand so let's just expand our body here and I'm going to parent the left arm to the body bone here and also the right arm and now we can rotate this and we can reposition also the same goes with the left arm we can rotate this by dragging the most upper bones here and then we can move this here okay so we can put the hand in the right position and the same goes with our leg and the leg will be the child of our pelvis bone so let's just expand the pelvis and then put the right leg and also the left leg to be the child of our pelvis and the neck should be the child of our body here and the body should be the child of our pelvis but the pelvis bone so now if we expand all of this by holding alt and then press the expand I can you see that we have this hierarchy and now I'm going to readjust the leg a bit here so the left leg I'm going to move this slide here and right like I'm going to move this down here okay and now we need to set up the I K and this is quite easy we can just put the I care on the most upper parent game object so let's just type sprite okay sorry this just type I K and yeah we need to add the IQ manager and now it's this is exactly the same as our previous tutorial just add the limb okay and I'm going to reposition this here so it's near our hand and this would be the right hand I K and at the right hand the lowermost bones here the right hand bones as the effector and then create target and if it's it's the flip is wrong then we can just press the flip checkbox and it will flip correctly so now if we move you see that we have the working I K and let's just create a couple more okay I'm going to press for this part okay so now we already set up all of the AI K and if we try to move the pelvis bone here you see that the i key stays so we can animate this quite easy and for the I K I'm going to select all of the I K object and then enable the icon so we can see it better and now we have a or working character that can be swapped and in order to swap this sprite here we need to create a sprite library so let's just choose create menu here in the project panel and then we have this under the sprite we have sprite library asset so let's just call this body parts and this is how it works so we need to create category and each of the category will have a list so for the first category I'm going to set this as the head category and since we have three characters I'm going to set the size to three so we can insert the first head the second head and the third head and we need to add a label so I'm going just to call this a B and C and now if we expand each of the sprite here we can drag the cart or a hat to the slot here and the character B head to the second so up and also the characters he had to the third slot and once we've created the first category the head we need to create all of the other parts category which is the left arm the right arm and so forth and I've already prepared this so here how it looks when it's done under the commit folder I've already created all of the category needed so let's just use this so now I'm going to go to all of the sprite renderer object so all of the component that has Speight render I'm going to select all of them which is the one with these suffix sorry the perfect character a and it has all of the sprite component and here I'm going to add a sprite library and then a under the sprite library I'm going to drag the character swap here and I'm going to add the sprite resolver so now we can we cannot edit describe the resolver when we multi-select an object but we need to modify this one by one so under the pelvis I'm going to select the pelvis category so now we can change the pelvis as you can see here if I press this it change and the same goes with the leg so I'm going to change this to D this is the right leg and this would be the left leg and we need to change all of the category accordingly and once we select this we can change the body parts and the rig will still works all of the ikd animation will be intact and the body set this to body this is the right arm and where's the oh this is the left arm and the neck and the last one should be the head oh sorry I forgot to add here so let's just add the sprite library and then the sprite resolver and add this character swap sprite and set this to head and now we can change the head of the character as you can see okay so now we have this setup I'm going to add an animator and I've already created animation here so I'm just going to add the controller and this has a jump animation and you can edit this character from the editor like this but if you want to edit this to the run time we need to create a script and I'm going to open the script okay so let's just zoom this bit so basically we need to in this unity engine dot experimental unity 2d animation and this is for adding this sprite resolver class and I've create a public class that are serializable and I use this class for containing a data here and I also set this body parts as an array so we can set the body parts a groups of body parts and each of the body parts we can set different sprite resolver and more than one sprite resolver and this is for changing couple sprite resolver resolver together and you see what I mean so basically we need to set a button to to add a method that will gets triggered when we click the button and this button will trigger this switch part and the switch part will change the ID and when it changed the ID we are going to grab the labels array which is a string array there is an index of our ID current ID and the labels is a string that we defined here in the switch part because whenever we change the art of the category we need to use this method from the sprite resolver class which is the set category and label and it asks for a string category and a string label for the category we can just get from its sprite resolver but for the label we need to grab a string label which is a B and C and we are going to define this label here in the array and then it will get switched whenever we increase the index of the ID the integer ID so whenever we run this method it will gets increase and this is basically for to prevent the array gets out of index so whenever our ID is bigger than the length it will resets to zero using the modulus operator and we are going to pass this ID as the index of our labels and it will and this will return whether it's a B or C whenever we press the button and I'm going to put the script on the description and let's go back to unity and basically we need to create a canvas here so let's just create a UI and a button to test this out and for this button here I'm going to set up Bui first let's just set this to that resolution and I'm going to reposition B button around this position here okay I'm going to just reposition the button here and let's just change the text to a an arrow and I'm going to increase its size and set the button to around C the pixel swipe and I'm going to create three different button and now for the character we can just add the switch part scrip and we want to have three sections that we can change which is the upper body the head and then be like so let's just create three body parts and each of the body parts will ask for a sprite resolver so for the first one this is going to be the head so it's only it only needs one sprite resolver and the upper body parts we have neck body and both arms so we need for sprite resolver and for the leg there is three parts which is the left leg the right legs and then the pelvis so let's just set the last body parts to a the sprite resolver size to 3 and now let's just expand this and drag the pelvis should be the lower body part here the first and the right leg and the left leg and for the upper body part we are going to drag the body the arm both of the arm and then the neck and make sure you are dragging the game object that has the sprite resolver component not the bowl game object and for the Hat we are going to drag this to the first one okay and each of this body parts asks for a button so let's just reposition the button first and this is the second button for changing the body and this would be the third button for changing the lower body parts go back to character I'm going to assign the button to the first one first for them to the first body part which is the head second one to the body the upper body parts and the last one should be the lower body parts now that we have set this up we need to set the labels and this should match our category so if we go to our sorry Carter swap here it should be match it should match this label string which is a B and C so here if I go back to character let's just fill this with the correct string which is a B and C and now we can save the scene here and since it's already have an animation I've already create animation before with the corresponding bones names so if I press play you see that the character will be jumping there you go the character is jumping and I know it's not the best animation but if we press the help you see it changed the hat art if I change the body here it changed all of the body parts here and if I change the leg it will change the leg as you can see so you can create custom character avatar of sorts and yeah I know the setup it's a bit complicated but hopefully unity will address this soon it's going to be much easier if we use the PSB files since we can do the category and the labels inside the sprite editor so we don't need to create these fright library for my understanding but I create this a tutorial for someone that doesn't have access to Adobe so what if we are using a normal slice body parts via PNG format so thanks a lot for watching and I'm going to put the scrip in the description but unfortunately I cannot share the art because this is a character that I'm currently developing so hopefully you can use your own sprite for testing this out if you liked this video please hit that subscribe button and those like button thanks for watching bye
Info
Channel: Romi Fauzi
Views: 20,145
Rating: undefined out of 5
Keywords: Unity, Unity3D, GameDev, Game Development, Tutorial, 2D Rig, Character Rig, Swappable Body Rig
Id: hUERvxcKt_I
Channel Id: undefined
Length: 22min 28sec (1348 seconds)
Published: Tue Nov 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.