Unreal Engine Part 17: Weapon and impact setup

In chapter 17 of Rob Redman's comprehensive introduction to Unreal Engine, learn how to set up your weapons and create 'bullet' impacts...

Previous tutorials

Part 1: Setting up a new project
Part 2: Introducing materials and landscapes
Part 3: Adding foliage and rocks
Part 4: Particle smoke
Part 5: Introducing the skybox
Part 6: Setting up cameras and post-effects
Part 7: Teleporters
Part 8: Adding random assets
Part 9: Destruction
Part 10: Adding sound
Part 11: Linking audio to actions and objects
Part 12: Using the matinee tool
Part 13: Lighting
Part 14: Post effects
Part 15: Virtual reality
Part 16: VR motion controllers

We are coming to the end of the series now but that doesn't mean winding down. Oh no! In this chapter we are going to take a look at some tools for dealing with weapons. So many games are combat based that no series would be complete without covering at least some aspects of this, so in this chapter I will show you how to do something that is very useful. We are going to set up a system that allows you to trace a route from a weapon (or any object for that matter) to an impact, based on the orientation of that weapon. We can use this to create all manner of effects upon impact but for this we will set it so it creates a little smoke, or an explosion each time a projectile hits.

The main difference between this setup and the standard one is that there is no trajectory, so bullets won't fall but will move along a straight path until impact.

I'm using the same DraghornVale level, so you can follow along, so load it up and let's get started.

It's all in the blueprints

With your level loaded up, open up the Character Blueprint and head over to the event graph. This is where we do everything except testing, so make sure you are familiar with the various menus and workflows. If you've been following the series you should be pretty comfortable here.

The event graph is where the tracer is set up

The event graph is where the tracer is set up

Initial Tracer Setup

Find a blank area in your event graph, then right-click and choose a GetPlayerCameraManager node. Click and drag from the Return Value socket and add a GetActorLocation node. Go back to the same output and drag again, this time adding a GetForwardVector. These two nodes give us a position and orientation for the actor.

Nodes to get information for our actor

Nodes to get information for our actor

Set for use in the level

Now we need to set up a couple of nodes to allow for range in our level, so from the forward vector output, drag and add a vector * float node, with a value which should cover the longest range in your level, or the maximum range of your weapon, should you want to limit it. I've gone for 10k, so it more than reaches the extents of my map.

One node to rule them all

One node to rule them all

Connect and trace

Drag out from the multiply node and add a vector+vector node, which you then hook the actor location node into.

From this node drag again, this time adding a SingleLineTraceByChannel node. Swap the node's input from start to end, as it's the point the projectile impacts that we are interested in. Connect the actorLocation node to the start socket.

Our tracer is now set. All that's left to do is tell it what action to take at the impact point

Our tracer is now set. All that's left to do is tell it what action to take at the impact point

Hook up an action

Okay, so the scene is setup but we need to define what happens when we pull the trigger. Find the weapon area of the event graph and grab the nodes you see in the shot below and drag them near your tracer setup. We could have set this up manually but it's far quicker to make them ready made. Break their links to the existing weapon and connect them as shown.

Take the output from the Play Sound node and connect that to your Trace nodes action input.

All ready to run. Compile and test, with draw debug turned on to see results

All ready to run. Compile and test, with draw debug turned on to see results

Impact actions

Now for the real fun. From the Trace node's Out Hit socket drag out and choose a Break hit node. Drag out from its Location port and add an emitter. From the template menu choose a preset, or one of your own emitters if you have one (see an earlier chapter for setting up emitters/particles). This is an action, so you need to hook up the tracer's action output to the Emitter's action input. Hit compile, then playtest the level. If you're happy turn off the draw debug and you are good to go.

Top tip 1: Options galore

A particle emitter is just the start. You can add any action to the impact site, so try something wacky, like spawning an AI teammate, or a vortex to suck up enemies.

Messy impacts or simple puffs of smoke. All easy to do with a simple graph

Messy impacts or simple puffs of smoke. All easy to do with a simple graph

Related links

Part 1: Setting up a new project
Part 2: Introducing materials and landscapes
Part 3: Adding foliage and rocks
Part 4: Particle smoke
Part 5: Introducing the skybox
Part 6: Setting up cameras and post-effects
Part 7: Teleporters
Part 8: Adding random assets
Part 9: Destruction
Part 10: Adding sound
Part 11: Linking audio to actions and objects
Part 12: Using the matinee tool
Part 13: Lighting
Part 14: Post effects
Part 15: Virtual reality
Part 16: VR motion controllers
Buy a copy Unreal Game Engine
Download Unreal