INTEGRATION 02 - Making an RPG in Unity (E08)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this second integration episode we're going to be extending brackets equipment system to support the clothing and armor meshes that were created in the previous two graphics videos now as before there is a download available for anyone who hasn't been following along with the graphics playlist so here I've just got the new player blend file and some icons now we need to be a little bit careful about how we import the player a blend file because of course we've already got this player file in the characters folder and if we just delete this and drag the new one in here then we're going to lose all of our settings so instead what we'll do is navigate over to where this is stored on the hard drive and will delete it there and without going back to unity yet we can drag in the new player blend file and make sure that that has the same name as the old one so that when we go back to unity it will just refresh that and we'll retain all of our settings and links to the object in the scene so that's very nice we'll then also want to import the icons so I'll just I simply drag those into the sprites folder here and we'll quickly select these and just change the texture type to sprite and apply that okay let's zoom in on the character here so you can see is wearing his armor now and it's maybe looking a little bit sort of plasticky at the moment so quickly going to the characters materials folder here just select all of these and phrenologist turn the smoothness down to zero okay let's fold out the player graphic so we can see all of the different parts making him up the body hair helmet pants and so on and let's go into the items folder we will create a new folder here to store the item meshes and inside of there I'm going to have the hair the helmet the pants plate body plate legs shirt and shoe all right and then I will delete all of those from the player object now we want to be able to associate each of these meshes with a particular item object so let's go into this equipment script and somewhere near the top here I'm just going to create a public skinned mesh renderer variable called the mesh and we can save that so each of these is of course a skinned mesh renderer so now if we go into one of these items say this helmet of fire which I'm going to rename rather bland liturgist helmet then we can go into the meshes and drag the helmet mesh into the mesh slot we just created I'll also quickly fill out the rest of this so the name is just gonna be helmet now I'll change the icon to the helmet icon we imported and I think the rest of that is correct let's quickly go through and do the other two pieces of armor so I'll call this the plate legs and just rename that up here as well set the icon to the plate legs icon the equip slot to the legs and of course set the mesh as well and then this last one here will become the plate body I once again rename that up here change the slot to chest should also set the icon let's over here and finally assign the mesh okay now these three items have been assigned to this little pickup objects in the scene and at the risk of being terribly boring I quickly want to go through and rename these as well so this is the plate body pickup this is the plate legs pickup and this last one is the helmet pickup all right and I'm little organized this all under a empty object called test pickups okay so I'll grab these three and parent them to that object all right with this groundwork out of the way let's go to the game manager and just open up the heat equipment manager script and in here we have this equipment array keeping track of all of the currently equipped items but we're also going to need an array of skinned mesh renderers to keep track of the actual meshes that we're going to be spawning into the scene so I'll create skinned mesh renderer array called the current meshes and we can initialize that down here current meshes is equal to a new skinned mesh renderer array with a size of numb slots all right and then I'm also going to create a public skinned mesh renderer variable called the target mesh and you'll see the purpose of that in just a moment coming down to the equipment at the bottom here I want to instantiate the new equipment mesh so let's create a skinned mesh renderer variable called the new mesh and I'll set this equal to instantiate of type skinned mesh renderer passing in new item dot mesh all right so it's now all instantiated the new mesh into the game world so let's now set its parent object with new mesh transformer parent to the target mesh dot transform so the target mesh is going to refer to the player mesh and we'll be using that now to tell the new mesh how it should deform so we want the new mesh to deform that based on the bones of the target mesh so we'll say new mesh dot bones is equal to the target mesh bones and we'll also say a new mesh dot root bone is equal to the root bone of the target mesh now we'll of course want to insert this new mesh into our current meshes array so we can say current meshes with an index of the current slot index is equal to the new mesh okay so now to handle unequipping once we have checked that the current equipment at the specified slot index isn't null let's just to be extra sure check that the current mesh at that slot index is also not null then we can go ahead to end destroy the mesh by writing and destroy current meshes with an index of slot index I don't want to get the game object associated with that mesh all right let's save that and go into unity and from this just remember on the game manager to assign the target mesh here so that will be the body mesh of the player we can then try playing this so I'm going to quickly right click on these three items to pick them up and if I press I to bring up the inventory got our nice little icons displayed yeah I'll click on each of these and those are equipped you can see that there are some intersections with the body there but we'll be using blend shapes to fix those in just a moment but I can now get my character to run around and the armor is deforming with the animations as we'd expect so that's all good let's not try and sort out those intersections so if we go into the body here you can see that under blend shapes we've got these three shapes at the moment legs arms and torso and these are values from 0 to 100 if I bump each of these up to a hundred you can see we get this much skinnier version of the character and that will help prevent the intersections with the armor so we want to change these values dynamically based on what the character is wearing so let me just set these all back to zero and I'm going to go back into the equipment script and just below this equipment slot enum I'm going to create a new enum called equipment mesh region and I want these entries to correspond directly to the blend shapes so I'll have first legs then arms and then the torso and let me just make a note here corresponds to body blend shapes all right now each piece of equipment is going to have a public equipment mesh region array called the covered mesh regions so if we go on for example to the helmet this is going to have none of those mesh regions covered since it doesn't cover the legs the arms or the torso but the plate body for example it's going to cover two of those regions it covers the arms and it covers the torso and the plate legs covers just one of those regions that being the legs now as one creates sort of more varieties of armor which might cover different regions of the body we can of course add in more of these mesh regions that we can more accurately define which parts of the body are covered but for now we should be able to get by on just these simple three anyway we still need to get this to actually control the blend shapes here so let's go into the equipment manager script and somewhere down at the bottom here I'm going to create a method called set equipment blend shapes and taken a piece of equipment called the item and an integer for the weight and then we're going to loop through each of the regions that that piece of equipment covers so you can say for each equipment mesh region called blend shape in item dot covered mesh regions for each of those or we'll want to set the target meshes blend shape weight with this set blend shape weight method passing in the index of that blend shape which we can get just by casting blend shape to an integer because remember we specifically wrote these in the same order as the blend shapes so carcinogen integer we'll get the index and then we pass in the weight that we've been given so when an item is unequipped we want to set the weight of all of the areas that that item used to cover to zero so we can say set equipment blend shapes passing in the old item and a weight of zero and of course when an item is equipped we'll want to say set equipment blend shapes passing in the new item with a weight of a hundred all right so let's save that and run the game and hopefully this time when I pick up these items and equip them we will no longer see any intersections with the body since those blend shapes have come into effect ok so now that's working quite nicely I'd like to make it so that at the start of the game the player equips is default outfit and that outfit is made up of his hair piece the pants shirt and shoes so we'll want to create a new inventory equipment I'll call this hair and let me write the name here yeah now this doesn't need an icon because it's going to be a default item which just means that it will automatically be worn when nothing else in the same slot is equipped so it will never actually show up in the inventory so let me then just add the mesh so hair mesh over there and I now want to go through and create the items for the shirt pants and shoes as well but I will just speed the video up while I do this because it's not particularly enthralling all right so I've got these default items set up now the pants shirt shoes and hair so I'm going to go into the equipment manager and up at the top here I'm going to have a public equipment array called the default items and somewhere at the bottom I'll have a method called equip default items and yet we'll just loop through for each equipment in the default items array call the equipment on that item okay so that will be called from the start method we can just say equip default items all right and perhaps you'll want to call that method again at the end of the unequip all method so here just equip default items so that once it's removed everything down to the bare skin then we layer the default items back on top of that all right now there is one small change that I want to make to how the equipping works so I think it makes sense that when you equip a new item it starts out by unequipping the previous item so over here I want to call the unequip method passing in the slot index now one benefit of doing this is that we don't need to worry about adding the old item back to the inventory over here since that's all handled inside the unequipped method but what we do need to worry about is that we need a reference to the old item that's been unequipped so that we can pass that into this on equipment changed event so we're getting you to make the unequip method return the item that's unequipped so I'll change this from a void to a equipment and at the end here it will return the old item and of if the old action was null then it will just have to return null at the antia so then we can say the old item is equal to whatever is returned from the unequipped method and then we can I just pass that in over there all right so let's save that go into unity and I'll go on to the game manager and I just want to add in all my default items over here so I'll lock the inspector and I'm just gonna select all of these and drag them into the array there and then I can unlock the inspector and let's give this a try so all of the default clothing is there and I'll quickly pick up these items let's run across the bridge and I'll try equipping these and it replaces the default clothing nicely and then if I press you to unequip those get added back there and we're back to wearing our default clothing and I can equip those once again and it all seems to work nicely all right so our models are now nicely integrated with the equipment system the only thing left that I want to do this episode is to replace these little cubes with the actual models of the objects that are being picked up so the plate legs plate body and helmet models now this isn't as straightforward as it might seem because these skinned mesh renderers are a little bit tricky to handle in the scene you can see if I add this here we can actually see the mesh at all so I'd rather replace the skinned mesh renderer with the normal combination of mesh renderer and mesh filter that we use for non animated objects we will of course be keeping the skinned mesh renderer versions for the actual equipped items but just for the preview items that might be lying around the game world I'd like to not use the skinned mesh renderer so we could do this by hand but if we're going to end up with lots of different a piece of equipment I think it's better to make a script for that so in the scripts folder I'm going to create a folder for my helper scripts and in here go to create a new C sharp script and I'll just call this something like convert to regular mesh okay go to open this up and this is going to have a method called convert and she will start by getting the skinned mesh renderer component so I'll just call this the skinned mesh renderer is equal to get component of type skinned mesh renderer alright will then want to add a regular mesh filter and mesh renderer component so it's a mesh renderer mesh renderer is equal to we have to say game object dot add component of type mesh renderer and then the same thing here for the mesh filter so call that mesh filter is equal to game object or add component of type mesh filter we can then set the mesh filters shared mesh equal to the skinned mesh renderers shared mesh and the mesh renderers shared materials to the skinned mesh renderers shared materials having done that we now want to delete the skinned mesh renderer since it's been replaced and by these two separate components so to remove a component from the editor we'll need to say destroy immediate and pass in the skinned mesh renderer and once it's done we might as well delete this script as well so we can just say destroy immediate this now we need some way to actually run this method so let's just add a little context menu attribute call this convert to regular mesh-like so I'll save that and then let me go into the meshes folder and I'll drag into the scene the helmet the plate body and the plate legs and to each of these I'm going to add a convert to regular mesh component and we can click this little gear icon and just ask it to convert to regular mesh this is that context menu that we created so I'll press that and now we've got these regular meshes around here that we can move and rotate in the normal way so in the prefabs folder I'm just gonna create a another folder here called let's say equipment previews and I have to do this one by one I think just drag this in there the helmet the plate body and the plate legs and then we can delete each of these all right so now we can go into these little yellow boxes that we've been using as our placeholder graphics and let me just set the scale of these up to one on all axes and I'm going to remove the mesh renderer and mesh filter components from each of these I also just turn down the radius little bit of the Eischen pickup script and then I'm going to add the appropriate model to each of these the helmet the plate body and the plate legs and I'll select each of those and just reset the transform so they're all lying over there and let's maybe just rotate these a little bit just to add some variety I'll move this one out over here and we also want to match the box colliders to roughly the shape of the object so something like that and on the helmet seems to be a little bit smaller on all axes okay and lastly the plate body pick up something like that looks okay so let's run this I'll grab the helmet the plate legs the plate body and open the inventory equip those and it looks like everything is working well so that is going to be everything for this episode until next time Cheers
Info
Channel: Sebastian Lague
Views: 157,231
Rating: undefined out of 5
Keywords: RPG, role-playing game, Unity, gamedev, equipment, armour, clothing, customization, blendshapes, shapekeys
Id: ZBLvKR2E62Q
Channel Id: undefined
Length: 22min 54sec (1374 seconds)
Published: Wed Sep 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.