Welcome back to the blog. Today I will be discussing how I went about creating a gun in unreal engine 4 that can shoot via line trace.
Line Tracing in UE4 can also be referred to as ray casting. A Line Trace is a way to detect collision, for example, the line trace can shoot out of a gun and when it hits the first object, you can program what happens after that like a particle effect or destroy and actor. In our case, the line trace is meant to function with the gun but we will be shooting the actual line trace out from the player camera to make it more accurate. We will need to do some simple math to make this work.
To accomplish my simple first person shooter gun, we need to get the world position of the player camera. This can be done by the nodes, get FirstPersonCamera and GetWorldLocation. Once we get the location of the player camera, we can spawn a line trace. The Line Trace By Channel node will need a start point and an end point. We already have the start point taken care of because of our first person camera vector.
The way I defined the endpoint here is taking the forward facing direction of the controller, in this case the character and multiply that vector by some arbitrary amount for now. This is essentially the range of your gun. I have chosen to multiply it by 2500. Then we need to add that value with the value of the camera we just got before, then plug that into the end pin.
The rest is relatively simple. From the Out Hit pin on the LineTraceByChannel node, we connect that to a new node called Break Hit Result in the hit pin. This node will let us do a variety of different things when the line trace intersects with something. In this case we are concerned with the location an hit actor pins. From the location pin we can spawn one of the default explosion particle effects that unreal engine has and trigger the CastToCubeBP node (more on that soon). From the hit actor pin, we can give the CastToCubeBP node the appropriate object.
I have made new cubes in this project, very similar to the ones that are provided in the default first person project provided by UE4, but they have a custom event where the actor gets destroyed. This event I have named “Die”. Now we have a gun that will destroy the cubes when the line trace intersects with them.
Right now we can spawn the line trace and it does destroy the boxes, but there is a slight problem. This next picture is of me shooting the gun while only moving left to right, paying attention to the red lines which are the line traces, can you notice anything wrong?
The problem here is that the line trace is shooting out above our crosshair. The crosshair here is the default one that UE4 provides in it’s first person template. I am not sure if the crosshair is below the center of the screen or if it is truly the crosshair that is above the middle but either way, there is a fix.
Since we have the vector of our camera, we can actually manipulate it by each coordinate. Luckily UE4 has nodes that can help us break, manipulate and make vectors. By my calculations, the line trace should be in the center of the screen if we subtract 100 units from the Z coordinates.
Now our line trace should be lined up with our crosshair.
To show the gun in acition. Here is a video of it functioning. It’s important to keep in mind only some of these cubes will be destroyed because some cubes are still the UE4 default cubes that don’t have the “Die” event that I made. Just for fun, I also multiplied the velocity of the default ball projectile by 2000, so you will see some boxes go flying at insane speeds.
That’s all for the post! I think my next step will be combining the 2 systems I have been blogging about. I am planning on making a super simple game, just for the purposes of my learning, where the player has to go around a level and collect/read all the notes to win the game, but shoot the boxes that are in their way.
Thanks for reading, I hope to see you in the next post.
Saarim