Button And Door System | Open A Door Somewhere Else In The Level - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another mention 4 tutorial in today's video we're going to be going over setting up a button and door system so in this you can press a button which will open a door and in this example as well i'm also going to be going over how to open a door in another part of the level and have the camera show you which door has been opening because both of these are separate requests i've got so i thought i might as well put them together because you can either use this system as one because it does go quite well together or you can split them up and use them separately as well if you wanted to so both of these mechanics are used quite commonly in different games so let me hit play and show you what can i make today so if i go over you can see we have a button on the wall here if i press e we're going to open this door which we're now looking through a different camera to watch the door open and if we to go over here this is where the door has actually opened if i to hit play you can see that this door is closed over there and then if i press e on this button to open it the door is going to open looking through a different camera and we can go see that door has actually opened and in this as well what we can do is we can have multiple buttons and multiple doors in the level and each button will work for a corresponding door which you can set up as well so this is very easy to build upon and grow to whatever size you want so if you have 10 different doors in your level with 10 different buttons it will all work perfectly for how you want so this is what we make today so without further ado let me do this code and i'll show you how i've done it so the first thing we want to do is create a blueprint interface so we can nice and easily and efficiently interact with different items i.e the door and the button so to do this we're going to right click go to blueprints here it is and get a blueprint interface and i'm just going to name this one interact interface like so opening this up straight away in here all we're going to do is name this function interact as that's what i want to do with this function and i'm going to add an input onto here as well naming this player or interactor or anything like that changing this to a character object reference because what i want to do is save that is when we interact with this interact interface it's going to fire off this function which will do whatever code we want which we can set up in the different blueprints and we're also going to input the current player interacting with this object and we're doing that so we can then change the camera views between this current player and only that player so that's all he's doing here so we can close that like so then we're going to create our door blueprint so i'm going to right click go to blueprint class create an actor and we're going to name this one doorbp or naming door pp1 opening that up straight away as well and all i'm going to do is just a basic cube and camera in here for my door so you can also use an actual door static mesh which you have if you want but for me i'm just going to use a cube like so what i'm also going to do is open up my reference one which i made previously just so i can get the correct dimensions for it like so because again i've made this specifically for something in my level so i'm just going to right click copy on the scale and then paste that into here so i'm using 6 on the x 0.5 on the y and 8.1 on the z again obviously customize this to be whatever you want then i'm going to dislike that add a component and adding in a camera and this so that we can then look at this door opening up through a different camera perspective when we are doing it so we're gonna be using this camera in the level and again i'm gonna be using my reference to find out where i want this camera to be placed by just copying the location and rotation so if i go on here i can paste that and i'll do the same with the rotation and i will also show you how i found this location rotation the first time if i compile and save that also close the reference what i did was i'm going to place in my door in the level so again i've kept this here just for a reference of where i want it to be so i'm going to copy that place this in paste that there and then rotate it and what i did was it was the other way around you can see in the bottom of my screen i have this camera view here that is the view that the camera is going to see in this blueprint so when we look through the camera we'll see this so what i did was i had that there opened this up just a little bit smaller like that so i can still see it and then i just moved it like so sorry i tracked into the wrong one i dragged in the reference there when i meant to drag in just the actual new door which i made so to do that now this should work for us so again if we were to just move this camera you'll see it's moving in the bottom right there so that's just how i find the correct position i want it to be in so i recommend doing that for you as well and so again i've also placed in my door in the level like so so we'll compile and save that and that's all we need to do to set up the door we've given it a static mesh and a camera but what we're also going to do is add a variable here naming this door value and this is going to be an integer and we're going to make sure this is instance editable and this is the first part of how we're going to actually set up assigning a door and a button so we're going to have a door value and a button value and if they are the same value they are going to be linked so that button will open this door so i hope that makes sense again we'll get into that later on then we're going to go to the event graph delete these three nodes and i'm going to set up the code for actually opening the door so i'm going to right click add a custom event naming this open door and out of this what i'm going to do is i'm going to add a timeline now if you have an actual animation you have made for your door you can obviously just do play animation but i'm just going to create a basic one here so i'm going to name this timeline open door t for timeline where they're going into play like so then i'm going to double click to open this up setting the length to 3 seconds because i wanted to take three seconds to fully open obviously set that to be whatever you want and we're going to add a float track naming this open track on here i'm going to right click add key to curve float to the time of zero and the value also zero right click again add key time of three so the maximum length you have and a value of one so it's going from zero to one over the complete length of our timeline compile save and close the timeline now you can see we have this track here drag out of the track and get a larp vector because we want to learn between the values of a and b using this alpha track here going from zero to one and the a and b are vectors because it's a location because i want to move the door now if you were just rotating the door you'd probably just want to use a normal float and just get the rotation on the z i do have other videos going over that but i want to move the whole door as you saw in the video introduction so what i'm going to do is go from a which for me is just going to be 0 0 0 so this position here and then i want to move all the way down so again what i'm going to do is just use the reference here so if i had to move it all the way down i can see when it's in that position that's going to be fully open for me so that is going to be minus 820 on the z or i can just copy that there compile save go back to event graph and just put that into b so for me it was minus 820 on the zed again do it for whatever it is for you then we actually want to move the door so i'm going to drag in cube drag out and set relative location like so i'm sure it is set relative not set world new location is going to be a return value there and that is now going to open our door perfectly for how we want but we're also going to remember that we are going to be changing the camera to be looking through this door which we'll do in another blueprint but once the door has finished opening we do want to make sure we set the camera back to the player so what i'm going to do is hold down d left click to get a delay connecting that into finished of the timeline so it's going to fire off after the timeline has finished so the door has fully opened and i'm gonna set the duration to one so one second after the door has opened we're gonna go back to the player so we're gonna right click get player controller like so drag out that and set view target with blend connecting that into the delay now the blend time i'm gonna leave a zero so it just instantly snaps between the two camera views however you can of course change this to be one for example and that means it will take one second going between where the player is to where the door is or suppose in this example where the door is to where the player is but again i just want it to snap so i'm going to leave it at zero and the new view target wants to be the player so it's going to drag that into the custom event to add pin to node and i'm just going to name this player which is again what we did in the function we made in the interface which we'll integrate in a moment and we're going to pass that through into this function so we know which one it is i'm going to compile save and that is all we need to do here for opening the door like so so we're gonna have the animation and then change the camera back to the player once we finished it so i'm gonna close this like so now we want to set up the button so again i'm gonna right click add a blueprint class add an actor naming this button bp one for me opening this up and again i'm also going to open up my reference so again i can keep the correct sizing which i had so button pp ref there now don't worry that i'm getting errors in there that's just because i deleted the interface to show you how to create it in this video so i can delete this code compile that fixes that so again i've got a cube and a box collision so in our button bp i'm going to add a cube giving it the same dimensions i had before which again you can obviously customize to get it perfect for you or again obviously use an actual static mesh this is why i have just .5.25.25 and then also add a box collision and this can be how big you want it just means when the player is inside this box collision they're going to be able to interact with this button so again the way i found out how big i wanted to be was to just place it in a level in c so if i was to get the location of this and delete it place in my actual button put it back we can see that the box version is this size so the player needs to be within this location in order to interact with the button and i think that's going to be good for me that looks good enough so i'm going to reopen the button bp here we're going to go to the event graph delete these three nodes and start doing the actual code for interacting with it so this is where we're going to be using the interact interface so we're going to class settings add an interface adding the interact blueprint interface which we just created earlier and you can see over on the right we have interfaces interact that's the name of the function so i'm going to right click implement function so we can do the code on here and now again you can see we have this player because that's the player which is interacting with the button so the one we want to be changing cameras for and so before we do the rest of this code actually what we also need to do is add a variable naming this button value and again like we did at the door this is going to be an integer with it being instance editable compartment save so again we can now compare the button value to the door value to make sure they are the same and to compare these what we're going to do is come out of event interact and get all actors of class like so and the actor class is going to be our door bp so we're going to be looking at all of the doors we currently have in the level out actors is going to go into a for each loop with break connecting the execution into there so it's going to go through all of the different doors we have in the level until we find the one we want to interact with so to find out what we want to interact with we're going to get the array elements get the door value integer which we made before drag out of this and get an equal equal integer with the bottom integer being our button value so if the door value is equal to the button value they are linked we want to make sure that we are interacting and opening this door so to check this we're going to hold down b left click to get branch with that being the condition and the execution going to loop body false we're not going to do anything so i'm just going to go back through the loop to check again to see if the next door in the array is the one we want to interact with off of true we're going to interact so we're going to come out of array element and get open door like so which is going to fire off the code we made before which again is going to actually obviously open the door and then afterwards change the camera back and the player is the input we put on that custom event so that is going to be the player from the event interact here so that's how we're going to be linking those like so then what we're also going to do is right click get player character and the return value is going to be again set view target with blend like we did earlier sorry i didn't mean to get play character i'm going to get player controller like so with return value being set view target with blend and the new view target is going to be the array element out of the for each loop like so so we're going to be setting it to the current door so again what we're doing is getting all of the doors seeing which one is linked to this button so one we want to actually open then we're opening it and we're also going to set our camera view to be that door so we're going to be looking through it again as i showed you at the start of the video so let's compile save and close that like so and now the final step is just to actually be able to interact with this button so we're going to do that in our character blueprint so for me that's content third person bp blueprints third person character very simply in here i'm just gonna do some nice interaction code so i'm gonna right click get an e keyboard event or you can obviously create an action mapping as well if you want to and underneath this i'm going to right click get overlapping actors with the class filter just being actor so we're going to see which actors we are currently standing on top of or overlapping ie our button and out of overlapping actors we're going to get a for each loop with break like we did in the button code earlier and after the array element we're going to get a does implement interface with the interface being our interact interface because obviously we only want to try and interact with an object we are overlapping if it currently has the interact interface otherwise we won't be able to interact with it so there's no point trying so then we're going to hold down b left click to get a branch with that being the condition and the execution going to the into the loop body false again doing nothing true is going to then interact with that element so the array element is going to go into an interact message like so so that is then going to fire off that interact event and function which is obviously going to do whatever code it is for the object we're trying to interact with in our case the button which will open the door and change the player's view and all that good stuff now this is where we're going to actually input the player so again this is the player which is interacting with it which is going to work for all of the other stuff we've just set up again mainly for changing the player's camera so all we're going to do is just get a reference to self because it's in the character blueprint this is the character trying to interact with it so it's going to be this one here then drag out of interact and that is going to go into the break of the for each loop like so which reminds me i'm not sure if we did that in the button so i'll go open it up in a second but again what i'm going to do is just stop the loop because we found what we want to interact with so we're going to interact with it so we'll compile save and that is our interaction code completely set up to interact with the button which will interact with the door which would do everything we want so we'll close this and again i'm just going to double check my button like so i don't think we did no we didn't so i'm just going to drag out of set view target with a blend taking that into the break of the for each loop again like so so it's going to stop looking for the other doors because we found the door we want to interact with that isn't going to be another one so we don't need to bother looking compile save close this and again i've already dragged in my door and button but once we've dragged them in what we're going to do is set the button and door value now by default they're both going to be zero which is going to work because they're the same but let's say this button it has a value of five i want to make sure that this door also has a value of five because again those are now linked together they have the same value so they're linked perfectly so if i were to hit play i can show you that this should be working so if we go over to the button press e it's changed our camera view and it's opened the door perfectly like so and a second later we've now gone back to this camera view and this door has opened so this works perfectly so i think that'll be it for this video which we've done everything we want to do we've set up so we can interact with a button which will open a door somewhere else in the level showing us a camera view of that as well and again or you don't even have to do the camera view you can just have it as a button which does something else for example opening a door so thanks so much for watching i hope you enjoyed and i hope you found 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 [Music] you
Info
Channel: Matt Aspland
Views: 3,153
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to, ue5, unreal engine 5, door, button, system, level, camera, look, watch, view, see, somewhere, else, in, the, open a door, ue4 door, open, door button, open a door with a button, for, advanced, different, doors, use, open door, ue4 button, press, push, open a door using a button, pressing, use a button, how to use a button, push a button, button push, interact, linked, link, interface, blueprint, blueprints, pushing
Id: exs1uMuqQSE
Channel Id: undefined
Length: 16min 0sec (960 seconds)
Published: Wed Oct 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.