How To Open A Door Using A Button - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another management full tutorial in today's video i create a door in which you can open by pressing a button so here you see we have a little button on the side of the door we're going to push that in the door will open so i show what's going on like now so if we hit play we can walk up to the door if i press the button that's going to open and the door will open if i press it again the bottom will activate and the door will close so you can see we have a little animation for the button door opens animation for the button the door closes and you can do this on anything that you want as well doesn't have to be a door you just follow the same code that i do here and obviously change the door section so i'll delete all the code i have now and show you how we're going to do this so our first step is going to be to create this door blueprint that we have here so i had it there by deleting obviously so to create that what we're going to do is we're going to right click create a blueprint class and then get an actor i'm going to name this one button door bp you can add this absolutely whatever you like and open that up straight away like so in here what we're going to do is we're going to add a few components the first one being a static mesh i'm going to name this one door like so and with this still selected i'm going to change the static mesh to be the start content door so sm door like so we have it there then we're going to add another component so actually deselect that add component i'm going to get another static mesh and i'm going to call this one button and then i'm just going to change this to be a cube like so i'm going to change the scale on the x to be 0.1 on the y to be 0.25 and i said to 0.5 to get this kind of button shape here you can obviously set that to be absolutely whatever you like and then i'm going to move this into a position which i'd like it to be in so actually i want to move on to this side so i think minus 150 on the y 125 on the z and then i'll move it forward on the x by minus five as well just so it's slightly forward so we can then push it in so i think that'll be a good position for my button like so obviously you can put this wherever you like and to get a good reference what we can do is just minimize this a bit like so and then just placing our door in here like this so then if we minimize that fully we can properly place our door in and then once we've placed our door in we can get a better representation of where this button will be in relation to the door on the wall so for example i want mine to be there so that's where i'm going to leave it obviously you can put this in open that up again and then you can just move this about and see where it will be on the wall like so and i'm going to show you another way of moving this about so i'm just putting as a reference so if we compile and save that what i'm going to do before i add in anything else in here i'm going to go to the construction script i'm going to drag and drop a reference to our button in here like so out of this i'm going to come out and set relative transform so set relative transform like that plugging that into the construction script there i'm going to right click on the new transform and promote that to be a variable i'm going to call this button transform like so and then i'm going to take this i here to make it instance editable meaning we can change it on each different reference so if we compile and save that i'm going to change the default values here so again on the x it was -5 the y minus 150 the z 125 obviously you can put this to whatever you like and the scale i had point one point two five and point five compare the save and now if we just completely minimize this all you see is that if we have this door selected you see under here under the default we have button transform now we can just change this position here like so so we can move it to be absolutely wherever we want so i want it to be on this side i can do that i can just move it there move it up a bit like so and then the reason we've done it like that as well is if we get another button transform in we can change it on here so it can be different for each door like so so each door can have a different door position bottom position sorry and button scale like that so obviously you can customize this to get it perfect for you however you want but i think for me this will be good like that but that just means we can dynamically change where the button will be on each reference to this door so let's open up our blueprint again and like i said we're going to go back to the viewport we're going to add another component and this one is going to be a box collision like so i'm just going to call this one interaction box or area anything like that which makes no sense for you as it sounds this is where the player needs to be in order to open the door so i'm just going to place this in the middle here and scale it up to how big i think it needs to be so i'll do it like this so basically if the player is in this area then they can open the door so again you can drag this in and see how big this is going to be so i think that'll be a good size like that so basically if the player is standing anywhere in here make it open and close this door so that's going to be good for me customize that to be the perfect size for you and after this what i'm going to do is we're going to go over to the event graph like so and now we're going to write the code for opening the door and also pressing the button so i'm going to delete these nodes here i'm going to do that is right click the interaction box add event add-on component begin overlap right click it again add event add-on opponent end overlap out of these the other actor i'm going to come out and i'm going to cast to third person character but you're essentially just casting to your character so it'll be third first or what if you've named it and the reason we're doing this is so it then registers if it is this actor or this character overlapping so we can only fire off this code if it is the third person character basically the player in this box collision so if you have like an ai it won't fire off for them as well so this is great and just a little bit above this and this is where we're going to get our interaction button so for this i'm going to create an action mapping so i'm going to go to edit project settings once this loads we're going to go down to input down here so input there and we're going to create a plus action mapping so i already have it here so i'll do is i'll delete it hit the plus action mapping there i'm just going to name this one interact like that i'm going to make this the e key so you can give this whatever you like but i'm getting with the e key as that's what interacts most commonly is you can have his e f left mouse button anything like that this is just essentially what you're going to use to open the door or interact with things and the benefit of this is you can add multiple buttons buttons for different consoles and then also key bindings so once you've done that we're going to close this like so then back in the infant graph here we're going to right click and search that action mapping we just made so i called mine interact so action event interact like so interaction there then after all this we're going to do so i'm going to hold down g left click to get a gate the enter will be the press of that interact there and actually one more thing we're going to do after this cast is we're going to enable and disable the input meaning the player can actually use this interact key here so to do that we're going to right click and get player controller so we are going to be accessing the player's current controller the return value of that we're going to enable input with not the target as player controller but the player controller and that will be going into the beginning of that then off the end overlap we're going to get disable input like so and this means we can only use the e key when we're in the correct area so it's not going to be firing off all the time every single time the player presses e so compile and save that and now that should be working great so the enable input is gonna go into the open of the gate disable we're gonna close and what this means is that we can only fire off this code if we are in this box collision and we press e or interact button so it just makes it a lot more efficient and this basically means you have to be near the door in order to open it so it just works great compile and save that and what we're going to do now is we want to have an animation for the button going in there now so you saw at the start of the video the button went in and out as i pressed it so we're going to do that now so go back to the event graph here what i'm going to do is i'm going to right click and add a timeline like so and i'm going to call this one button press like so as that makes sense to me i'm going to double click on this to open it up the length i'm going to set to be 0.1 so it's going to be 0.1 seconds so it's very quick it's just going to go in and out like you would normally press a button and then i'm going to add a float track here the name of this track i'm just going to call button track like so then i'm just going to right click on this add key time 0 value of 0 as well as this is at the start of the timeline right click again add key time is going to be halfway through so 0.05 with a value of one so it's halfway through and it's in the end position and we're going to right click add key time of 0.1 so it's at the end of the timeline and a value of zero again and we can zoom to fit horizontally vertical there and what this is basically going to do is the button will be out the bottom will be in and the button will be out again so it's just going to do that like so so if we close that timeline now to actually make it do that with the actual button so we can see it what we do is we're going to come up with the button track here and we're going to get a lap vector like so this is going to change the position of the button so to do that as well we're going to drag and drop a reference to our button in here like so come out of that and we're going to set relative location set relative location like that with a new location as a return value of that love vector there a and b are going to be the start and end values so a we're going to right click promote to variable call this button out b right click promote variable call this button in like that so see they are the button in and out locations so button out is basically the bottom location it's in now so select button transform we can right click location copy go back to button out compile so we can change the value right click and paste so we have that in there button in i just want to have the same so i'll paste i'm just going to change the x to be zero so i moved it out by minus five and i'm going to push it all the way back in to zero and it'll come back out to minus five so again you can customize that to be what you want essentially what i'm doing so i've just got my button i'm moving in i'm moving it out so you can do that like so as well go back to the event graph here and this set relative location we're going to plug that into the update of the timeline there so basically every single time the timeline updates so it's going through these keyframes it's going to be moving the button static mesh between these two locations like that and that is that done so i'm going to do is i'm going to select these i'm going to right click on them i'm going to collapse nodes so it's in a collapsed graph like that just to keep it nice and organized and this means we can also use it later on as well if we want to so if we want to push the button again on another piece of code in here we can do that so what i'm going to do of this is i'm just going to rename this to be button press animation or anything like that compile and save that and you can see this is just a collapsed graph here we can't do anything with it so what we want to do is we want to hit a plus input there i'm going to call this play from start and i'm going to change it from a boolean to be an execution pin like so i'll tell you why i'm doing these names in a second the output i'm going to hit new parameter again i'm going to call this one finished making sure that it's still an execution pin like so compile and save that and now what we can do is we can plug the exit into the play from start and be finished we'll go into the rest of the code for opening the door so if we double click this again to open it up back up you can see we have our input and outputs in here like so i named this play from start and finished so play from start wants to go in the play from start and finished let's go into the finished like so and so now this will work when it fires off this collapsed graph it will input so we'll go in here into the timeline move the button when it's finished the timeline it will go into this output here so this is going to work perfectly for us so compile save and we can go back to the event graph like so and now it will do that before opening the door so like i say we want to open the door so let's do that one now so after this that would be finished so once it's finished doing that we're going to get a flip-flop which basically just toggles between two values and in this example it's going to be between opening the door and closing the door then what we're going to do is we're going to come out of a again i'm going to get another timeline so add timeline like so and i'm going to name this one door timeline like so i'm going to leave that in the play and then b will go in reverse like so so basically the first time we press it it will be a so it will play this so we'll open the door and then the next line will be b so we'll reverse it so it's going to close the door so that's going to work perfectly like so play will be open reverse will be closed so we don't need to make two different ones we just want to reverse it as obviously closing it it's just the opposite of opening it so then again we're going to do is we're going to double click this to open it up and this time i'm going to change the length to be two seconds so i want this to take a little bit longer obviously and i want it to take two seconds to fully open the door again we're going to add a float track i'm just going to call this one door track this time in here i'm going to right click add key like we did last time time is zero as it's at the start value is also a zero and then we're going to right click add key time of two this time as it's at the very end of the timeline and a value of one so it's completed so it was going to take two seconds to go from value zero to value one so basically close to open or if it was the first open to close so we compile and close that timeline like so out of this door track here we're gonna come out and again get a lap but this time it's just going to be a normal up so it'll up float and it's not going to go in a the door track will go into alpha like so so it's getting the position between a and b depending on the timeline value a we're going to right click promote to variable and call it close angle or door close angle right click b promote to variable and call this door open angle so as you can tell we're going to be doing this based on the angle of the open and closed positions of our door closed i want to just keep a zero so we can compile save we can change the value i'll leave that as zero so i'll see if it's closed the angle be zero you're open i'm going to set this to be 100. now to tell what you want this to be you can just select it and then you can just basically move it on this head like so so i want it to be 100 meaning that is the open position you can have this as minus 100 so it's that way if it's 90 have it is 60 or 160 sorry 80 any value you want you just set it as that let's have a compile go back to event graph so i've set that to be 100. what we're going to do is again we're going to get a reference to our door static mesh here drag out of this and we're going to set relative rotation this time as we want to rotate the door so set relative rotation like that again go into the update of the timeline the new rotation you can see that's a rotator and the return value of the lap is a float now the reason we've done that is we're going to right click the new rotation split structure pin so now you see we have a float of x y and z we're going to reflect the return value into the z as we only want to be rotating the door on the z value meaning we only want to rotate it like this not like this and this so that's just going to work a lot better for us so we compile event graph you see that is now going to work that is going to open and close our door so this is the code finished so i'm going to do is i'm just going to comment some of this so i select all this hit c to comment i'm just going to call this hit e to open door if player is close enough just a very brief note so we know what it does comment this here i'm going to do push button so again select it and hit c and then this one i'm going to comment as open and close door like so i'll move it out a bit like that compile and save and now like i say this should be done so if we minimize it close this you see we already have our door in here again we can change the location of the button like that what i'm going to do then is i'm going to hit play to test this i'm going to first person if we walk up to the door we hit e the button went in went back out and the door opened if i hit it again the bottom went in and out and now the door's closing so you can see this is working perfectly actually there's one thing as well that i'm going to change and that is i've just remembered actually if i move this button so if i move this be over on this side instead i have a feeling it's going to teleport to the other side because of how i set up the variable so if i do that you see it did do that so i'm going to fix that right now as i know why it's doing it i just want to change it go back into our bottom blueprint here and then you can see in our button press animation if we open that up where we have our button in what we want to do is we don't even need that value so we can just delete that variable there like so what i'm going to do is i'm just going to come out with the button out i'm going to get a vector minus a vector i'm just going to minus 5 on the x there so just put in the value of 5 plugging that into the b so basically what this is going to do is it's going to get the value of the button out position take 5 off of the x so it's going to go back in towards the door and then put that into b so it's going to be our open position so now we should see if i leave that there this should still work perfectly it's going to go in and out open the door and then if i move it over to this side over here should see this now still working as well on this side although no sorry that didn't work either because the button out is also actually not the dynamic variable so we can do is button out is we can also delete that and we can just get button transform right click split structure pin and just get the location in a and a location into the top value of that minus there and now this should work so this should also be the dynamics section as well so if i leave that there hit play if i hit the button it's going to go in and out as it is there and if i move it back over to this side where it was originally if i hit play this should now still also work as you see there we now have the dynamic button still working like so so you see this works perfectly so i think that'll be if this video is we've done everything we wanted to do we've created a button in which it has an animation of going in and out and once that is played it will then open and close this door and we can also move the button to be aware if we like on each reference to this door as well so thanks so much for watching i hope you enjoyed it and i hope you find it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one
Info
Channel: Matt Aspland
Views: 23,035
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, games design, ue5, unreal engine 5, button, door, use, open door, ue4 door, ue4 button, press, push, animation, animated, open a door using a button, using, pressing, by, first, redstone, minecraft, open, close, use a button, how to use a button, push a button, pull, timeline, anim, timelines, anything, multiple, times, sound, button push, pushing, animations, moving, in, and, out, back, interact
Id: PvnSBaYPhyM
Channel Id: undefined
Length: 18min 4sec (1084 seconds)
Published: Mon Dec 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.