How to make a grappling hook in UE4
Author: Ian Wilson
This tutorial is not suitable for a beginner user of Unreal Engine and assumes you have solid knowledge of the basics.
This tutorial will go through a method for setting up a grappling hook in UE4, I'll try and write it so you could follow the main principles even if you aren't working with Unreal. I'm not sure if this is the perfect method for doing this but it works and feels pretty good so I hope it can help you get a grappling hooking going in your game too. This tutorial assumes you have a basic knowledge of Unreal.
There is a link at the bottom with the full project files.
First thing you are going to want to do is look for points where you can grapple. So after doing a check to make sure the player isn't currently grappling we jump into a function I called 'LookForGrappleLocation'. Personally I want the player to be able to swing on any stationary geometry so I look for static and dynamic objects. You could also set this up with a custom object type so you can only swing on specific points.
The way we look for a point is by using a line trace starting at the camera and end at a point straight in front of where they are looking (at a distance set in the grapple trace distance, I used 1500 here). If this line trace fails we then do a sphere trace to see if the player was aiming just off from where they intended to go.
If one of these has a successful hit then we want to setup the player ready to start swinging. I do this in a function I called 'ActivateGrapple'.
The first thing we want to do in this function is set 'IsGrappling' to true so the player can't do any of this again while the grapple is active. Next we set a variable called 'MaxGrappleDistance' to be the distance from the player to the hit point (this is used later on in the swing where I'll explain what we use it for).
After this I set up the cable and the hook by moving the hook to the grapple hit location and then making the cable and hook visible.
Final thing in here is setting the gravity scale to 2 which I do to just make the swinging feel a bit better (it's also a good idea to give the player a decent amount of in air control in the character movement component, I've set it at .5). Then just set the boolean 'IsSwinging' to be true.
Now we have done all the set up lets get to the swinging. I created a swinging function and run it every frame on the event tick.
Inside the function I check to see if we should be swinging first and then for the cable component I adjust the length of it to match the players distance from the hook.
Now we should put in the code for swinging, this block here is the main bit of maths that does that. By doing a dot product of the players velocity with the direction to the hook we can map the velocity in that direction to maintain the players distance to that point.
Unfortunately this doesn't do a perfect job of maintaining the distance to the hook and as you swing around you will find that you slowly get closer to the floor. To fix this I do a small correction by adding some additional force in the direction of the hook. As you can see this block of code is a little bit more complicated than that. This is because I'm actually adding the force towards the location the player should be at if they hadn't drifted away. I do this by getting the max grapple distance and finding the difference between your current distance and that.
I wanted to add one more additional correction to get the grappling hook working like I want. Currently if you walk on the floor and attach your hook you will find you are unable to walk towards the hook. This next bit of code is designed to solve that. There are two things we do here, first is this block of code which updates the max grapple distance if you walk closer to the hook. It does this by checking your current distance to the hook and updates the ‘MaxGrappleDistance’ if that’s shorter than it’s old value.
The second is this block of code which compares the direction you are walking in to the direction of the player to the hook. If they are the same then we don't do the swing code so it will no longer block you.
Now what happens when you jump? Well there are two things I do depending if the player is on the ground or not. If they are then we hook into that correction above and briefly don't do the swing code to make sure it doesn't hinder the players jump. If the player is in the air we can run a function I've called 'ReleaseGrapple' and give the player a little boost out of the swing.
The 'ReleaseGrapple' function is also called if the player presses the grapple button again while they are swinging. In this function we simply reset the player back to their default state and hide the grappling cable and hook.
As a final little touch I've made it so using the right mouse button you can pull yourself towards the hook. This hooks into the grapple distance code I've already written. On the tick event we run the ‘PullIn’ Function after the swing function and it just reduces the ‘MaxGrappleDistance’ every tick by a fixed amount (I've set it to 10 here).
That's it then I hope this helped you get going, if you have any questions feel free to leave the below or on our discord: discord.gg/UYdJd4a
The full project files can be found here: drive.google.com