How To Make A Grappling Hook - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another engine 4 tutorial in today's video i'm showing you how to create a grappling hook system in your game so in this we're going to be able to shoot a grappling hook gun wherever it lands it will take the player to that location as well so let me show you what it's going to look like now so once we get in you can see we can walk around if we press left mouse button it's going to shoot it will take us over there and then we'll fall like so after we've got there we shoot again it's going to take us all the way over there and once we get there we'll drop down like so you can see it actually shoots a little grappling hook as well in the direction that we're firing in and when we shoot we can't shoot multiple ones off at the same time we can only do that once we get there and collect this up again and you can see if we're halfway through and if we press right mouse button it's going to stop us as well so we can cancel this like so as well so we're going to create this today so let me delete this code and i'll show you how we've done this again at the very end i press right mouse button which is why we felt before so our first step is going to be to import this grappling hook that we're going to use so for me i put that in content grapple hook here and it is simply just this little model here now it's very small by default so we'll scale it up later but we're going to be using this model here i'll leave a download link to this in the description down below to close that that's what we're using the next step is we want to open up our first bus and projectile or basically just the bullet the projectile for your grappling hook so for me that's in first person bp blueprints first person projectile or what you can do is just simply right click blueprint class and get actor and what i think i'll do in actual fact is i will create it from start just so that even if you're not using the first person project you know how to do this as well so i'm going to right click and get a blueprint class i'm going to get an actor like so i'm just gonna name this one grapple hook bp like so and open it up straight away in here what i'm going to do is i want to add a component we want to add a static mesh like so i'm just going to name this one grapple or hook whatever you want to name it i'm just gonna change the static mesh to be our grapple hook here so mine's called grappling hook like that and i'm gonna scale this up by 15 so we have something about this size you can make this whatever size you want but this is what i found to be good when i was making this code earlier what we also want to do is we want to rotate this by minus 90 on the y like so so that when we spawn it it's going to be facing forwards so it's like we've shot it out of the gun next we're going to do something to deselect that add a component and we want to add a box collision and this is obviously going to be the collision for our grappling hook so we just want to scale this accordingly so it fits perfectly around our grappling hook like so so i think something along these lines is quite good so we don't need to change it too much just make it a little bit skinnier and i think that's a good size again customize this to get it perfect for you what we want to do next is we want to just simply make this box collision the default scene root so it's going to drag and drop that on d14 root there to make it the new root and then that works perfectly like so so the collision is the root which is good practice for any projectile that you make then we just want to mess about with some of the collision settings for this so what we're going to do is we're going to scroll down on the right until we find collision we're going to leave simulate generate hit events false leave generate overlap events true can character step upon yes collision presets we're going to change to be custom and then we're going to change the object type to be projectile like so and then we want to do is want to change some of these i'll just either say block overlap ignore i'll be going down the list like so starting with visibility we've got block block block block ignore block block block block so basically all of them are blocked apart from pawn which is ignore like so and now this should work perfectly for us we compile and save that now go over to the event graph and start setting up some of this code so what we want to do is we want to delete these three events here and instead we want to right click and get event hit like so and so this is going to fire off when this grappling hook lands or when it hits something so after this what we're going to do is come off of that i'm going to cast to our character which for me is the first person character but for you this could be third first well if you've named it the object is obviously going to be get player character like so what we want to do is then open up our first person character and create some variables so let's do that now so let's minimize this and just open up first person character there in here like i say i'm going to create some variables so i'm going to hit the plus variable there i'm going to name this one grapple start lock like that which basically means the starting location for a grappling hook so you can name that whatever you like to make it make more sense for you so starting location anything like that but grapple start lock makes sense for me i'm going to change that from a boolean to a vector like so so it's a location i'm going to hit plus variable again again this is a location so we can keep it as vector i'm going to name this one grapple end lock so we're having the start and end locations for the grappling hook then we compile save that and we can go back to our projectile for our grapple hook here as we can then now access this so we've come out of as first place in character we can set grapple hook or grapple start lock plugging that into there like so and the start of it just wants to be where the player is now so we're going to come out as fast button character get actor location like so plugging a return value into there like that we'll come out with a star passing character again and we'll set grapple end lock and this one is just going to be the location off of this event here so we're going to get hit location plugging that in there which basically means wherever the grapple hook ended wherever it hit wherever it collided that is where we want to go to so we're going to use that there i'm just going to double click on these to get some rubric nodes to keep it looking nice and neat and tidy and organized so i think that looks good like so and then we'll come back into this code later to finish it off so i'm going to compile save and go back to our first person character in here we want to do is want to find our spawn projectile code that we have here or if you don't have this what you can do instead is just get this fire or left mouse button come out of that go into a spawn actor from class in your spawn actor from class you want to set the class as the grappling hook projectile we just made so this is if you have this code as well what we're going to do is change this to be our grapple hook bp there like so compile that and i'm also just going to disconnect this playstyle location but you can keep that on if you want if you'd like a sound but i personally don't so i'm going to get rid of that like so then i'm going to go back to the start of this where we have the input action fire and what i'm going to do is i'm going to hold down b and left click to get a branch plugging that in there like so so when it comes out of the fire it's going to go into the branch the condition of this branch is going to be a new boolean we're going to make so hit the plus variable there and i'm going to call this one is grappling question mark like so i'm going to change it from a vector to a boolean so it's a true or false value and that's going to go into the condition there like that the false of this will then set is graphing to be true and that is going to go into this play montage there or just for you the spawn actor so the only reason i've got this montage play here is just so it then plays the animation for firing a gun as well and true we don't want to do anything as if we are grappling we don't want to grapple again now this system does work for that but i feel like it's just more realistic if we don't so i think that's good for me like so the next step we want to take is you want to make sure that this is always going to be facing forwards so no matter which direction we're facing when we shoot this grappling hook it will always face forwards as if it's just been shot out of the gun so let's do that now so what we're going to do is we're going to come out the return value of this spawn actor here and we're going to set actor rotation like so that execution should go in there like that what i'm going to do here is i'm going to right click new rotation and split the structure pin and i'm going to drag and drop a reference to our capsule component up here and out of that i'm going to get world rotation so the rotation of our player i'm going to right click that return value and split the structure pin as well and the set is going to simply go into the z of the new rotation like so so it's going to get the direction the player is facing and set that to be the direction the grappling hook should face as well so now this should work like that as well so let me actually comment some of this code as well so i select that hit c to comment it and i'll call this one grapple hook always faces forwards like so what we can do now is we can start setting up to actually move the player on the scrapple hook so we'll come down underneath this code right click and get a custom event so add custom event like so i'm just going to call this one move grapple or move player or fire graph or anything along the sides which makes sense to you but for me i want move grapple under this i'm going to get the grapple end location so get grapple end lock there and then get grapple start lock underneath it as well so get grapple end get grapple start like so uh out of the end i'm gonna get a vector minus a vector plugging the bottom value as the start and then out of that we're going to get a vector length so what we're doing is we're just basically getting the distance between the player and where the grappler landed so where they want to go so what we're doing is we're basically getting the distance between the end of the grapple to where it landed and where the player is currently so essentially finding the distance in which the player needs to travel out of this what i'm going to do is we're going to get a float divided by a float we're going to use a value which i found earlier while creating this code which was 1200 i'll tell you why we're doing that in a second out of this we're going to go into a clamp the clamp float with the minimum as one maximum is five so now what we're doing is we're doing this to set the length of how long it should take the player to travel along the grappling hook so the vector length will probably be something like 500 to 2 000 anything like that we're gonna divide it by 1200 to get what we want which is either one or five and this is basically the time in seconds that should take for the player to travel so we're dividing by 1200 so it goes from a very big number down to a much smaller number and the minimum is one maximum is five because i want it to take the player a minimum of a second to travel in a maximum of five seconds now you don't need to do this clamp you can just put it straight in if you want but i just want to be like this so it doesn't take too long and it's not too quick so again this is finding the length in seconds for how long it should take the player to travel off of this return value we're going to get another float divided by a float except this is going to go into the bottom value and the top value will be one so we get 1 divided by this value here of the time in seconds and why we're doing this is so we can then change the actual length for how long it should take so this is finding out the length this is changing the length so what we need to do now is we need to right click and add a timeline so we're going to be using a timeline to move the player so add timeline like so and i'll name this one move grapple tl for timeline like so i won't connect that to anything just yet but i'll double click on it to open it up the length we want to set to one and make sure you set it as one as this is how we're going to be changing the time later on as it needs to be one for it to work i'm going to add a float track i'll just call this one grapple track you can know it's absolutely whatever you like in here i'm going to right click add a key to curve float time zero value also zero right click add another keyframe time one value one so it goes from the very beginning to the very end of the code compile and then close that timeline like so just before this want to do is you want to open the components tab on the left here to drag in our timeline like so so move grapple tl get like that so that's a reference to our timeline and out of this want to do is we want to set the play rate like so plugging that into play from start on the timeline and the start of this going into the move grapple custom event there the new rate of this will be the return value for this one divided by the value we made earlier put that in there like so so like i say this is determining how long it should take the player to travel so i'm going to select all this history to comment it and i'll call this one set max time for grapple min time one sec max three so hopefully that makes sense what we're doing is we're getting the end location and the start location and getting the length of that so how far the player wants to travel and then we're going to be getting that in seconds so the length but putting it into seconds so how long we want it to take and then putting that into something between zero and one and then putting that into a decimal to set the play rate of how fast the timeline should play through it to get that working perfectly for us like so so after this we want to actually move the player so what we're going to do is out of the update we're going to set actor location which is obviously going to move the player it's going to set that location nearly the target itself we're going to come out with the grapple track there i'm going to get a lerp vector the return value going to the new location at that set x location there a is going to be grapple start lock b is going to be grapple end lock so it's going from the start location to the end location so let's make that end like so it's going from start to end location smoothly with how long it should take which we're determining there off of finished want to do is want to get a reference to our character movement here so get character movement we want to simply set movement mode so i'm doing this because i actually forgot to do a step a second ago so let me just plug that into the finish there set movement mode and we're going to be setting this to walking like so so we're going back to walking instead of traveling on the grapple hook so i wanted to do just before this timeline here so we move this forward a bit i want to drag and drop a reference to our character movement again come out that we want to disable movement like so plugging that in there like that go back into the play from start there so we're disabling the movement so the player can't move around while they're traveling as because then that would kind of mess it up they'll move off course it will break and you can't really move on a grapple hook anyway so that just makes it more realistic and again when we finish we want to set the movement mode back to walking so player can move around again and then they start falling once they hit a vertical surface or anything like that off of this we want to set isgrappling to false as we've now obviously stopped grappling so that should work perfectly like so now what we're going to set up so actually before i do that sorry i will select this and hit c to comment it i'll just do comment this one move player like so so now underneath this we're going to set up hitting the right mouse button to cancel with the grapple so underneath this i'm going to right click i'm going to get right mouse button where you can set up an action mapping for that as well if you like to do that it's edit project settings once this loads go down to input hit the plus action mapping there and i'll name this one cancel grapple you can call this whatever you like and then i'm going to set this to be right mouse button like so and maybe i'll also set it to be the backspace as well but again put this to whatever you like and the benefit of doing action mappings is like you see there we can set up multiple keys we set it up for different consoles and we can also set up key bindings so once you now we can close that delete that event there and now if we right click we can search for what we just named it so for me that was cancel grapple like so you can see we have it there off of pressed wanting to do is hold down b left click to get a branch plug in there like so and we're going to get another reference to the character movement out of this we're going to get movement mode and now with that we're going to get an equal equal enem like so with that going to the condition of the branch there and we'll leave that as not so let's move that up there and the reason we're doing this is because we only want to be able to cancel the grapple and fire off this code if our movement mode is none so essentially we've disabled the movement there and that will work for us like so and because i say we only want to do it if that's true we're going to come off of true like so so we're going to do is get another reference to the timeline so components move grapple tl get that out of this we're going to simply just get stop like so plugging into true that will stop the timeline so it will stop the player from moving and because we're stopping it this is why we want play from start not play and also because when this ends we haven't reversed it so we'll stay at the end so we want play from start after this we again want to set the movement mode so we'll come at the character movement again and set movement mode like so plugging that in there and putting this back as walking like so so the player is now walking again and then we also want to again set his grappling to false as we've stopped grappling like so and this is all to allow us to re-grapple up here again like so to fire it off once again and there's one final step so i'll actually comment this again so select it hit c to comment it i'll just call this one cancel grapple like so compile and save that and now we'll go back over to our grapple hook bp here for our projectile and what we're going to do in here is actually call the event to move our grappling hook so we created this custom event here we want to call this so we'll come out of our set grapple ends lock there so come out the as double passing character again i'll come out this room node there and i'm going to call function move grapple like so plugging that in there like that i'll rewrite node that again by double clicking on it so that is then going to call that function to move the player but what i want to do now is i also want to run a check to see when the player is at the location so i can then just delete the actor destroy the actor of this grapple hook bp here so it then gets rid of it and so it looks like the players picked it back up so it's very simple to do all we want to do is just hold down d left click to get a delay plug that in there like so and i'll just set this to 0.1 seconds off of this i'm going to hold down b left click to get a branch plugging that in there like that and the condition of this is going to again see what the character's movement is so we're going to come out with the asda passing character again and get the character movement so should we all go at the bottom there get character movement out of that we're going to get movement mode and then out of that we're going to get a not equal to anim so that's an exclamation point and an equals for not equal anim and leave that as none plugging in a condition there so if the player's movement mode is anything but non so they have movement enabled we're going to come up with this so true we'll then just go into a simple destroy actor to get rid of it and false we'll just go back into this delay here to keep checking it and double click that to root node again to keep it looking neat and tidy so this is just going to continually loop this to check to see if the player is there or not and then when it is it will just delete this code here or delete the grapple to make it look like the players picked it up and obviously because they finished the player can then fire off again so it's compile save and that should be this code done so i'll give you a quick run through again so what's going to happen is when the player presses left mouse button to fire or whatever you have it's going to see if we're already grappling if we're not it will say that we are fire off the grapple hook and make sure it's rotating to face the direction the player is facing and then once it's fired off when it hits something what it's going to do is just set the grapple start location to where the player is currently sending that end location to where the graph will hit then it's going to move the player when they're moving which we'll look into in a second it's going to continually check to see when they stop moving when they stop moving the graph will get destroyed to move the player all we're simply doing is just getting the distance between the player and where they want to go getting that to seconds to see how long it should take us and then it's going to move them by setting a location for how long we wanted it to take once they're finished we'll go back to walking and we'll set its grappling to false so that we can then do it again again we can also hit right mouse button to cancel this at any time while we are grappling so let's minimize and let's hit play to test this to see if it's working for us so what i'm going to do is i'm going to hit left mouse button you see we fired it off it just stopped straight away like so so let's have a look at why that happened although sorry there's one actually big thing which i missed out which i forgot about because when i was making the code i just used the first person projectile so if i open it up i just use this here so one thing i forgot to add is actually add the projectile movement to make this projectile so we created all the code for it but we didn't make it projectile so it's going to spawn in but it's not going to fire off so let's do that now let's see if we can actually just duplicate this over to make it quicker for us so we go back to the graphical hook bp we can indeed so if you don't have this just simply add components and get projectile movement there you can see we have it here now working perfectly like so and one thing you also want to make sure that you have is should bounce unticked like so so now if we hit compile and see if we can test this out once again hopefully this should now work so if we try this we can move around like normal if we press left click the grappling hook is going to fire off we go over here and now you can see that we're actually stuck on this wall now that doesn't happen very often and it doesn't always happen and you can see it fixed itself there however we obviously don't want that and after a couple minutes of pulling my hair out trying to figure out why this is happening because the code is the exact same as when i did it before nothing is different but it worked before after a lot of testing it doesn't nail so after some debugging i figured out the problem so all i need to do is this delay here i just need to lower that so instead of 0.1 i'm going to set it to 0.0001 because what it's doing is it's going back out of non for just a fraction of a second point one of a delay isn't fast enough to detect that again not sure why it's an issue now where it wasn't earlier but this will now fix that issue so this in the grapple hook bp sorry we have the delay going in here this loop which needs to be quicker so if we make that smaller compile safe we should now see this working a lot better so now let's do the proper test again if i left click it's going to fire up over there we go over and then we fall down if i fire it all the way over there and if i hit right mouse button it's going to cancel and we'll go back here and we can fire off again go over here we'll fall we fire off again and this is now working perfectly for us we had a slight hiccup earlier but we managed to resolve that issue fairly quickly fairly easily just took a little bit of debugging on my end because for some reason something went wrong again not sure why but it did but at least we have it fixed now so i think that bit of this video we've done everything we want to do we've created this grapple hook in which we can hit left mouse button to fire off we will go to the location where it landed and fall like so and we can use this multiple times we can only use it once when it's been fired when we pick it up we fire again and if we hit right mouse button it will cancel it and drop us wherever we are when we press it 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 thanks so much for watching and i'll see you in the next one you
Info
Channel: Matt Aspland
Views: 22,226
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, grappling hook, grapple, grappling, hook, fly, move, ue4 grappling hook, spider man, shoot, gun, ue4 grapple, timeline, cancel, system, lets make, widowmaker, widow, maker, grapple hook, overwatch, swing, basic, advanced, beginner, rope, physics, make, create, batman, bat, man, arkham, level, fps, first person
Id: Mu4oJvirSGc
Channel Id: undefined
Length: 23min 10sec (1390 seconds)
Published: Thu Jan 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.