Simple Key Door System in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to make a key door system we're going to create keys of various colors and make sure a specific key only opens a specific door the player won't carry the keys and opening doors 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 nf2 torrents made by a professional indie game developer so if you find the video helpful consider subscribing ok so here we're going to make a very nice key door system one example of this system in use is in the FPS game I made recently if you haven't played it yet go check out the link in the description it's fully playable in your browser and nice and short the game contains several colored keys and several colored doors so just like we have right here so we're here I have my three nice doors so the bull door red door and the green door and the corresponding three nice keys and if I try to go through door yep there you go as you can see I cannot open it so I can go into the key and as soon as I collide with it there you go I've picked up the key and you can see that the UI updated so the key is currently in my key holder and now if I try to go through the green door there you I cannot open because I have the red king but if I go into the red door there you go the key is consumed the door opens and I can go through and over here I have my nice trophy and there you go I've won this mini game alright great also this really cool Alpine cheater who is made in we're half in another video so go check that out see how it works and then all of the other keys were the same so the green key there you go open the green door and the flue key and there you go opening up the nice bull door so here we have our system also in this video we're going to create a custom key holder in order to hold our keys but you could very easily adapt this code to work with the inventory system we created previously so that way you don't have a container just for the keys but we rather store them in the inventory and like any other object so again here it is pick up the key open door go through the door and window level alright so this is our goal let's get to it okay here I am in our starting scene all I have is a player character that I can move around now let's begin by making the key object so here is the scene and on the hierarchy let's right-click to create an empty game object let's call this the key red let's add a sprite renderer drag the key texture and let's send it in red alright there's the key now let's add a circle Collider and we make it a trigger all right so here we our red key set up to handle collisions and now over here we have the player game object and as you can see he has a rigidbody 2d and also a box Collider 2d so this is what we need in order to handle collisions on both sides we need two colliders and at least one rigid body so let's make our key script so we make any C sharp script call this our key and drag it into our key game object so just like that okay now in here well its first to find the various key types using an enone so we make a public you know call this our key type and this will be our various key so let's say red green and blue okay and now we want to set the heeta for each key so let's simply add a serialized film for our key type key time and now we can go into the editor and yep there's our key script and we have a nice drop-down menu in order to choose our key so this one is red okay so far so good now that we have our keys let's create a script to hone them so let's make a new script and we're going to call this one the key folder and this one won't go on the player so we're on the player and dragged the script okay so here we want to hold a bunch of keys so the way we're going to do that let's make a list a list of type key key type and this will be our key list okay so we have a list of keys and we instantiate it on awake now let's make some functions to work with this list all right so here we have some functions we can add a key we can remove a key and test if this key list contains a certain key now the way we're going to interact with our keys is with a trigger so on our key holder let's add a private void on trigger enter to D again here on the key red we made a circle Collider and we checked is trigger so that's why we're using ontriggerenter so this gets fired whenever the collider enters another collider you know when we do let's try to see if we collided with a key so we test if the collider not get component of tight key if it is not known so if the key component is not known then we have collided with the key and now we want to add it into our list so we do add key now we're going to pass in the key type so we need to go into the key and make a function to expose it we're going to call it get key type so let's make this inside of our key okay so in here we just make this okay there it is very simple and now here we have the key from that king and after doing it we want to destroy the key so we just call destroy the key that game object all right there it is very simple so ontriggerenter we check if we can live it with something that contains a key component and if so then we add the key onto our key holder and we destroy the key game object now let's just add a debug log in here so you should be able to see our log saying that we've added our King let's test okay here I am and you can see the key down there and as I move towards it yep there we go the key was destroyed and over here on the console yep edit key right right awesome so now that we can capture keys let's handle using them so first let's construct the simplest door possible so make an empty game object call this our dorm now inside let's have the door sprites okay so here the texture I'm using is just of one side so we have a sprite for the on left side and a sprite for the right side now that we have the visuals let's say the colliders so we want to have one Collider to stop the player so working as a wall and another one to test when the player is near so first on the door game object itself let's add a box Collider and for this one let's make it a trigger so being trigger means that it will not stop anything related to physics so we're going to use this one to identify when the player is near the door so let's make it quite a bit bigger okay so just like that we're going to identify when the player enters this area you know inside let's make another game object this one we call our Collider and in this one we also had a boxing either except this one is not a trigger so this is the one that won't stop the movement of the player and let's make it match the door size just like that okay so over here we have our door split on the left and right and we have our main game object now let's just rename this to the door red since I'm later we're going to deal with different colors and inside let's just end this in red just to see okay so we have our door set up and over here on the side as you can see there's also a bunch of walls which only have is a sprite renderer and a box Collider so if we test this out okay so here I am and I can go inside the trigger area no problem okay great but I cannot go through the wall and I cannot go to the dark alright so far so good now let's make our door script so a new script this one we're going to call our door drag it into our door okay and now here we're going to do similar to what we did to grab the King so on the key holder we can use the same ontriggerenter and we're going to test if we collide with something with the key door but before we do that let's first add the key that relates to this door so let's simply add a surmise field for our key type okay we just have a film for our key type and a function to return it and now here in the editor we see our door with our key type and yep this one is red okay good so now we can go into our key holder and in here we're doing it on trigger trying to capture the key okay if it is not the key then we try to capture the key door script so if we did collided with something test if it has a key door and if it does then here we want to test if the key holder currently contains the key that is used to open that door so you go into the key door in order to get the king that is used to open it okay so we have the key type and we can use this function to test if we contain that key so if we contain this key then we are currently holding the key that we need to open the store so we're going to make a function on our door called open door so let's make that over here on the key door very simple just void call it open door and here just for testing let's do the simplest thing so we just set the game object to active as bolts so we're going to hide the door when you open it okay and on the key holder after we open the door it's also consumed so we remove the key of this key type this is obviously optional it depends on your design whether you want to consume keys or not but in this case and let's do it like this all right so just like this we have pretty much entire thing working so on the key holder which is placed on our player we have our trigger enter and if we collide with the key we grab the key and if we come right through the door we test if the door is opened with the a key that we contain and if so then we remove the key and we open the door let's see that okay here we are the doors closed and if I go inside the trigger nothing happens since I do not have the key but if I go down here yep there you go I've picked up the key and now I move towards it and as soon as I enter the trigger area there you go the key opens the door I know here's my nice star and I've captured it and I've won this mini a right awesome so here our system is pretty much fully working we have a door that only opens if we have the key and here just for fun I had this nice outline effect and if you want to see how that was May then check the link in the description it was fully created in she so now with this working let's add a new type of key and a new door okay so I have had the green key and a green door they working exactly the same the only difference is over here on the infield so you can see this key door contains a key type of green and again on the key green we have a key type of green all right so just like this let's try it out okay so here I am and I cannot open the red door neither can I open the green door okay I can't open any of them since I don't have any keys now let's pick up the green key okay I've picked it up now let's try to open the red door and nope I cannot open it but if I go into the green yeah there you go I can open the green door and if I pick up the red key any up now I can open the red door and when our minigame all right awesome so just like this we can easily support multiple key and door types now let's try replacing these doors with something more animated okay so I've replaced the basic doors with some nicer ones with some nice animations as well as a key so if I try to open there we go we have a nice animation saying we cannot open the door and same thing on this one so I cannot open any of them now let's pick up the green key and I'll try to open the red door and yep I still can't open it now try to open the green door and there you it opens perfectly and I can go through so same thing pick up the red door and yep go through and awesome and I've won again alright great so just like this it's looking really nice okay so back in the editor we can see how it works here we have the door and the key prefabs and here in the door you can see that it has an animator then the same box now either as previously working as a trigger then we have our key door with our red key and we have a simple script to handle the animator inside it still has the same thing so it's played on the left and on the right with next from one just for the animations and then for the key again the same thing just a simple animator playing an animation so we have our sprite than our shadow and again here is looking great so pick it up open it and you through and yep awesome alright so now one final thing let's add a UI element so here in the editor let's make our UI object so go inside the canvas and let's make an empty game object we're going to call this the UI key holder now inside let's make a container game object this is where we're going to place our key images so let's make a template for a key image okay so here it is very simple we just have a image now let's make our script to handle this so we make a new script going to call the same thing so you wanna keyholder and we drag it on to our game object okay now here's the first thing we need is grab the reference to the container and the template okay we grab our references and we hide the template now in order to set up our visuals we also need to have a reference to the keyholder that we want to represent so let's make a sterilized film - in order to add it okay there it is now we can go into the editor and here we can simply drag the key holder that is placed on the player okay there it is now here we need a function to update the visual so in order to update it we need to go into the key holder in order to figure out which keys is currently holding so let's go into the key holder and we need to make a function to expose our key list okay we have this function and now we can cycle through it now instantiate our template now we position it correctly now we need to set the correct color based on the key type all right so that's about it here on our UI key holder we have a reference to the key holder and then we have a function that is going to update the visual so we just cycle through all the keys that we are currently holding we duplicate our template and we set the correct key cone so let's test this and here we are and let's pick up the red key and yep I picked it up but nope nothing happened the reason for that is because we're not calling our update function anywhere so here the function works but it's not being called at all so let's use an event inside of our key holder to fire when something changed so here in our key holder so we have an event called Anki change and whenever something changes let's invoke this event alright that's it we just fired when we add a key and when we remove a key so now we can go back into the UI and let's make a private void start and on start let's go into the key holder in order to subscribe to the on keys changed event and now here we can update our visual and one final thing on the update we are in sin cheating but we are not cleaning it up so let's do that quickly okay so on the update visual we clean up the old keys so we just cycle through the container and we destroy every key in there that is not the template and then we instantiate based on the current key holder key list so just like this everything should be working let's see okay so here we are and there's nothing on the UI and I cannot open this door nor this door okay now let's go and grab the red key so as I pick it up yep there you go the UI now shows that I have the red key so if I try to open the green there's more put the red yep there you go it opens and the key as he saw was correctly consumed no grab the green there days on the UI now use it anywhere you go the key was consumed and I can go in and win our minigame all right awesome so here we have our entire system working and now just for fun let's add a blue door okay so here we have the entire system working with our three nice door and key types I've added the blue door just like we did for the green door so we can easily add a lot more types by just adding another valid in the you know both the key and the door use the enum value to figure out if they should open the doors have two compilers so there's a trigger compiler to figure out where the player is if the player is close enough and there's a second compiler which works as a won't so just like this they're all closed and nope as you can see a nice animation because I cannot open it then the keys also have a coiler and if I go onto it yep there you go I've picked up the red key and you can see it on the UI and if I go and try to open the green door nope I can't do it open the pool door nope but the red one any of there you go I can open it so I can now go through and over here I have my nice trophy and pick it up and there you go awesome so now I can try to open the green key with the green door and try to open the blue key open the bold or any of there you go we have our nice three doors nicely open all right awesome now here we created a custom class to hold our keys but you could very easily adapt this code to work with the inventory system that we created previously that way you don't have a container just where the keys would rather store them in the inventory unlike any other object and also here we built this in 2d but this is the exact same system I used in the FPS micro game that works in 3d the system there also has keys doors a key holder and a UI script so everything works exactly the same so if you haven't seen it yet go watch that video and play that game it's very quick and fully playable in your browser so here we have our nice key door system for working and open great as always you can download the project files and utilities from unity cosmic comm subscribe the channel for more unity tutorials post any questions you have in the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 44,996
Rating: undefined out of 5
Keywords: unity key and door, unity key door, unity door, key door, unity key inventory, unity open door with key, unity door open tutorial, unity door animation, unity door tutorial, unity key, 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, programming, coding, c#, code, software development, learn to code, learn programming
Id: MIt0PJHMN5Y
Channel Id: undefined
Length: 19min 9sec (1149 seconds)
Published: Thu Nov 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.