Basic First Person Line Trace in Unreal Engine 4

In this guide we will explain what a Line Trace is an how to create a Basic First Person Line Trace in Unreal Engine 4

This guide builds upon the First Person Example template built into Unreal Engine 4.

How a Line Trace Works

A line trace is a way to check if an object is intersecting a line. The hit data is then broken down access more information such as location of hit, normal of hit. the actor that was hit and distance from hit.

An example of a line trace hitting a first person character

Different Types of Line Traces

“Line Trace for Objects” checks an intersection with a specific collision object type. (WorldStatic, WorldDynamic, Pawn, Ragdoll, PhysicsActor, etc)

Calculate a line trace by collision object type

“Line Trace by Channel” checks for an intersection with a specific line trace collision channel. (Visibility and Camera are the default options)

Calculate a line trace by trace channel

“Line Trace by Channel” checks for an intersection with a specific collision profile by name. (These can be viewed and created in the collision tab of the project settings)

Calculate a line trace by collision profile

How to Calculate the First Person Line Trace

All line traces have the same basic setup. The nodes below setup the vector maths required to check for any collisions wherever the camera is located and facing.

The universal setup for all first person line traces

Breaking down the Maths

Firstly you use the FirstPersonCamera variable connected to the GetWorldLocation node to begin the line trace from your character’s camera position.

Getting the world location of the first person camera

Next using the Get Controller node, get the controller rotation from the Get Control Rotation node. This will get the rotation of your camera and even works multiplayer games.

Getting the control rotation for multiplayer compatibility

Afterwards connect the Get Control Rotation node to the Get Forward Vector node. This will get the direction your camera is facing of your camera.

Multiply this Get Forward Vector value by 5000 (Multiple Vector by Float) to get a point in space from 0,0,0 that is 5000 units away facing the direction of the camera.
(This is the distance away from the camera to check fora collision. Change this to whichever distance you need for your project).

Finally add the Get World Location return value of your First Person Camera to the multiplied value. This gets the point in 3D space that you are looking at 5000 units in front of your camera.

Accessing the Hit Data

Connecting the return value to a branch only allows successful line traces to be accessed. Not including this branch will cause lots of console errors when accessing the hit result data.

To access this data click and drag from the Out Hit value and create a Break Hit Result node.

Accessing the data from a line trace

The Complete First Person Line Trace

This is one example of a complete first person line trace.

To use this system simply connect the execution pins to any event you want e.g. Event Tick or an event

An example of a complete first person line trace

Conclusion

Your project now is setup to use a basic First Person Line Trace in Unreal Engine 4!

Your project can now detect when objects are in front of the camera. Many use this system for weapons, interacting with objects such as doors, pickups and much more!

Further Reading:

Combine this line trace system with our easy AI movement guide to create computer controlled character that can interact with objects! https://couchlearn.com/easy-ai-movement-in-unreal-engine-4/

Unreal Engine’s documentation on single line traces: https://docs.unrealengine.com/en-US/Engine/Physics/Tracing/HowTo/SingleLineTraceByChannel/index.html

Unreal Engine’s full documentation on traces: https://docs.unrealengine.com/en-US/Engine/Physics/Tracing/Overview/index.html

Leave a Reply

Your email address will not be published. Required fields are marked *