Unity - Open Door, with Safe Code.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to my channel and today i will show you how to make an open door script with a keypad so you have to type in the correct code to open the so how that looks is if i walk over to the door i made here hit the e button a little panel will open up and if you press some numbers here and you press the wrong one it will just disappear and if you hit the correct one like in my case eight five two the panel will go away and the door will open and you can enter the door yeah that's what i will show today let's get to it so the first thing that we'll need to do here is to create the door so let's create a cube let's scale it a bit so it will look more like a door let's put it up here do something like this this looks like it all pull it up a bit like this let's name it the door make an empty object call this the door parent put the door underneath that one and put it at zero zero zero move it up a bit like that again underneath the dolph parent make a great empty game object and call this the hinge this is the all we can all call the the rotate point move it to the right of your door somewhere take the door and make it a child of that rotate point so when you rotate it now you can see it will rotate around that one cool now let's on the door parent here let's add let's add an animator let's add tab oh sorry what happened here let's add a tab here i do not have the animation here let's create an animation on the door parent and just call this open door anim and here we can take that rotate point remember to hit the record button go to something frame 20 and take the y axis and rotate it 90 degrees so now it will do like this head back to your project um and if you go to your door now you can see you have the door here so let's open up the animator if you don't have it we can go down to animation here the animator and drag it down here so right now if you see here it's the default one right click create empty and just call this uh i don't know idle state or closed all so and right click and may set it as default right click make transition down to the open door in the parameters here let's make a trigger and call it open door now hit the arrow here again in the condition let's say open door it doesn't have exit time and the duration set it to zero so now when this is triggered the door will open oh remember to to take your animation and remove the loop yeah now let's get it to work let's make a new script and call this open door put it on the door parent let's open it and the first thing that we'll need here is a reference to our animator so let's say private animator call it anim and in the start function we need to say anim equal to get component animator so now we have a reference for that so what do we need to do now let's make the panel yeah let's close this down head back to unity you'll of course need to make yeah right click ui image uh let's hit 2d here and this one will be our keypad so let's put it up here in the middle somewhere the keypad we will need to have um let's make it a ui text here let's make it black this text will be the um uh the ticket number it's called that and that is oh it's actually the it's the code let's make it 24 put it in here [Music] it will be we should actually just make it maybe 14 like this yeah let's center it both ways like this put it up here then we will need to make some buttons and here we can simply make an empty one and call this buttons and underneath this one will make a ui text miss pro button and let's make this 30 by 30 maybe that's fine and the text for this one just type one okay it's maybe it's too big let's make it 20 by 20 and make this 12 something um what's happening here so and what the reason why i did this because on the button here we then can add a grid layer group and the constrain let's uh fix column count and we will have three in my case so if you take this button and let's first make it another cup color and you can see the button size is what's 20 by 20 yeah uh let's make the color on this one oh this one could be something like this it was more the text that i was thinking about let's make it wide so here on the padding let's now to the left something like this and from the top put it down here now duplicate this one like this let's move it a bit more to the right like this good and of course we'll need to fix all these so it says two three four five six seven eight nine like this and then we should take and open up the script again let's save this and head back to the script and here we will need to say private text mess oh remember to say using tm pro and then we'll need to say text message pro gri and this will be our code text and this we will serialize so it can be seen in the inspector then we will need a string for our code text value this is equal to empty here in the from the get-go and then we need a public string that will be our safe code to enter the dawn and then we'll need a public game object and this will be the code panel and then of course we'll need a ball as well so we will need a private ball and and i just called it is at door and from the get go from the get-go it will be false yes so in the update now let's say fast on trigger enter so when we enter the or go to the door we will need to say if other dot dot tag is equal to our player so when we go to the door we will have to say this at door is equal to true and then we need to do the same for the pr void on trigger exit here we'll just say is at daw is equal to false and our code panel that we are gonna open in the update function we will need to code panel is equal to oh sorry dot set active to false here so we we will open up a panel when we go to the door and when we go away from the door this pedal will be set false and then we also need a public void add oh sorry add tickets and this will take in a string and we can call it take it for the num all numbers that's up to you and the code text value plus equal the ticket or the number uh yeah let's set this up and then save it and go back to unity and let's set it up what we have here so if we take the the door here right now we need the code text so if you go to the keypad that is the ticket number here then we need an the code to get out so we can just say let's say 852 and then we need the code panel and that is the keypad here yes so let's open up the door script again and let's do the last part and that is of course here in the update and here we need the code text dot text is equal to our code text value and then we need to say if code text value is equal to the save code that we have typed in then our anim dot set trigger is equal to open door and our code panel so if the door opens then the code panel of course need to be set to false as well and then i have made this if my staff if statements that if the code text value is greater or equal to 4 oh sorry dot length then the code text value is equal to nothing so this you you do not need need this one uh if if what this does is that when you type in some not some of these numbers that if you hit hit four and it's not the correct save code then it will delete all the numbers and uh yeah you have to start over that's what this does and yeah then of course we need to open up the panel of as well so if input dot get key key code dot i will choose e and our is at door is equal to true and this one is set true when we go into the trigger on the door then the code panel dot set active is true not tree true like this so let's save this and head back to unity and let's see what happens so do we need to set anything else up here nope the only thing that we need to set up is at the door on the door we need a box collider and this need to be a trigger and let's do like this this is uh if the player hits this one then this panel will open we should not be so far away so let's do and we should not open it from the other side because it's not closing in this case yes and the other thing that you will need to i only have a simple player in here with a player movement so i can move around in the game and remember to check your player as a player the keypad let's hide this and now let's hit the play button and see what happens so as you see here we can walk around when we go to the daw and hit the e button it opens up and we should be uh we forgot to set up the buttons course so on the keypad here we have all our buttons let's close all these again an easy way to do it is if you take all your buttons here on the on click click here take the door parent move it down here on the function take the open door and here at digit as a string and then when you need to set it to one two [Music] three four five six seven eight and nine and if you wanna use letters instead you can simply just write them in here instead and of course change the save save key yeah two letters as well but now when we hit the play button it should work let's see so now we can walk around we can walk to the dawn open this one on e you can press some numbers here and if we take four numbers it's the wrong one everything disappears and you can try again and if you hit the correct number uh was it not two five eight nope that was the correct one right oh i typed it wrong i see if i check my console here the para parameters does not work because i said open door it's open door with two it looks like this silly mistake you need to remember that text need to be 100 percent the same as this one you typed here else it will not work so let's maximum maximize on planes that hit the play button here go to the door hit e let's say eight five two it opens the door and it disappears and you can enter the door yeah that was uh what i had to show to show you in this tutorial um if you have any uh comments please let me know in the comment section below and if you want to see more videos from me please hit the like and subscribe button thanks a lot for watching
Info
Channel: GAW
Views: 3,948
Rating: undefined out of 5
Keywords: Unity Open Door, unity, Unity3D, Unity2D, Unity, unity3d, unity2d, unity 2d game tutorial, unity health, unity 2d heart, unity healthbar, Open/close, Door, Open, Close, Unity3D open/close, Open Key, open door with safe code, unity open door with key press, unity open door with button, unity open door with key, unity open door trigger, unity open door animation, unity open door script, unity open door with raycast, unity door open close animation, unity vr open door, unity 3d door open
Id: dVUIg8A71RE
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Sat Feb 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.