Unity3D - 3D Model equipping | Scriptable Object Inventory System | Part 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to coding with unity today we'll be doing the 3d item model equipped Inge in your character so that your item models will move with your character animations we'll also be talking about different ways of doing it and the pitfalls and advantages of doing certain things we'll be discussing different programs you can use if you're not a 3d modeler and you just need to get some items into your game without actually knowing how to model or rig we're gonna use use and mix of modes who automatically generate the models and rigs we need to combine these characters that's where I got all the models that this character is currently using and I'll show you how I did that but so you know it is better if you're a 3d modeler which I'm sure if you are a 3d modeler you already know this it's better to make your own models and your own rigs and your own items then you can customize them and have them be more performant and things like that but for prototyping or programmers or anyone who's not good at art this will be a great way for getting assets into your game to quickly get up and running while waiting on the actual assets that your game will be using so to get started let's open up the project that we are using in the last video and you see I have this project opened up now so if we go and look at this character right here if you open up his hierarchy you'll see has an ethan skeleton and if you hold down alt and click the drop down arrow it'll show everything and if you click it again with all held down it'll close everything so it'll be closed the next time but anyways this shows all the bones for your character and we're pretty much going to do is we're going to create items that use the same bone names as the area where we want to put the item so if we wanted new boots we'd probably need left toe to left toe one left foot probably left leg because we need the bin for the blower part of the leg and we'll need the same thing on the right leg you can also use every single bone but it's a little less performant and I'll talk more about that when we actually get the code in and we had the models and the items that we're going to be using will rip the bones off of one item and I'll show you the difference why it's maybe not better it's a do that for every item and it's better to import your items as sets possibly or just have one armature the every item references from but we'll get more into that when we get to that part of the video so all you really need to understand now is your model has a skeleton each part of your skeleton has different names for that part of your model and it controls how that part of the model moves so if I was to click on left leg click on rotate and bend it it's gonna bend the left leg how that works is with the weight painting of the model the way painting determines which part of the mesh the bones are going to affect and that's all done inside the 3d modeling program which we aren't going to have to worry about in this video because miksa MO will handle the rigging for us and views is going to handle the model generation and item generation for us but all we need to do is create an item that also has weight painting and bones that says when the leg moves backwards this item should move backwards also and then we're just going to take the bones off of the item that we made and put it on to our actual character and tell our new item to use the bones of our character instead of the bones that are on the item that we created so it won't add any more bones to your hierarchy it'll just be using the bones that are already there and adding the object and its weight painting I hope I explained that well maybe I over explained it maybe it was confusing I'm not sure if you have any questions leave a comment below or easier join my discord I'll definitely answer you easier there but if you are gonna do that consider leaving a comment on the video also for the YouTube algorithm so it'll help the video get a little more traction and while you're doing that go ahead and like the video and subscribe to the channel also these videos take a long time to make especially since I'm doing a series and not one-off videos it's it's pretty dang difficult so the least you could do is click the subscribe button but anyways let's move on to getting views so I have an Adobe Creative Cloud account but last I checked views is free so you shouldn't need an Adobe Creative Cloud to be able to use it so we'll just go to fuse I mean we'll just go to Google and search views Adobe and here it is yeah it's still in beta and I'm pretty sure it said while it's in beta it's going to be free for everybody but I could be wrong if I am wrong yeah it's giving me the download button without being signed in so it should be fine yeah for a limited time fuses available as a free beta to Creative Cloud members so maybe you have to be some form of member and veiws comes free with it I'm not sure y'all just have to let me know on that if you're not able to use fuse guess you're gonna have to model your own items not sure but we're using fuse for this video so once you already have fuse downloaded go ahead and open it up and when you open it it's going to look something like this your layout may be different because I'm on a one hundred and forty four percent dpi for my monitor so it's easier to see the smaller objects on my screen so all we need to do is assemble a character inside a fuse so go ahead and do that I'm just gonna use all the basic male character pieces so once we have our model created we can send it to mix Emma to get the nude version of our model so let's just go ahead and click the send to mix ammo button I'll call mine whitey character model nude I'll click Save and it's going to export the model and send it to mix Emma once it sends it to mix mo the auto trigger is gonna run which may take one or two minutes it doesn't take long at all so just wait for the auto rigging to finish if you're not using fuse to create your models and you already have a model that's in T pose you can send that model to mix ammo and have mix mo rig your custom model as long as the character is created to look pretty much like a normal bipedal human mix the mode does a pretty good job at rigging the model but if you have a model with a really large head it may not rig it perfectly and you might be better off just using blenders built-in rigger cuz it works pretty much the same as the auto rigger inside of mix ammo but we're using mix ammo because we're using views models so it just it seamlessly works together pretty well once mix mo finishes rigging your model it'll load up and you can check and make sure that everything is rigged nicely mmm everything looks pretty good to me looks like it's moving pretty smoothly I have no complaints you can also lower your Gallatin LOD which is probably a good idea since we're doing item equipping there's no reason to have such an extensive skeleton so for your project you might want to disable the facial blend shapes and use a to chain finger or maybe no fingers I'm just gonna leave mine as default so we're gonna go ahead and just use facial blend shapes and standard skeleton 65 mainly because I want to push the performance of the system that I'm creating so if it runs good with 65 bones then it should run great with a two chain finger because pretty much happier models bones are almost in your hands alone so something to keep in mind so I'll go ahead and click finish and we'll use this character now we can download it but I already have one downloaded so I'm not gonna bother but go ahead and download yours and then how we're gonna get the items is we can go back into views and go to clothing and you have a whole bunch of items that views already has added into it that we can add to our character I'm gonna show you how to create a set of items and then we'll also use a set of items that I imported and created which is this is the one that I imported and created it's the knight armor it's the one you saw at the beginning of the video which is also a mix of mo item I sent the knight model from miksa mode to my friend and he cut the knight pieces into parts and then i reimpose pieces and a fuse so that i can put them onto a customizable character but thats a lot more work than what i'm gonna show you in this video I would have tooken me forever to do that and he did it pretty well you can see at the beginning of the video it has a few little problems like you can see right here the leg is sticking through a bit but in a final system you'd want to split your character into parts also and disabled parts of your character that there's a model on so you don't have to worry about the overlapping meshes and it's more performant no reason to have to render the tries underneath the model that's covering it up so let's just use some built-in fuse items for now I'm going to use the cop outfit I think it's called like combat I believe and after I have this set up come back practical once you have your character set up we're gonna send it to mix um oh I'll call this one tactical set now that our tactical set is also rigged we're just gonna go ahead and click finish it just keep in mind if you did your facial in shape changes and skeleton LED changes make sure it's the exact same on the items that we're doing now as the base character modeled that we did before the items need the same bones is the character model for this to work correctly so we're gonna use this character and we're gonna download it as an FBX for unity and T pose then let's go into blender and we're gonna import that object that we just downloaded so we'll import it if you don't have blender it's a 3d modeling program I don't think I need to explain that but just go to blender comm and download it or use whatever 3d modeling program that you prefer but we need to delete an object off of this model technically I guess you don't have to do that you can leave it on and just import the model into unity also but that's making the file way larger than it needs to be not that I'm making it as optimized as it could be and I do suggest if you're watching this video and don't know about much about modeling go look more into modeling because there's a lot of stuff you could actually do to make this file size smaller to help your game out but that's just well beyond the scope of this video and I don't even know all those things so I'm not a model but let's import the object that we just downloaded wherever it is somewhere in my downloads folder I know that much my downloads folder all right once it imports let's open up our armature and we're gonna delete the body all we need is the items so we can click the body the default and the eyes right click and delete those so you can see all we have is the items now there's no body inside so we're done with what we need to do inside a blender lean just export this out into a new file or you can overwrite this file either ones fine I'm just gonna go ahead and export it straight into the project that we're working on because we're gonna need it anyways I'm gonna call it tactical set and I put it inside of a new folder called models inside of the graphics folder we're gonna export the FBX straight into unity so if we open up unity you'll see it automatically generated the models folder and there's nothing in it strange might have had a selected only turned on on my export let's go to export FBX says it's in it I hear the ok it just took a minute to import it then it's gonna import into unity if you drag and drop it into unity it is massively ginormous and laying on his back so let's go fix that inside a blender alright so we'll go back into blender and we're gonna click on our armature and we're gonna change our size to point or our scale to point zero zero one point zero zero one point zero zero one and now we need to apply our scale but we can't just apply it to our armature because if you do it's gonna change the location of our models which is not what we want so I'm gonna hit control Z to get it back to being unapplied so our scale is still point zero zero one on our x y&z and what we're gonna do is select the models and not the armature and then we're gonna hit control a and apply all transforms or all rotation and scale now all our rotation and scales are zeroed out and our scale is one so we're gonna go back into our file export FBX and you noticed our we were laying our back also usually just click this experimental apply transform and it'll fix that if that ever happens let's go back into unity and see what happens nothing changed cool let's go to our armature click control a and we're just going to apply the rotation and nothing else now it's file export FBX once it's imported let's drag it in you can see the it's no longer lying on his back but he's still way too big one thing you can do is just change your scale factor to point zero one and click apply we just change the scale factor to point zero one to get the scaling right inside of unity normally you'd fix this inside of blender but I just spent like 30 or 40 minutes trying to fix it and the things that I normally do just weren't working and I'm chopping it up too it's because I have like seven programs open and I'm recording with OBS at the same time I'm just going to assume it's some type of bug because I can't get it to scale down from inside a blender I'm probably just gonna cut that footage it's not really worth watching let's just change our scale to point zero one hopefully it'll still work I've never tested it by just scaling it inside of unity but I'm not going to spend more time on it so we have our tactical set inside we need to bring the nude character that we downloaded into unity also so locate the nude character that we downloaded earlier in the video and bring him into your project into the same models folder and if you don't have fuse or a way to get models just download the project in the description below and you'll have all the models that are in my project to get you up and running I'm just gonna delete this because I already have a tactical set that I downloaded before that is working without the scale being messed up you can see my scale factors on one so I wasn't just talking earlier I actually was able to do it for some reason I couldn't now I'm deleting it using the old one so now that we went through the programmer hell of dealing with 3d models and we have our 3d models into our program but you can see we don't have the textures if you want textures you can go back and diffuse and go to file export export textures and it'll export all the textures of the items that you're using I'm just gonna drag in the textures that I already exported of my armor set from my other project and we have a bottle mat glove mat and hat map so I can go back to my armor set unity go to materials we can just set it right here we'll go into night materials bottle mat oh one glove map hat map shoe map and top map click apply now we have a textured armor set beautiful now we have our models in we have a armor set that is a tactical uniform by get an FPS shooter and we have an armor set like you would want in a medieval game and the next thing you're gonna want to do is import your character that you downloaded earlier and the materials for that character the way you'll get the materials is the same thing that I showed before go in the fuse you can take off the items or leave the items on I think you'll still get the nude body textures when you export but if you want to make it easier just click on an item and click delete on your keyboard and it'll remove the items and then you can go to file export export textures like the location where you want to export them use the physical based or unity 5 either one I believe will work fine maybe unity 5 will make more sense inside the editor but I think I used PBR based can't really remember so now that you have your character model imported and the materials for that model let's drag our character model into the scene here's our nude character let's apply his materials the same way as we did with the armor set we'll click on him go to his materials inside the inspector is exposed materials that came with the model are exposed and we can just drag them in click apply and there we go if you're following along with this and you're using fuse models and your eyebrows are messed up go to the material that your eyebrow is using which I believe is you can tell by just clicking on default in eyes it's using the for the eyeballs and for free the eyelashes so we'll go to four and we can change the rendering mode fade so you it'll go from this ugly black to the fade that we need for the eyebrows you can leave the others on the pake also adjust your sliders to get a better reflection that you want on your character but now that we have our character imported and everything set up how we want it let's put our character controller scripts on it so we'll be replacing Ethan with Joe I think we'll just add these files to our armor set body nude and rename this to player so we'll just copy and paste everything that we need so copy component go to the player base component values I think it's fine that it uses the Ethan avatar I believe their bone structure is pretty much exactly the same we'll paste this as a new component you can see when you paste the third person character controller in it automatically adds the other script in also that's a good thing we need both of them but copy the values from this script so it has the same values so we'll paste component values then we can copy our player script that we had created from the last video also and paste it onto here you'll see all the attributes and stuff are the same from the other character that'll make things a lot easier on us our tag is untagged in the layers default we don't have to worry about that so we go ahead and delete this character if you need it for reference for any reason just disable it then we'll go to here and the collider is good enough let's just click play and see if we're able to move around with the one last thing we need to do is if you're using cinema sheen we need to reconnect our target huh apparently our cinema sheen is completely broken that's strange let me fix my cinema sheen I read the project and it reacted our scripts to the main camera so we can go to cm cam and awesome it riad addthis I'm not sure if the values are the same we can fix it though but we still need to replace what I was going to say since it did update it back with the third person controller I'm gonna assume it updated everything we're gonna replace this with the player we just created we're focusing on the player and let's also make a prefab out of our player so just drag and drop it in here and make it an original prefab we don't want to make it a prefab variant of its like base 3d model we want an original prefab so we have our player is a model or a prefab let's click play and make sure everything is working everything's working beautifully except he's not animated at all the reason is if you go into your models and click on Armour set body nude go to animations or go to rig we need to change it to a humanoid and click apply let's click play again and he's still not animating let's use the armor set body nude avatar see if that fixes it there we go now it's working you can see we can do everything when you jump everything all the animations work exactly the same for our mix mode character as the animation that came with our Ethan model can we pick up item still excellent everything seems to be working exactly as expected let's just make sure we can equip stuff if the camera looks stuttery it's just because I have so many things open so now that we have our player working and our armors imported we need to start setting up the code for attaching the armor to our player so the first thing we're going to want to do is go into our item objects script and we'll add a variable slot to put our character display item so we have a UI display here so we'll put it right next to this we'll call it public gameobject character display then when we go back into unity and look at our items you'll see as a variable slot to put a character display the way we'll set that up is by going to the models that we created open it up and this is a bones let's go to an item that makes sense we'll use boots go back to your models and we'll drag in some armor set unity shoes now we need a way to pool this model from our item object and put it onto our character so that's going to be done currently inside of our player script where we apply our stats in the next we're gonna do a bit of refract during and we're gonna split our player script into the player stats and the player equipment script which is better because we won't have our player script clogged with a whole bunch of stuff and it'll just be a localized script for controlling all the things that the player does and not exactly how the player does it but for now we're gonna use our equipment place where our stats are applied and removed from and we'll add our armor here and remove our armor here and to make this a little easier to look at we're gonna change on before slot updated to say on remove item because we know when this fires were removing an item and when this fires we're adding an item so we'll say on add item that'll make it a little easier to read later down the line when we don't exactly remember what these functions do just a one thing to also say you could instead of saying string contact I learned that using the money sign is just as performant and then we can just put these right here so that's pretty much saying the exact same thing but I believe it looks a little cleaner to me and it's a little easier to write it's really your preference though just place a money sign and then you can put these around all your variable calls but anyways inside of here we're gonna add the item to our character how we're gonna do that is we need to check does the item that we're equipping have a character display so we'll say underscore slot die item object die character display is not equal to no that means there's a character display that needs to be added to the item and not just stats that need to be added to the item now we need to figure out what type of item that we're adding to our character and how we're gonna say that is by saying switch then you can hit tab and we'll type slot Dot allowed items 0 so what this is saying is we're getting the inventory slot that we just dropped an item onto and then we're looking at the allowed items of that inventory slot so if we go back into unity and we go to our equipment and we look at it all of our slots haven't allowed items we have a helmet a weapon shield weapon chest and boots so you're probably thinking well two of these have a weapon so how do I know which one's a weapon and which one's a shield in this iteration of the system just make sure shield is an element zero and we know that that's the offhand and how we're gonna code it you'll be able to place a weapon or a shield in there it'll work perfectly maybe not the best idea maybe we'll come up with some type of offhand thing instead but because of how we're coding it we want main hand weapons to also work as off hands so we can't just tag up off hand type items unless we add in a double tag system which I don't feel like doing this is much easier than that so just make shield in your element zero if you want dual weapons that is so now we know using slot allowed item zero what slot we just placed an item in so let's make a case for each item type and since I'm using jetbrains there's actually an easy way to do that I can just hit alt enter and generate switch labels then remove the ones I don't need we don't need food and we don't need default helmet weapon shield boots and chest sounds like all we need now we need to make a variable slot to hold the 3d model that gets generated for each one of these items and maybe you're starting to see why this should be split off into the player equipment class now because we're cluttering this up with so many different things that it really doesn't make sense so I'm gonna type in all the variable names for the items that we're gonna use and we'll get back to it so we have a variable to store each item we can go back into here and we're gonna say things like underscore boots is equal to some type of script that we're gonna access to combine our bones let's call it bone combiner you can see I'm adding these underscores now for my variables that are in scope for the whole file it makes more sense to me and I'm starting to switch all my things over to that I guess I'll stick to my old way of doing it for this video but yeah I'm switching to that for now on so we'll have some type of script called bone combiner that will create on the entity that needs bones combined then we'll access script and will have some type of function that says add limb and on that function we're gonna pass in that character display that we created which would be accessed with underscore slot die item object dot character display so we need to set this up to make it work meaning we need to create the bone combiner so let's just line this out for now so it doesn't give us an error go back into unity and let's create the file so we'll say great c-sharp we'll call it bone combiner go ahead and open it up and inside of here is where the real meat of this video is that it's probably what you actually came here to see and I'll probably tag what part of this video starts out at the beginning for people who just want to see the code itself and aren't worried about how this integrates with the rest of my series it's really not a lot of code to do this either it's just can be really performance heavy depending on how you do it the way I ended up doing it there's no telling if other people do it this way or not but I got it to end up being as performant as I can reasonably think to get it without just coming up with some crazy other ideas so let's talk about that so now that we have our bone combiner script up and we know that we're trying to add a limb using the character display and our bone combiner script the first thing we need to do is get rid of this boilerplate code and now let's talk about how this is going to work I gave a simplified understanding of it in the beginning of the video but I don't know how well explained that was it was like two hours ago now at this point maybe two and a half not sure but so we have our player who has an armature and he has all these bones on it and we have an armor set which has all these bones on it we want to add the bottoms of our armor set to our player the bottoms if we could see our bones at the same time do we have this set open we have this set open so we can x-ray these bones that I'll be able to explain it so we're trying to add pants onto our character and the pants and our character used the same exact bone structure we have these legs here so to bin our pants we need these bones and these bones maybe these bones and maybe these bones you can decide that on your own you might want a little bit of Bend on your pants like when your foot moves it's really depending on how much performance you can allow towards your item equip thing to depend on how nice the item will turn out being once it equips if you want to just apply the entire armature to every single item that you put on you can do that it's just going to be 67 calls every single time you're gonna be needing to reference your characters bone structure and your items bone structure 67 times so that's your choice you can do it for sure but 67 is a lot more than what 1 2 3 4 5 10 so your choice so we just need the bones of the object that we want to equip or we can equipped every bone how we're gonna start off is by equipping every single bone and then I'll show you a way to only equip the bones that the item requires and then I'll show you why importing all your items with just the bones that it requires and importing the entire set with one armature both had their caveats importing each item separately ends up being a lot bigger file size wise than just importing the whole set with one armature mainly because of the bone structure and stuff so there may be other things you may be able to think about to make the system better but let's move on for now we will be removing the bones from this model and using the bones from our character instead so let's do that in code inside our bone combiner script let's first create a dictionary of our characters bones because we don't want to have to look up our character's bones every single time we equipped an item let's just do it when the game starts and then we won't have to worry about it again go make a public read-only dictionary and the dictionary is gonna be an int which is gonna be the key for finding the transform which is the reference to the bone we'll call it underscore root dictionary or root bone dictionary now let's make another private read-only array and it's going to be an array of transforms and we'll call it bone transform and we'll give it a new transform with the size of 67 so with this bone transforms array is is it's going to be an array that's set to the size of our bone structure and it's gonna hold the bones that return from this root bone dictionary so if we're equipping gloves we'll get the glove bones from the root bone dictionary using the bone names of the item or equip ting as the key and return the root bone dictionaries transform and add it to this transform array to give those bones to the item or equipping that'll make more sense once we add in the rest of the code now let's just add in a private read-only transform is just to hold the transform of our root objects transform or our player now let's make a constructor for this class we'll say public bone combiner we'll pass in a game object which the game object that we pass in will be called a root object and what the root object is is the player's essentially now that we have our constructor will use this constructor in the side our player to create the bone combiner so now we need to make the add limb function so let's say public transform a head limb and we're gonna pass in a boned object so we'll say game object boned obj which what that is is the item or limb that we want to attach to our player so as you can tell maybe from the naming you could also use this for more than just equipment you could actually attach new arms or new heads to your character and things like that so inside of here let's save our limb equals and what it's going to equal is a function that we haven't created yet we'll be creating it next and it's gonna be called process bone object which basically is going to be creating the boned object that we'll be adding to our player and we need to pass in a renderer so we'll say boned obj dot get component in children and we'll be getting the skin mesh renderer we don't have this added yet so let's just line it out so we don't get an error we'll just say return transform just to get rid of the air so now we need to make this process bone object function the is where the meat of this class takes place so we'll say private transform process boned object and the processed boned object needs a skinned mesh renderer we'll call it renderer now we can add this in add a semicolon on the end this function will return the limb and let's finish the process boned object inside of here we need to first create our sub object so we'll save our boned object equals new game object dot transform that's going to instantiate a new game object into the scene and then return the transform of that game object to this variable that we created now we need to add a renderer to it so we'll save our mesh renderer because we need a reference to that renderer that we're about Dada equals boned object die game object dot add component the component that we want to add is a skinned mesh renderer now let's get the bones on the skinned mesh renderer that we passed in so we'll say for our bones equals renderer dot bones using those bones let's do a for loop over the bones dialing and inside of here that bone array that we created earlier we're gonna populate it by saying underscore bone transform I equals and this is where we need to use the root bone dictionary meaning we need to make a way to populate it how we're gonna do that is create a function called triggers hierarchy and we're gonna pass in our transform now let's create this function I'm gonna move it to the bottom of the class and this function is gonna be quite a heavy function but we're only gonna use it one time when the game is started for each character that needs item equipping actually so you could actually do this in the editor maybe but we're gonna do it when the game starts so we have a private void traverse hierarchy and it passes in a transform called transform and we'll do a for each loop and we're gonna be doing the loop is over the transforms the variable name will be child so we're looking for all children and transform now we'll say root phone dictionary dot add child name make the for each a transform child in transform then we'll say transform child named I get hash code and we'll pass on the child itself what 'get hashcode does is it returns the hashcode representation of the name this should just make it a little quicker for accessing the dictionary than using a string so we're adding the child into our dictionary and we'll use the hash code of the child's name as the key to return the transform now to make sure we add every single child that is on this object we're gonna do something a little heavy what we're gonna do is rerun this function and pass in the child so we'll just keep rerunning this until we run out of children now we can use traverse hierarchy right here to populate our root bone dictionary and now that our root bone dictionary is populated when the game starts we can go back down to this for loop that we are creating and say underscore bone transforms I equals underscore root bone dictionary bones I die get hash code now to use the name from the bones on the object that we're adding to our player it'll use the name of that bone and use it as the key to find the reference of the bone that is on our player and we'll return that and store it in this bone transforms array so that we can apply these bones to the mesh renderer that we just created so now that we've assembled our bone structure let's assemble the renderer by saying mesh renderer dot bones is equal to underscore bone transforms then we'll say mesh renderer dot shared mesh is equal to renderer die shared mesh then we'll say mesh renderer dot materials is equal to renderer dot material with an S on it make sure there's an S on both element case your items have more than one material now let's return our boned object return boned object so we created our sub object we added the renderer we assembled our bone structure and we assembled our renderer then we returned our boned object which returns it to the limb and now that we have our limb created we can say limb died set parent and the parent it's going to be underscore transform and that's everything that's all of the code that we need for adding an item to our player zoom out on it a little bit so you can see more at once and then I'll zoom in on it also and just in case for some reason our code hasn't been Eames easy to read for people all right excellent let's go see if we can if this is working we just need two unlined this and create our bone combiner inside a player so we'll say pry the bone combiner bone combiner see how JetBrains recommends putting an underscore that's one of the main reasons I'm starting to do it inside our start function will say bone combiner is equal to new bone combiner transform game object dot transform we just pass in the game object my bad and we want to go into our bone combiner remove the mono behavior then we can save that go back into player that yellow line goes away so we have our new bone combiner created so we can use the bone combiner to combine a limb so let's just copy this for each area and then fix the slot that it gets reference to our helmet should be a helmet weapon should be a weapon thing I caught his sword shield should be an offhand boots or boots chest his chest all right so all the codes should be set up now it should just work let's go back into unity make this smaller let's set up some items we have the boots set up already so I guess let's just test these boots and make sure they work let's see what these boots look like it's the leather looking boots let's click play and see if it actually works we got in there ki not found exception in the bone combiner right here where we're getting the bone name we forgot to say dot name we need to get the hash code of our name instead of the hash code of our transform array entry so now that we made that change it should work so he picks up some boots put them on and it works puts we got some type of material there that's strange not allowed to access render dot material on prefab object use renderer diet shared material instead isn't that what I'm doing oh I forgot the type shared materials change this to say shared materials then we're gonna save go back into unity click play and see if we get another error and there we go it's working without an error it's moving with our animation you can tell the toes are moving a little bit if we do this we can hit shift F to focus on our player then let's just get a better view you can tell it's bending with our knee cap it's doing exactly what we expect it to cool so let's just set up some other items so we can have a full set that our slots allow us to I'm not gonna set up the other slots because if you watch the previous videos or if you have your own inventory system you should know how to do that so that's that's just gonna make the video an extra 30 to 45 minutes longer for me and for my own sanity I'm not doing it so let's set ups the we can set up a helmet a chest and boots so our helm one will be a hat our chest one will be the tops so we have boots chest and helmet set up we got to add in different code for the sword and shield we're not gonna use a bone combiner for that because that's overkill so we have a chest helmet and pants set up let's make sure those work don't exactly remember which the items look like for that so we'll just pick up a bunch of stuff and test them is it this one is it this one yep cool and we do know what the boots are so excellent it's working perfectly we can focus in on our player with shift F you can see everything's moving with our animation there are these little gaps though and to fix that you can split your character into pieces and either just disable it and enable it or you could actually delete it completely and re-add it with the same exact code that we're adding items with but it's probably more performant to just disable and re-enable it so yeah that's how you combine items I'm sure that's what most of you came here to see which we're pretty much done but it's actually extremely performance heavy to to access the name in a for loop as many times as we are so let's look at the performance impact of how we're doing it right now so we'll go to windows analysis profiler and you can see I have my profiler open but I have it on my secondary window so let's pick up some boots and I'm going to bring this right here so you can see it actually will just dock it to right here now that it's stabilizing a bit let's go ahead and put the boots on and see what type of spike we get pause it click on our spike let's do it go to the hierarchy view and we can just type in the name that we're looking for you just have to record stock recording to do that we're looking for the add limb function so here's bone combine or add limb it allocated 8 kilobytes of garbage and took 1 point 4 8 milliseconds that's not bad if you want to leave it like that that's fine but I'm gonna show you a quick way to make it faster if we go inside of this and take a deeper look at it the bone combiner runs processed bone which gets the item it runs 67 times if we look at object get name 4.6 kilobytes of our garbage collection comes from that so we can get rid of all of these object get name calls by doing them inside the editor and just storing ball in our item object instead of referencing all of the names when we add the item it's pretty easy to do let's do it real quick turnoff deep profile we've played mode so we need to store the bone names of our character display bones on the item object to do that let's open up the item object and right here is the item object so inside of here let's say private void on validate which on validate runs inside and outside of play mode every time a variable changes so inside of here we're just gonna every time the variable changes look and see if we have a character display on this item object if we do we're gonna see does that character display have bones I a KA a skin mesh renderer and if it does we're gonna store those into a list so we'll first want to clear that list because we don't want to keep re stacking it but before that let's actually create the list so we'll say public item or public list and the list is gonna be filled with strings we'll call it bone names equals a new list of strings and every time on validate runs let's do bone names not clear you could also do is there bone names already if so just return out of this but then it wouldn't update when you change the item so that's your choice this isn't like how I'm doing this there's a no way a blueprint way of doing it it's how I'm doing it and you should pretty much take it for a grain of salt do more research and figure out if it's the way you want to do it or not but it does work and I'm showing you the profiler results of it being reasonably performant once we do this it's pretty fast and it's hardly any garbage collection so it should be perfectly fine for a production game to be honest I'm sure there's plenty of production games that have done it worse in my opinion so if there is not a character display we'll return we don't want to run any more code and now we'll check if the character displayed doesn't have a skinned mesh render then we'll also return and not run any more code so if it does have a character display and the character display has a skinned mesh render let's get the render by saying var renderer equals character display diet get component skinned mesh render now let's get the bones off that skin mesh renderer by saying bar bones is equal to render err diet bones now we'll do a for each loop over those bones so we're doing a for each loop over all of the bones and getting the transform4 each one so we'll just say bone names dot ad T dot name so we have a list of bone names inside of our item object that'll get stored every time we add or change or remove a character display and any other variable also so now that we have this list of bone names inside of our player we can just send that list of Bo names along with the item that we want to equipped so we could just say underscore slot item object bone names and we can put this on each one now we'll go into our add limb function and our add limb function needs a list of strings called bone names so let's make the list of strings and call it bone name what we're gonna do with that inside of this function is just pass it off to the processed boned object so we'll pass off the bone names to the process boned object going down to here and adding the list to here too so we'll say list of strings call it the bone name now using this list of strings bone names we can actually get rid of this call to renderer dot bones which is a pretty heavy call in our profiler garbage collection wise so that's a positive and we can use bone names count instead of bone stock length and then right here instead of saying bones name dot get hash code we can just pass in bone names hi dot get hash code and that gets rid of the dot name call gets rid of the renderer dot bones call and it gets rid of the bone sock Cal call but I mean that doesn't matter so let's say even make sure everything is still working and then we can compare the profiler to see if it's actually worth doing that or not first let's go into our items and make sure that it updated our bone names so this has shoes and we open it up and it's got a bunch of bone names cool so it worked let's click play and see if we can equipped item still or if it's just completely broken pick up our boots and they equip just fine helmet chest excellent everything's working so let's open up our profiler and make a profiler comparison to see if that was worth doing or if we should have just left it how it was pick up an item we'll do it with our chest click record or we need the D profile click deep profile and record let's pick up an item think this one is the one that has an item on it wait for this to stabilize a bit once it stabilizes drop it on and click pause so drop pause well we got two spikes uh Shh taki mushrooms that's never happened before do they both have an add item call or add limb I mean this one does not have an ad-lib call so that must just it may be a dprofiler editor thing there here's our add limb call but you can see it's only two hundred and forty bytes of garbage collection and took 1.15 milliseconds so that is definitely worth it definitely you see this spike right here this was a garbage collection spike don't know why we got it but how much kilobytes we were applying for each item that we are equipping before what it caused a lot more of those garbage collection spikes and your game may have been running perfectly fine none of your codes actually lagging but you're getting these weird spikes garbage collection so if your time millisecond is perfectly fine and your garbage collection is like five kilobytes or one megabyte or ten megabytes you need to stop and take a deep look at your project and see what's wrong so you can see we made a huge improvement by doing that it's totally worth it might make our file size a little bigger when you build the game out it needs to build out all those bone names into the item objects but the slight file size difference is definitely worth the game running better while people are playing it everything works just as expected the last thing we need to do is just add in the ability to do shields and swords and we'll be done shildt's and swords is pretty easy too luckily we just need to instantiate it and apply it to a transform I'm sure most people if you've gotten this far in this video you could probably figure this out on your own but I feel like I would be cheating people if I didn't just go ahead and add it so let's go ahead and add it in so shields and swords are gonna be done completely different first we need to make the transform of where it'll be applied at so we'll say a public transform and for the sword we need a transform for when you're holding a weapon in your main hand so we'll call it weapon transform now we need a transform for when you're using a shield we'll call it offhand wrist transform because the shield usually goes on your wrist but in our game you're going to be able to use a weapon in your offhand - so we need a transform for where the weapon goes will say offhand hand transform all right this one's not grayed out and these are it's strange okay anyways let's go down to here and we'll do the weapon first because it's easier all we have to say is instantiate item underscore slot item object dot character display and we want to put it on the weapon transform let's return the transform now we're gonna do the same thing for the shield except when it is a shield slot that we're applying it to we need to do another switch case and this is gonna go over the underscore slot die item object allowed items or a diet type now let's put our cases in and we just need shield and weapon because it would have been easier to type it than doing this I suppose so if your item type is a weapon we're going to instantiate the character display and apply it to your offhand hand transform if it's a shield we'll apply it to the off hand shield transform or wrist transform I mean excellent so that should work the only thing that may not work is the positioning of our items possibly let's go back into unity we also need to actually import the models for those things don't we we don't have a sword okay I'm just gonna import the models real quick I think that you can probably figure out how to import a sword and shield model on your own so I'm just gonna drag it from my other project to make it easier if this videos already long is absolute crap so yeah so we have a shield and we have a sword we just need to apply these to the correct items they belong to let's just find a sword sword one models it's a sword now back into our items find our shield models shield let's click play and pray this works and we don't have to do anything else didn't work at all got a no reference exception we accidentally still had our offhand equals bone combiner at the end of our switch so let's remove that click play and see if this works that's not looking promising for if the shield's gonna work okay that sucks oh we didn't we never applied the transforms to our player so we applied the sword to our sword 1 and the shield to our shield now we need to create the transforms for where they're going to get put at when they are instantiated onto our character so let's open up our characters armature and find his arm let's see your left arm is your shield and we'll use the think we'll use left forearm so right click create empty and this is zeroed out right here let's add in the pre fat or the model for the shield that we added to the game onto here and now the shield is zeroed out completely we want to make our game object make it to where whenever the shield is added at 0 0 0 on to here it's just gonna look correct so we want to rotate the game object and not the shield so let's do that rotate it like this and like this it's that correct let me think yeah he should be holding it correctly like that now let's just move it a bit once you have it in a spot that you think looks good enough we can delete the shield go to our player add this transform as the off hand wrist transform let's click on play and see if that worked excellent it worked but our shields backwards so we'll just riad the shield to here make sure your shield is zeroed out click on the game object that is holding the shield rotate and just to give it a quick 360 Bri delete the shield click on play clipped your shield and there you go you have a shield and you have body armor so you have non boned item equip ting and boned item equip ting I think you can technically add your helmet is a non boned item also we still need to add deleting and only but yeah it uh it's your choice for the helmet that is not the deleting I guess deleting is your choice too let's quickly make the transforms for our sword and the other hand transform so this is wrist transform just duplicate it drag our sword onto here call it hand transform make sure the sword zeroed out and get this looking correctly should be good enough I'm just trying to do this quickly I've already spent quite a bit of time in this video let's see I'm gonna open up OBS and let's see how long it's been it's been two hours and 30 minutes so yeah I'm trying to get this finished let's open up the other right forearm create an empty move it to the hand spot put a sword on it rename it to hand transform z-row your sword out she zeroed out rotate this correctly good enough delete your two swords go to your player drag your transforms in make sure your weapon equipped is working alright well pick one of these up one of these up equipped your shield Clips your weapon save load see it auto added those items let's go pick up another sword oh yeah we don't have deleting added in so this isn't gonna work but you'll see it kind of it so it added that item there so we just need to delete the items now deleting items is easiest part out of everything we just need this exact same switch including the if we get rid of this default actually first get rid of your default now copy the if and we'll we're in on add item so let's go to on or move item paste this in instead of setting the helmet let's just destroy the helmet so destroy the sword destroy the off hand and we don't need to do a check on this one because it's always just the off hand variable being set so we can get rid of the extra switch inside of here destroy the boots and destroy the chest and I did the memory test on this and there's no memory leaks from doing it you're not adding more bones to the game you're only affecting the object that gets added and not the bone structure of your player so just destroying the object works perfectly fine so let's see if we can destroy items now let's just load and try to take something off excellent cool so let's pick up another sword and see what dual wielding looks like let's focus on our player with shift effed and oh we equip the wrong weapon click the one that has a sword on it and there you go we have a dual wielding player that can also equipped items I hope with this video you figured out how this works and how you can make it more performant and maybe some more ideas of how you could make it even more performant one thing I'd like to point out to you is if you go to your armor set body nude go to your rig you have this optimized game object button which will hide all of these transforms and then you can just select which ones to expose so if you're not using everything you're in your hand to bend your hand you can hide those armatures and just use the fingers that your item object requires to bend your hand and it should work fine or if you're not using any legs at all and you're just using chest piece on your character you can only expose the bones that the chest piece needs and it'll help with your game performance a ton so that's just one thing to keep in mind that's there to use so if you wanted to add the tag to here to check for the tag you would literally just say if child compare tag and then type in the tag so like maybe you have a tab called armature and then you can just apply that armature tag to the actual armature and your route character object but I'm not gonna do that but that is how you could do that so I'll show you what I was meaning by the file size difference this is the armor set of our nude character this is the full armor set so for every single piece of armor on the armor set plus the armature it's 1600 kilobytes for your armor set gloves deleted bones so this is a pair of gloves with only the glove bones it's 563 kilobytes it's one-third the size of the entire set and in the set there's helmet chest pants boots and glut there's five pieces so you can see you'd be increasing your overall file size a lot if you separated all these out so maybe think about a way where you don't actually have to separate the items but you could somehow only store the bone name so changing this code to only store the bone names that that item requires instead of storing every boat on it that way when it stores these bone I'm not even looking at the right spot that way when it stores these bone names inside of on validate it'll only store the bone names that it needs to store which will in turn make the bone combiner only need to look up the bone names that got stored so if the code is understandable to use so far maybe that makes sense to you why that would help on performance a lot it wouldn't cut your calls down from 67 to like five while also keeping the file size of having everything with one armature so that's just something to keep in mind little extra information on figure out there on the end yeah also just suggest getting JetBrains to fix all your stupid naming problems like that it also tells you cool things like this this list string bone names can be an I read only list that's kind of cool and then apparently this transform can be an I enumerate an ienumerable which that's kind of cool to write alright I believe that's everything that I need to show you in this video in the next video we'll be doing some refractory to just clean this project up so yeah I hope you enjoyed it if you did leave a comment below consider subscribing to the channel and until next time have a wonderful day and stay coding
Info
Channel: Coding With Unity
Views: 23,002
Rating: undefined out of 5
Keywords: Unity3D model equipping, Item equipping, inventory item equiiping, 3D model attaching, game model equipping, unity3D item system, unity3D equipping system, unity3D bones, unity3D inventory model equipping, unity3D equipment system, equipment system, inventory equipment attaching, how to attach items to player, how to equip armor to character, equip character armor
Id: RESuTNMdqqQ
Channel Id: undefined
Length: 60min 54sec (3654 seconds)
Published: Wed Feb 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.