Unreal Engine 4: Part Five – Opening and Closing Door [Lock and Key]

Unreal Engine 4: Part Five – Opening and Closing Door [Lock and Key]


Hey everyone and welcome back to part five. So in the last video we created a line trace So now we can actually interact with things. So in this video, let’s go ahead and create a door that we can actually open So let’s go ahead and add new blueprint class select actor Let’s rename this I’m gonna call this door underscore BP Open it up Then over in the components window, let’s add a new component. Let’s type static mesh Call this door. There we go so over here We’d go ahead and select your door mess if you’ve loaded one in or if you don’t have one on you selected The start to content with go ahead and search for one So I have the static mesh door We’ll just use this for now and then later on we can always come back and change these doors for our own So now I have this let’s go to the event graph So the first thing we need to do is Cast to the third person blueprint let’s get rid of these two here And we’ve done this before so you guys probably already know what’s coming let’s drag this out cast to third person character or whatever character you’re using And from the object wildcard, drag this out get player character Then from as the person character, I’m gonna drag this out and then just promote to variable And let’s go ahead and rename this variable Player character compile that I’m also going to comment this And I’m casting to the third person because there’s going to be some variables that we want to use from the third person So we will come back to this in a second The next thing we need to do is add the interaction interface as we did in the previous video So we just need to go up here to class settings Then over in the interfaces section. Let’s just add and then just find whatever you called it. Mine is called the interaction_interface and there we go, so Now I can right click Add event interaction So when the interaction button is pressed we want something to happen so we want this door to move So let’s just drag this out add a time line Then we can rename this time line to be rotate then if we double click this we can now add a float track and Then let’s rename this Rotate blend you can call this wherever you want. So I’m going to shift and left-click just to add a keyframe then over here I’m just going to type 0 and then 0 then hold shift left click again now this is going to be 1 and The value is also going to be 1 Now we can see the time line is 5 seconds, but we’re only going to be using one second of this So let’s go to the top here where it says length Change this to 1. So now when the door opens, it’s going to be a linear from 0 to 1 from closed to open So what we could do get a little fancy and click this one Shift click this one then if we right click on one of them and change the interpolation to automatic We now get this sort of curve this nice Bend so it will speed up and slow down rather than stay in a constant speed So now we have this we’ve compiled it save it. Let’s close this tab down now we don’t need it. So Now we can see we have this rotate blend option here which we will use From update we’re going to drag this out. We’re going to type set relative Rotation So we go down I want to set relative rotation for the door Then for the new rotation, I want to right-click here and just split the struct pin So now we have these individual values we can change So now we want to rotate this on one of these axis We could go to the viewport and find out which one it is If we select the door we can see we want to rotate it on the z So if we can see we want it to open and close like this So from this new option here whatever you called it, I’m going to drag this out and then we want to multiply by a float So now this value here becomes whatever degrees of rotation we want the door to turn so if we just plug this into the new rotation what we could also do is I we drag this out and Then just promote it to a variable Let’s also rename it Rotation Z And we also want to make this editable so click this then compile And there we go. That’s a very basic of a door opening We obviously need to refine this but I’m just gonna quickly show you one of the problems you’ll first find So if we drag this door into the scene, press play and try and open it If we try and open this down see what happens nothing happens and if we go around we can clearly see that the line trace isn’t hitting this door and As I mentioned in the previous video if we click this and the line doesn’t turn green after hitting the object We know the line trace is not interacting with the object For example, if we click this battery here we can see it creates a square and then goes green so what you’d need to do is let’s just go through the content and we would need to find where the door mesh is If you have loaded in the door yourself, you’d keep it in a folder But an easy way to find it if we go back to the blueprint Change this to the viewport select the door and Then if we hit this magnifying glass icon, then in the content browser, we’ll have the door selected so we can go back You see we have the door selected. So for this door let’s double click it Then all we need to do is go to the collision add box simplified collision and there we go Make sure we save it and then close this down Then we can close this down and press play If we try and open the door now, well, nothing happens since we didn’t actually set a rotation value So I’m going to go back, select the door Here’s where your rotation value will be. So as I say a rotation Z, let’s say we want this to open 90 degrees Now if we press play Go over to the door Opens and if we click it again Nothing happens so we go back Go back to the blueprint and then go back to the event graph So when we do this, we don’t want to play it. We actually want to play from start So what we’ll do is I’m going to hold ctrl and left click just disconnect this and then plug it into play from start Then when we press the button again, we want it to reverse from end So an easy way we can do that is adding a flip flop So from here drag out a now type flip-flop you can see we have flow control>flip-flop So what this is going to do when we click the button It’s going to first do A then the next time we click the button. It’s going to do B, then we click it again It’s going to do A B>A>B and it will just switch between them So if we take B just plug it into the reverse from end now if we compile that and go back Press play The door should now open and then close There we go So if you just wanted to do a basic door, that’s pretty much it But the next thing we want to do now is add a lock to this door So let’s go ahead and do that. Now. I’m going to move these battery pickups out of the way Let’s move them over here Then I’m just going to change the content browser so when we go back to content third person blueprint And blueprints open up the door blueprint if you’ve already got it open Which I do we’re going double click it again. What we could do now is add a lock So I add a condition to see if the if the players got a key Another thing we could do is maybe make the doors slide open and close instead of rotating For example elevator doors and other sliding type doors. So I wanna do is just grab all this Make some space Let’s go ahead and drag this out And we’re gonna add a branch Now we need to add a variable so click new then I’m gonna type rotate and then put question mark Just compile that also, I want to make this editable and then I’m also going to drag this and just place on top of rotation just so it moves it on top of it and Then if I grab it just drop it on to the condition. So if rotate is true We’re just going to rotate it If it’s false, then the door should slide open So really all we need to do is just select these like this Hit ctrl W just to duplicate it. Move it down Then if we connect this to the flip-flop Now we need to change this since we no longer need it to rotate. So I’m just going to delete that and Then from update we want to set relative Location and again for the door I might also select this now and delete that as well. We don’t need it And then just like before we can split this. So right click split struct pin And then we can connect this up as well and then this variable is no longer needed for this one We actually need to make some variables for the location X Y or Z If I go back to the viewport, so if this was a sliding door, it would be moving on the y axis So I doubt you’d need to move it, you know like this or like this But if you did you can also add those variables as well So go back to the event graph. We know we need to move it on the Y and Then this here we can bring this out promote to a variable and this is going to be move on the y axis We want to make sure it’s editable as well compile that So now we’ve got the option is it rotate? Yes it is. Then you rotate it by whatever degrees you want It’s not rotate then you can move it by whatever degrees you want. So I say maybe 50 see how that looks We’ll see it jumps forward a little bit But everything else is fine. I think let me just go back So it probably has something to do with the fact that we’re now set in the location of it as well So what I can do is just grab this We can say gets relative location And then we can right-click on this split the struct pin And then connect this to the X and the Z to the Z compile that Let’s try it again, so now it doesn’t jump forward it just plays how it should And we should go ahead and add another door, control W Make sure we put that back in position Move over, so obviously we need to rotate this one round as well. So I want to grab this. I’m just gonna rotate this round 180 degrees and then move it back in position So we’re gonna try and make a double door kind of thing move this over There we go. So I always like this door. It’s gonna rotate by 90 degrees This one is also going to rotate But we’re gonna want to flip this round. So I’ll just show you what I mean Like this opens this one opens the wrong way so all you need to do for this is just change this to minus 90 and It should work fine. All we need to do now is create a lock for this door and then create a key pickup to then open the door So inside the door blueprint Now we want to add another condition to this so before the rotate branch we’re gonna add another one Branch, so we need to know if the door requires a key. So that’s what I’m gonna create another variable and We’ll say need key as a question Compile that and then just drag this on here We also want to make this a public variable so we can edit it compile that So once we interact with the object, we’re going to see if it needs a key If it does need a key that we’re going to need to do something else if it doesn’t need a key then you can go Ahead and just rotate it. So I’m gonna disconnect this by holding ctrl and left click and Then I’m just going to connect this and connect the false to the branch. There we go So if it does need a key we need to check if the player has the key So we need to now create a variable inside the third person character. I’ll compile this Close this down for now Open up the third person character and then what we can do from here. Just create a new variable We’ll say has key1 as a question Compile that so by default the player doesn’t have a key but then when he picks up the item then this will turn to true So go back for the door. So again, if this door does need a key then we need to create another branch So now we can check if the player has the key. So we need to grab the reference of the player character We want to get the player character So just grab this and drag it out and then we want to get has key 1 like this And then we can connect this up. So if the player does have the key then you can go ahead and open it So check if it’s rotate or not So you can go ahead and open it as normal. So If the player doesn’t have the key we could create a prompt. So for now, I’m just gonna add a print string And then type collect key one Compile so now let’s test this out So what we first need to do is select one of these doors So down here where it says need key? we say yes, it does need a key Press play So the door in the left should open fine with no problem. And this one should tell us that it needs a key There we go So no matter how many times I click this It’ll tell me you need to collect a key and it won’t open So that’s exactly what we wanted the next thing we need to do is just add that key mesh if you guys want to download the key mesh and I’ll make Sure to leave a link in the blog. So I’m going to go to my assets Right click add a new folder. I’m going and call this key mesh So I’m definitely gonna have more than one key since is gonna be more doors that need to be opened But for this example, I’m just gonna bring one over Like so import it And I also need to tweak the material. So again, this is just a placeholder material for now So now we have the key. Mesh. Let’s go ahead and create a blueprint so I’m gonna go back to blueprints Right click go to blueprint class Add an actor And then let’s call this key one underscore pickup Underscore blueprint then we can open this up. It’s pretty much the same thing We did with the battery pickup just with it maybe a different model or to add component or static mesh Key one and then let’s go to the static mesh a search for key. Mesh. There we go Now let’s go to the event graph Select these two delete them. We’re going to be using the event begin play Since we need to cast a third person so I’ve dragged this out cast to third person character From the object wildcard grab this out get player character And then as the third person character will drag this out and then promote to a variable And rename this variable call it whatever you want I’m gonna call it player character compile it and I’m also going to comment this just to keep it tidy. So now we want to interact with this key and since we’ve already created the line trace and the interface We can just go over to the class settings Then down to interfaces Find the interface that you created. The one I created was interaction underscore interface Now we’ve added this I can right click Add event interaction interact Now when the player interacts with this key we want to change a variable So let’s grab the player character Get the play character and if you remember, we already have that variable that key variable So if we grab this out and now this time we want to set the variable so we can type set has key 1 Then plug this in When the player interacts with the key it’s going to change this to true Then we just want to destroy the actor Let’s compile that So now let’s make sure this works. I’m just gonna grab the key drop it into the scene It is still far too big So let’s just make some adjustments Now let’s try this now So we can see the key doesn’t look too good But we can always come back and make this look better if we first try and open the door You see it doesn’t work if we collect the key and then open the door. So there we go That’s pretty much the door system done So now what we could do with this door blueprint, we could probably modify it or simplify it and turn it into drawers So if we wanted to open drawers and then collect an item from inside it it’s pretty much the same way Next part that’s going to be coming on Monday We’re going to be working on the objectives and the objective description. So when we want something to happen We’ll add an objective So that’s going to be pretty simple to do so. Hopefully you enjoyed this video as always Thanks for watching and I’ll see you next time

Leave a Reply

Your email address will not be published. Required fields are marked *