How To Open A Locked Door With A Key - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another version 4 tutorial in today's video i'm going to show you how to use a locked door so in this the door will be locked by default so you won't be able to open it you pick up a key once you've picked it up you're going to be able to open the door again so this is something which is quite commonly used in horror games and other stuff like that so essentially you can't open the door you pick up a key and then you can so let me show you what this is going to look like now so i go in i try to open the door we can't open it and you get that sound effect there to signify that the door is locked and then when we pick up this key here we have a sound effect for picking it up and then we can open this door like so so this is what we're gonna be making today that's quite a nice little concept and it works really well so let me delete this code and i'll show you how i've done this so the first step we want to make is we want to obviously import these models so i've got a key model and two sound effects and i'm leaving a link to these in the description down below so we've got the key pickup the key model but it's very small so you probably won't have to see it in here nope we'll need to open it up later on so i'll show you that when we use it and then the locked door like so and so after this we want to open up our character blueprint so for me this is the third person character so that's content third person bp blueprints that person character but for you this could be third first or if you've named it and all we want to do in here is we just want to create a variable so in the bottom left down here we're going to hit the plus variable there and i'm just going to call this has key question mark like that compile leaving it as a boolean there and leaving the default value as false and that is all we need to do in here so we close that straight away and we're just gonna be using that to tell the player in the code whether or not we have picked up the key or not so once you've done that we're gonna go back to our folder where we're making the door and the key so for me that's just simply content and locked door this can be anywhere that you want it in here we're going to make the door first so we're going to right click go to blueprint class get an actor i'm going to simply call this one locked door bp you can name this whatever you like i'm going to open it up straight away in here we're going to add a component i'm going to add a static mesh like so this is just going to be the door and i'm going to be using the door starting mesh in the starter content which is sm door there like so and then one other thing we're going to add in the viewport it'll deselect that add a component if you want to add a box collision and this box cushion is where the player has to be in order to open the door so i'm just going to put that in the middle and scale this up a bit so again once the plate is in this area they can open and close the door so get this to be as big or as small as you want but i think that is a good size for me once we've done that we're going to compile and go straight up to the event graph and we can delete these three notes here what we want to do in here is we want to right click on our box collision in the top left add event add-on component begin overlap right click it again add event and a component end overlap and so this is what's going to be telling us whether or not the player is inside or outside this box collision and to see if it's the player what we're going to do is out of other actor we're going to cast to our character for me that is the third person character like so and you can do that on both of the other actors for each event so again this for you this could be third first or what if you've named it after this we also want to right click and get player controller like that just putting that in between those two there i'm going to right click again i'm going to get enable input target itself player controller is a return value there plugging that into the top cast off of the begin overlap we're going to right click and get disable input like so plugging into the bottom cast of the end overlap again target itself player controllers get player controller and what this is doing is this is allowing us to use the e key or whatever key you want to actually be able to open the door if we don't enable the input we won't be able to open the door and we're disabling it so that whenever we press e even if we're not close enough it isn't flying off this code to try and open the door so it just makes it more effective and it can also stop other things from breaking later on as well so then what we want to do above this is actually press e so we could just get an e keyboard event but to make this slightly more efficient we can go to edit project settings once it slides we're going to go down to input in the bottom left here and we're going to create an action mapping now you see i already have it that's just from previous tutorial so what you're going to want to do is hit the plus action mapping there name this whatever you like so i've made mine interact and you can change this from non to the e key or the f key or left mouse button literally any button you want this is just a button the player press to interact once you've done that you just close that straight away right click and search for it so my name's mine interact you can see we have interact action event there so we have input action interact so whenever the player presses that button this will fire off off of pressed so when the player presses the button we're going to hold down b left click to get a branch like that plugging in there the condition of this is we want to see if the player has the key so to figure that out we're going to come off as third person character for this cast here and we're going to get has key that boolean we made earlier put that above there and plug that into the condition of the branch like so so what's going to do is if the player is close enough to the door and they press e and they have the key then we're going to open the door which we'll do here so if that's true we're going to go into a gate so hold down g and left click to get a gate with the enter going into true there the open we're going to enable input the close going to disable input so again if we are close enough to the door and we have the key and we press e then we'll come out the exit and open the door so this just basically links these together to make sure that everything is correct and how we want it out of the exit of this we're going to get a flip-flop which just toggles between two different values of a and b and this is how we're going to be opening and closing our door so we're going to come out of a and we're going to add a timeline like so i'm just going to name this open slash close door like so i'm going to make sure that that is going into the play the b will go into reverse because if we're playing it it's going to open if we're reversing it we're going to close it because we're just going to be playing the open animation backwards which is how you close it it's just the complete opposite it's just reversing it so now let's set up that animation so let's double click this timeline to open it up the length in here you're going to want to set to how long you want the door to take to open so i want it to take two seconds so i'll set the length to two then we're going to add a float track up here and i'm just going to name this door track you can name this absolutely whatever you like in this graph here i'm going to right click and add a key to curve float time zero value also zero so this is at the very start i'll right click add another keyframe with time two and value one so this is now at the very end you can press these buttons here to zoom to fit horizontally vertical and it's just going to simply go from the beginning to the end of the timeline and therefore opening and closing our door so this will open this will close then we can just close that timeline like so now you can see we have this float track here so what we're going to do is we're going to right click i'm going to get a lerp float so just a float up there the alpha going to the door track there like so so what this is going to do is it's going to use that value of 0 and one as the alpha to go between the values of a and b which we open and closing the door so let's set up those values of a and b now so what we're going to do is we're going to create some variables so if we go back to the viewport here we can have a look at the values that we want so our closed angle is going to be how it is now so you can select it you can see that on the zed the rotation is zero and we're checking this set because you want to rotate it on this edge like this so when it's closed it's going to be zero so it can do is we just hit a plus variable there call this closed angle changing this to a float up in the top right up here compile to change the default value and leave it at zero i'm gonna hit plus variable again image one open angle keeping it afloat compile change the default value and now let's set up what we want that to be so if we press e to rotate it and rotate it on the z i want it to open there so that's minus 100 on the z so i want it to open all the way to the angle of minus 100 let's put it back to zero and the open angle here let's change that value to minus 100 like so now if we compile go back to the event graph we can put these values in this lab so a is the start position which is going to be closed so we'll put our closed angle there b is the ending position which is going to be open so we'll put our open angle in there like so and when we're closing it it's going to reverse it so we'll go from open to close so this will work perfectly like so then to actually move the door all we need to do is simply just drag and drop a reference to our static mesh up here that we have for the door so you can name that door as well drag out this and then set relative rotation like so plugging that into the update of the timeline so every time the timeline updates it will move the door we're going to right click the new rotation split structure pin plug the return value of the slurp into the z as again we are rotating it on the zed and this will work perfectly this will now open the door so i'm going to select all this and hit c to comment it and i'll do open door if close enough and has key like so and then i'll select this hit c to comment it and just have open slash close door animation like so so we know exactly what the code does as well and that is it so that is the opening and closing the door code done on whether or not we have the key so now what we need to do is we need to set up the actual key blueprint and being able to pick it up so we compile save we can close this code here as that is what we need to do in there although actually there's one thing in here which i've just reminded myself of when i close this if we open this up what we're going to do is off of this branch here where we have false i'm going to play sound at location i'll put that there the location i'll just come back back and get active location so it plays this at the door like so and what we're going to put in here is our locked door sound effect which we have like so so what this is going to do is essentially if the door is locked and we want to open it it's going to come off a false because we don't have the key so it'll play the sound effect of the door being locked so the player then knows oh this door's locked i need to find a key somewhere so now this is it done we can compile save and close that and that should work perfectly so we can test that out as well to see if it does work so if we just place our door in the level where we want it like so and what i'm gonna do is if we hit play i'll go up to it i'll press e we get that sound effect there and the door doesn't open because we don't have the key yet because we haven't even set it up yet so press e we get the sound effect and the door doesn't open so let's create the key blueprint now so to do that it's very simple as well we're going to right click blueprint class get another actor i'm just going to call this key bp like that and we'll open it up straight away in here we want to do the exact same as the door when to add a component add a static mesh like so i'm going to make this one the key model that we have here now like i say this is very small by default from the person who made it so i'm just going to set this up to be 350 on the x y instead like so so it's just a little bit bigger scaled up more like that and again that's the value i found good to be earlier we deselect that we added another box collision like so again this is the area the player has to be in in order to pick up the key so i'm going to make this quite big so if we have on a desk or something the player can be next to the desk and still be able to pick it up so i think something like that will be good if we deselect it you see we have the key there and this box collision in which the player needs to be inside it in order to pick up the key you can compile go to the event graph like so and again delete these notes here this is all going to be very much the same as what we did in the door so in actual fact what we can do is because it's basically the same thing we can just minimize this here open up our door bp and we can just duplicate the code over so all this is the same so if we select interact the begin and end overlap with the cast and enable and disable input with the player controller and then also the gate as well we just select that hit control c go up to our qpp and hit ctrl v we now have this in here like so and this will work perfectly and the reason we had no errors is because everything had the same name so the box collision was just called box so it knows what it should be and then we can just plug the pressed straight into the gate like so and now we don't need to write the code again because it's the exact same and just duplicate it over except because it's now with this box collision instead this will work only for this key so it's not going to work for the door it'll work for the key and the code in the door won't work with the key it'll work the door so they're still very much their own independent pieces of code we just copy it over just to save us some time and now all we need to do out of this is as the person character from the cast again we're going to instead of get has key this time we're going to set as key plugging that off of the exit there we're going to set it to true so now the player does have the key off of this we're going to play sound at location with the location again just being get actor location like so and this sound i'm gonna have as our key pickup sound effect so key pick up there so when the player picks up the key they have a sound effect to signify as well then after this all we're going to do is get a destroy actor what this does is it just destroys the actor so it deletes it it removes it from the scene so the key is now gone as if the player picked up so it just gives the illusion of the player picking it up and this boolean here is essentially the item being picked up so now this works perfectly so we compile save this we should see this working so if we minimize as well then place in our key in here we just rotate it now this is quite a big box collision so it's also good to get it in the level see how big it is so the key is quite big as well but what we can do just go into the viewport and make this a little bit smaller so instead of maybe 350 i'll just make it 300 and if i minimize this like so i can see how big the box cushion is actually going to be when i am making it smaller so i if i make it smaller like so i say about there is a good size like that so now if we compile save minimize again we can now test it so we hit play to test it we go in try to open the door it's not going to open we have that sound effect like that as well we'll go over to the key we picked it up we've got the sound effect it's disappeared go back out to the door the door can now open and we don't get that sound effect anymore because it's no longer locked so that works perfectly like so so i think that'll be for this video is we've done everything we want to do we've set it up so we have a locked door by default and it signifies the player that it's locked with a sound effect we can go to a key and pick it up again with the sound effect it disappears all that good stuff and we go over to the door and we can now open it perfectly like so so thanks so much for watching i hope you enjoyed and i hope find it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see you in the next one
Info
Channel: Matt Aspland
Views: 43,870
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, blender, unity, games design, ue5, unreal engine 5, door, ue4 door, locked, lock, key, open, how to open, close, open door with key, open locked door, pick up, padlock, sfx, sound effects, with, lockable, unable, without, until, keylock, unlocked, ue4 locked door, locked door, unlock door, unlock with key, with key, animation, blueprint, blueprints, bp, easy, free, timeline, timelines, using
Id: 29--ieorX7Q
Channel Id: undefined
Length: 14min 57sec (897 seconds)
Published: Wed Jan 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.