How To Open And Close A Door In Unreal Engine 5 | Efficient Methods And An In-Depth Explanation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another unwrenching five tutorial so in today's video what we're gonna be doing is going back to some of the basics of our mention four but obviously doing it now inside of unreal engine five and today's video is going to be how to open and close a door inside of unreal engine five and the way i'm doing it today is also going to be very efficient and very customizable so let me hit play and show you what i'm gonna make today so we go over here to this door press e and this one is going to take five seconds to open i believe it was anyway but it's gonna open and then press e to close it again after obviously walking through if i wanted and the same with this one just to show that you can have multiple doors within the level and they will still all open and close and work perfectly how you want as well and again this is very easy to customize so i've gone to this door i can make this one take five seconds to open and three seconds to close and this one over here i can have three seconds to open and five seconds close i've wanted obviously that doesn't make much sense but it's very easy to customize to see how long it should take to open and close in seconds as you can see there and we can also do is maybe add in the door mesh we can change that as well if we wanted change the collision box size all these different things we can customize however much we want to and like i say this is also going to be a nice efficient way of doing it and we're going to be using blueprint interfaces so this tutorial will also teach you how to use those as well so without further ado let me take this code and i'll show you how i've done it so the first thing we want to do is we want to actually create our blueprint interface so we can interact from our character with the door so to do that we're going to obviously press control space to open our content browser and then we just want to create a new blueprint interface so i'm going to be just doing this here so i'm going to right click go to blueprints not blueprint class but blueprints under create advanced asset then we want to create a blueprint interface this one i'm going to name interact interface as that makes the most sense for me and we can then use this for anything we want to interact with as well it doesn't just have to be doors it can be doors picking things up or anything which you want to interact with i'm going to open this up straight away in here all i'm going to do is name this function interact and that's all we need to do this is a read only function so all we need to do is give it a name we can compile and save that what you can do as well if you want is give it an input and output so just add some examples so i'm going to create them i won't use them but i'll create them just to give some examples so what you can do is give a new parameter on the inputs naming this one interactor and then make this a character object reference then an output you could have reset that there you can have an output as did interact or was successful or anything like that with this one being a boolean so we can compile and save and this is what you'd now have again this is all you need to do in here so we're going to close this and essentially what that does is we can customize that function to have something different inside of it in each blueprint and then we can call that specific function from another blueprint so that'll make more sense when i get into it later and i do also have another video where i go through in more detail if that's what you wanted but next up we need to actually call this interact interface so once again we're going to control space this time we can open up our character blueprint so for me that's content third person blueprints bp third person character now in here what i'm also going to do first is go to edit project settings once this loads let me put on the correct monitor here we're going to go down to input and we're going to create an action mapping so i'm going to open this up hit the plus and i'm going to name this one interact and i'm going to set this to be the e key now this can be e f or it can be e and f it can be absolutely whatever you want and the benefit of action mappings is you can set up multiple keys keys for different consoles and you can also set up key bindings as well if that's what you wanted but this is all i need so i'm now going to close this like so then back in our third person character we're going to right click and search for whatever you just named that i named mine interact like so now we don't want the interact message that is the blueprint interface which we're going to use later we want the action events interact there so we're going to select that and now you see we have this input action here so every time the player presses the button which you set up in your input action it's going to fire off this event here now what this we're going to do is obviously interact with a blueprint so underneath it we're going to right click and get overlapping actors and we want to set the class filter to just be actor like so now out of this array return value here we're going to get a for each loop with break the with break is important because we only want to interact with one at a time otherwise this will interact with every single thing we're overlapping all at once which we obviously do not want so out of the array element we're going to get does implement interface with the interface being our interact interface which we just set up here then this is obviously giving a boolean return value so we want to hold down b left click to get a branch with that being the condition and plugging that into the loop body so if this is true or false i.e if the actor we're currently overlapping has the interact interface if it does we want to interact with it if it doesn't we're not going to do anything and then we're going to check the next item in the array that we're standing on so out of array element we're going to call the interact message here so your interact interface interact message as that's the name of our function true we'll go into there and then what we're going to do is we can then do interactor is self so get a reference to self so it's the player interacting with it and then hold down b left gates get another branch connecting that to there with the condition being the did interact again you don't need that if you don't want it but i'm going to put it there anyway just to show you how it works and true of that branch then goes into the break of before each loop with break like so and i'm going to double click these to get some root notes just to keep it looking nice and organized again essentially what this is doing is going to get what we're overlapping and check to see if we can interact with it and if we can it will interact with it if we can't it will check the next one in the array and if it tries to interact with it but didn't manage to interact it will then again go on to the next item in the array and try and interact with that one instead so i'm going to select all this and hit c to comment it naming this interact code so this will allow us to interact with anything in our world so instead of having to do all the code in each individual blueprint you do it once here and then just set up the interact code within the function in each blueprint and it should be able to call it here perfectly so again it's nice and efficient and a lot easier so we can compile and save that now we're going to close that as that's all we need to do there the next step is now actually setting up our door blueprint so once again we're going to go to control space back to content i'm going to make it here so right click go to blueprint class create an actor and i'm going to name this one door bp like so opening it up straight away in here i'm going to add a static mesh like so so search for add components static mesh and i'm going to name this one door sm for door static mesh over on the right down here we can change the static mesh to be a door sm sm door and this is included in the starter content of unreal engine 5 as well so we can use it like so now if you don't have the start content what you can do is minimize this go to the content browser go to add in the top left and we want to add feature or content pack go to content and you want to just add the start content and add to project and that will then give you the start content which includes this door as well now if you want to use your own door static mesh instead of this one that's perfectly fine we need to make sure you do though is have the axis point on the side here so when you rotate the mesh it will rotate like that otherwise it will rotate from the middle and that's obviously not how a door opens so you want to make sure the origin point of the mesh is in the correct location so it actually rotates on the side like this then after this you want to deselect it and add a box collision like so and then we just want to scale this up to be the correct size we want so what this is is doing is basically if the player is inside of this box they can open and close the door so again this is now how the interact code in our blueprint in our player is seeing what we're overlapping so if they're inside of this box collision they're overlapping the door so they can interact with it so again just keep that in mind of how big or small you want this if the player is within this box collision they can open and close the door we'll compile and save that and that's all we need to do in the viewport we're now going to go to the event graph and start setting up some of the code in here we can delete all these three nodes if you don't need them compile and save that and we need to actually now add in our interact interface so we can interact with this door so that'll be class settings up the top under interfaces we have no interfaces so we're going to add and search for the interact interface which we created there you go we'll compile and save that and now you should have a tab on the left under interfaces and we've got interact here if we double click that it will open the function in which we can now do all of our code and you'll see this is exactly how we set it up within the blueprint interface so i say this is now where we're going to be opening and closing the door so to start off with what we want to do is get a plus variable here naming this one is open question mark leaving it as a boolean compiling and leaving the default value as false so by default it is closed because as you can see here it's closed by default now if you have it open by default just tick this and obviously change it accordingly so back to our function we're going to do is hold down control drag this blue line in it to get it and out of this we want to get a not boolean so not not equal but we want a not boolean here which is essentially just going to inverse this boolean then hold down b left click to get a branch connecting all of these in together like so so essentially is open not i.e if the door is not open so if the door is closed it will come off of true and if the door is open it will come off a false now that might seem a bit weird i'm doing that backwards only reason is because if it's open we want to come off with true because that's going to be in the top of the rest of the code you're doing so it just helps to keep it looking a bit more organized that's the only reason why i'm doing that next what i want to do is create some more variables so i'm going to hit the plus variable down here again naming this one time to open brackets s for seconds so we know it's in seconds and this is going to be a float a float value being a number value which can also have a decimal point so you can make this 3.5 instead of just three if that's what you wanted then we're going to create another variable naming this time to close in seconds also leaving that as a float as well and now these two variables here we want to press this little i next to the float and that will make them instance editable which means we can change them for each different instance of the door which we have in the level which is what you saw me doing at the beginning where i was changing the number for each door that is how we do that here so we'll compile and save that like so now to actually implement these so it will change how long we want to open the door for we need to first animate the door so a nice easy way to do that is by right clicking and add timeline if i suppose it correctly add timeline although we can't actually do that here because this is inside of a function so what we need to do is go to the event graph and here we can do right click add timeline like so and i'm going to name this one open door t now the reason why we can't do it inside of a function is because a function can't have latent nodes i.e anything which slows down like a timeline or a delay or anything like that so if you haven't included these nodes here so the interactor and did interact if you haven't included the input and outputs this won't be inside of a function it will just be event so it will still work perfectly for you but if you've done what i've done what i'm doing now is the workaround for that so i'm going to do is just here simply right click add custom event naming this open door right click add custom event naming this closed door nice and simple like so then open door we're going to play closed door will go into reverse not play from start and reverse from end just play and reverse simply enough like so and there we go now we're going to go back to my interact interface and start setting up the rest of the code which i mentioned so once you've created that timeline in your variables list you should have a components list and have open door t here or the name of your timeline drag that in and get it here and out of this we're going to set play rate like so so set play rate there connecting that into true and move that above and using the play rate is how we're going to actually determine how long it's going to take to open to open and close so out of this we're going to get a divide like so and that should give a float divided by a float the top value is going to be 1 and the bottom value is going to be our variable of time to open in seconds so one divided by the time to open set the play rate and that will then give us the correct amount of time to open and close inside of our timeline and we're obviously just going to do the exact same for reversing as well so for closing the door so we can duplicate these set play rates connecting that down there and i'll just move this here connect it in like so move this down so it's equal and then once again it's the same so i can actually just duplicate these as well connect that in there then what i'm going to do is just drag on times close on top of the time to open and it will change it like so so we can compile and save that and that should now work perfectly for us after this what i'm going to do is just after the top set play rate i'm going to call function open door and the bottom one i'm going to call function closed door perfectly like so we'll compile and save that and that's all we need to do in here now so i'm gonna then just connect those into there and tick did interact as we've now interacted with it so compile and save again and now we're gonna go back to the event graph to finish setting up the code for animating our door so if we double click on our timeline we can open up we're going to add a track adding a float track and i'm going to name this one door track if i could spell it correctly that'd be better the name doesn't matter too much but it just makes sense to call that and the length we're going to set to one second now that is very important because if you want this code here to work for changing the time to open and close the length needs to be one second then inside of here we're going to right click add key to curfload time of zero value also zero right click add key time of one value auto of one so it's going from the very beginning to the very end in a nice smooth line like this now if you want to make this line smoother you can do you can right click on the curfloads and change the key interpolation to auto like this and then you can obviously also move these about a bit as well if you wanted to get a little bit more customized if you wanted and you can obviously then add more keys like this i mean that's what's going to be where it's going to open and close open but you get the picture it's very easy to customize if that's what you wanted to do but i'm not going to bother doing that for this door as i don't think i need it but we'll compile save i mean close that timeline now and then go back to the event graph here so now you notice we've got this door track value here out of this we're going to drag out this and get a lurp lurk being under float there this track is going to go into the alpha not the a as what it's going to do is it's going to go from a value of a to a value of b via the alpha the alpha being a zero to one so it's going to smoothly interplay between a and b using this value here i do have another video going more in depth into what larp does if you wanted to see that so a and b is going to be our open and closed positions so we need to input those in now so i'm going to go to the viewport here and this is our closed position so what actually is this position well it's zero because if we look at our rotation here on the z it is zero so that's the blue one here it's zero so we're going to create a plus variable here naming this close position and compile that leaving the float value as zero then i want to move this into the open position so if i select it press e and rotate it to where i want it to be when it's open i think that's going to be good now you notice for me that is -110 so i reset this back to zero so it's closed create another variable calling it open position we'll compile that and set the default value to -110 so now we've set up our open and closed positions so go back to event graph we can put those values in to our a and b a being close b being open because again playing goes from a to b and playing is opening so we're going from close to open perfectly like so so i hope that all makes sense so now we've got the correct value going from the close to open how do we also set that to be rotating the door well that's very simple we can drag in our door static mesh here out of this set the relative rotation making sure it's relative not world right clicking rotation split the structure pin and connect the return value from the lerp into the new rotation z like so and if you've changed the x or y rotations then make sure you input those in there as well you probably haven't changed it but just in case you have put them in there too and we're going to connect that into the update of the timeline like so so now that's actually going to update and open and close the door how we want however we also need to make sure we tell the code if the door is opened or closed so out of finished of this timeline so once the door has actually opened or closed we're going to set the is open boolean which we created earlier again connect that into finished there what we're going to set this to is basically just the opposite of what it currently is because if it's open once this is finished it'll be closed if it's closed once this finishes it will be opened so to do that we can just get is open and then get the knot to be in once again which we got earlier because again that just essentially inverses the value so we'll set it to the inverse of its own value so if it's open it's closed closed open and so on and so forth so we'll compile and save that and this now should be the code all completely done and working perfectly for us so again nice and simple but also very efficient and customizable for us to do so we'll close this drag one into the level like so and then we can customize it so time to open let's put two seconds time to close let's put five and we'll drag another one in as well just to show it what's working here too this one being time to open one time to close one so let's hit play to test this out we'll walk over to this one you can press e to open it it's going to take three seconds i believe it sets up or two and we can close it as well and you can see that's taking longer as i set it up to take longer we can go to this one we can also open this one too and this one is nice and quick now it's opening that way i want it to open the other way so very simply i'm just going to rotate it around like so perfectly like this and i'll rotate this one around two and then let's have a look at this and we'll notice these are now opening the other way perfectly like so so i think that'll be it for this video as we've done whatever we want to do we've set it up so we can now open and close the door inside of unreal engine 5 very efficiently and very customizable as well so we can change how long it's gonna take to open and close and as you can see as well this also works for multiple doors within the level two so thanks for watching this video i hope you enjoyed it i hope you found it helpful and if you did make sure to like and subscribe down below so thanks for watching and i'll see you next one [Music]
Info
Channel: Matt Aspland
Views: 48,382
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, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, door, open, close, and, in-depth, explanation, how to open, how to close, timeline, use, efficient, multiple, doors, in level, level, how to open a door, ue5 door, in, animation, animations, animated
Id: Eakt2hBd3YY
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Fri Apr 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.