How to Equip Weapons and Items in Unity (Drag Drop, Inventory)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to create an inventory equip system we're going to have our basic inventory and then drag out items to specific positions in order to equip them let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with enough tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so this is what we want to make over here is my player character and I can move maram as you can see right now he has no harmer no helmet and when I attack I can only use punches and kicks and in here I have the inventory where you can see the various items that I'm carrying and in here on the player equip you can see the various items that I can equip so for example I can click on this helmet drag it and place it on the helmet slot and drop it and there you go the player equip the helmet however if I try to drag an armor into the helmet slot as you can see it doesn't work so it needs a helmet item onto this helmet slot and then you can see that I'm attacking using punches but now let's go and equip this sword and there you go now I'm attacking with a sword so I can drop any item I want in the valid correct position and everything works great so over here we have a very versatile system for equipping items and weapons there are several things that we built previously that we're going to use in order to make this first we're going to use the inventory system to hold our items then we're going to use what we learned in the drag-and-drop video in order to equip our items and the way that you're equipped is based on modifying the texture which I covered any modular spreadsheets video so again here is our character and our equip you I and we can modify our loadout and do anything we want this video is made possible thanks to these awesome supporters go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone alright so this is our goal let's get to it okay so here's our starring scene over here I have a player character and I can move them around as you can see there is no helmet no armor and if I attack I can only attack with punches and kicks then down here we have our inventory as you can see we have a bunch of items so we have an element two swords and two armors this inventory was fully created from scratch in a previous video so go check that out if you haven't seen it already alright so with this let's begin making our equip UI here in the editor let's go into the hierarchy go inside of our UI canvas and in here what make a new game object let's call this our UI character equipment now enzyme let's make some slots okay so here we have a weapon slot the visual is set up exactly the same as the inventory now inside let's make an image for the empty image and in this case this is for the weapons so let's drag the sword icon and now here I have a simple black in white shooter so let's use that one and there you go now it's in black and white and just reduce the Alpha by a little bit okay so here is the black and white children as you can see very simple we just take our texture we sample it we use the saturation node set saturation to zero in order to make it black and white and then we simply take the Alpha and apply it on to it so just like this we have an empty weapon slot now let's make the other two alright so here we have our three equipment slots now let's see how we're going to drop something in here now in here we already have drag and drop working so I can click and drag a certain item and place it in a different position in the inventory this was made in the drag drop video so check that out so I can drag it to a new position and if I drag into an invalid position it simply goes back in there now what we need is in order to capture when I drag an item from the inventory and drop it inside one of our equipment slots so let's make that script down here in our scripts and let's make a new c-sharp script call this our UI character equipment slot and let's drag it into all three of our slots so drag it in there okay now in here first we need to add the event system so using unity engine dot event systems and now we can implement I drop handler so this function is called whenever something is dropped on top of this object okay now we need to check what item is being dragged now the class that handles the atom drag is in here all we're doing is updating the position so that it follows the mouse position and we have some functions in order to show and hide it and set a specific item so we always know which we are dragon so when we drop something in here let's see if we're dragging something so we go into the UI item drag we access the static instance and we get the item that is currently being dragged so over here we now know when an item is dropped onto this slot so for example let's test you would debug log so here we are now let's drag this item drop it in there and if there you go we have our helmet all right so we are correctly identifying when an item gets dragged and dropped into a certain position so now that we know that we need to know when the item is dropped in each specific slot so let's make a script to handle the entire character equipment window so we make a new script come to our UI character equipment and we drag it in to our character equipment window now in here on let's grab references to our slots all right so here we have all three of our references now that we have that we need to know when an item is dropped in a specific slot so for that let's go back into the slot script and in here we're going to use an event that is fired when something is dropped in here so we make a public event and in here when we have something dropped we invoke this let's make an event our X class so we can pass in the item that was dropped all right so we have the event which uses this type of event art so in here we invoke we pass in this as the sender and a new event args and inside we pass in the item that was dropped in here all right so we have this nice event firing now we can go back into our main UI window and in here we can subscribe to those events so for example the weapon slot would subscribe to the on item drop so here we know when an item has been dropped into the weapon slot when you do let's do a a bug download all right let's test okay here we are let's pick up this sword drag it on top of the item weapon and there you go here in our console we can see equip weapon sword one alright so everything is working correctly awesome now with this working we should also update the visual and we should make sure that we keep the UI and the implementation separate so let's make a class to handle the actual character equipment so here let's make a new script call it our character equipment and let's apply this to our player game object so drag it in there okay now here we're going to have the items that we're currently equipping so here this is going to be a very simple class that simply contains what the character is currently equipping and on the UI we're going to display the state of this class so on the UI window let's make a function to receive a character equipment alright we have our character equipment now let's make a function to update our visual and in here we need to grab the currently equipped items so let's go back into our character equipment script and make some getter functions alright so we have three functions to get the weapon helmet and armor and now we can go back into the UI window and now we want to update our visual and for the visual you can use the same UI item that we're using on the inventory okay so here's our script we had the new field for the UI an item prefab we also added a transform for a item container so that's where we're going to be in Senshi ating our UI items and down here on the update visual first we clear all the items that were previously in the container then we get the currently equipped weapon item from our character equipment if it is not known that means we are equipping something so we instantiate our UI item we position it on top of the weapon slot then we get the component of UI atom and we set the item that is currently carrying and we hide our empty image and if we're not carrying a weapon then we simply show the MTU image all right so this should be correct now for this to work we need to call this function so let's make a script that won't set that connection here in the scene I already have a testing script as you can see we're doing the connection between the player and the inventory now let's set a field for the UI script and a field for our character equipment so we set it on our UI all right now fine let's just drag the references so there's our two fields drag the UI character equipment and the character equipment which is on the player and here in the character equipment let's start off with a weapon on our awake any of here we are and as you can see we are currently equipping a weapon alright so far so good now let's actually handle being able to drop a weapon in order to drop we're already capturing that event so all we need to do is set that in our character equipment so in here and let's first get rid of our default and now let's make some setters all right we have some setters so we can go back into the UI and in here when we have our character item drop let's go into the character equipment script and we simply set something this case we set the weapon item to the one that was dropped in here now when we do let's also tell the character what we equipped so for that we can use the character equipment since this script is placed on top of the player and on the player we can simply use a function called set equipment and we passing the weapon item dot item type all right there it is very simple now finally in our UI we need to call this function to update our visual so we could do directly after setting this but a better way is for the character equipment to fire off an event when something changes that way it won't still work if we equip something without using this specific script so in here we just make a new event and we simply fire off whenever we change something all right so now in the UI we can subscribe to this when we receive our character and when that happens all we need to do is come our update their own function alright that should do it so if we drop something inside of our weapon slot we're going to call the character equipment call it set weapon item we pass in the item that was dropped so then this one won't change the internal weapon item it won't tell the player that it modified the equipment and it won't fire off the event then that event is captured by the UI window which then updates the visual correctly showing everything so let's test that ok so here we are and we have no equipment and if I click there you go and just got punches and kicks now let's take this sword and drag it into the weapon and yep there we go we are currently equipping this weapon and if I attack if there you go now I'm attacking with a weapon and now let's try dragging this one and equipping it and there you go now I'm using this weapon alright so just like this we have everything working now one major issue that we have is for example if we drop the helmet into the weapon slot and if we do there you equip the helmet so that is obviously not supposed to happen so let's validate our item type when trying to drop something so let's make that here in the character equipment script let's make a function to try to equip something so a totally boy try equip item and we also need to know where we're trying to equip something so let's define a and noon so if phone we can don't call it the equip slot so you have the helmet armor and weapon so down here on this function you will receive the equip slot and then we receive an item that we want to equip and now here we're going to test the item type based on the equipment so here we do a switch on the equip slot and do switches on the atom type or we can also make that on the item class itself so let's put it in here in order to make our code easier to follow so here we have a bunch of functions for each item so we get sprite color and so on and now in here let's make a public to and return a character equipment dot equip slot and you can't get equip select all right so we have a function which the switch on our item type and returns a matching equip slot so here we can simply test if the item equips not matches this equivalent if so then we can equip it alright this should be working now we need to make sure that we work with this function instead of all of these so let's make these private keep our code nice and clean ok that should do it so now we're going to use this function let's go into the UI character equipment and up here where we have our drop instead of using that one directly let's use the try equip item and now this is the comeback on the weapons want some lights passing the equipment slot for the weapon and then the drop item let's subscribe to the others as well alright so just like this let's see if we can now equip things and we can only equip them on the actual correct slot okay here we are again just punches now let's drag the weapon on tv' weapon slot and there you go equip the weapon yep just like that now if I try to drag the helmet into the weapon slot and there you go nothing changes and we're still doing that now try to drag the helmet into the helmet slot and yep he did equip the helmet we just forgot to update the visual and the same thing with the armor equip the Harmon there and yeah nothing changes now put in there and there you go he equipped it so let's fix the visual here the fixes down here when we are updating Divisional right now we're only testing for weapon item let's test the other two all right so now we have the case for updating our weapon armor and helmets visuals now obviously we got a lot of code duplication here so after we check that this is working we should really clean this up but let's first test ok here we are and let's equip the sword there you go there's a sword equip the helmet and if there's the helmet and equip the armor and there you go there's our armor alright so just like this we have our working in character equip window now just for fun let's have a chem review in our UI I've covered how to do a portrait camera in another video so check that out for an in-depth look in here I'm just going to add it very simply all right so here we have the camera view on to you I only have is a raw image and then a camera on the player and you up here we have our character and our UI showing our character so we could have the main camera very far away and we'd still be able to see the player when we're equipping items so let's equip a sword and there you go now we have our sword and equip this armor and this helmet and there you go there's our player now equip this armor equip this sword and so on and there yo everything all x squared all right so here we have our system fully working we can drag items from our inventory into their slots if we drag an incorrect item then we cannot equip but if we drag the correct one then we can indeed equipped and when we equip an item we can see the atoms change in our player visual this video is made possible thanks to these awesome supporters go to patreon.com/scishow new code monkey to get some perks and help keep the videos free for everyone as always you can download the project files in a tony's frumenty code monkey comm subscribe the channel for more unity tutorials post any questions I have in the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 85,538
Rating: undefined out of 5
Keywords: unity equipment system, unity equip weapon, unity equipment, unity equip items, unity inventory system, unity inventory, unity drag and drop 2d, unity drag and drop, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming
Id: isAmoM3RPEI
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Fri Jan 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.