Traces & Collision Channels
A trace asks the physics scene a question: "if I fire a ray (or sweep a sphere) from here to there, what does it hit?". Hitscan weapons, interaction prompts, ground checks, line-of-sight tests and camera collision are all traces. Which objects a trace can hit is decided by collision channels and responses.
Channels, object types and responses
Every primitive component has:
- an Object Type: what it is (
WorldStatic,WorldDynamic,Pawn,PhysicsBody,Vehicle,Destructible, plus your own); - a response to every channel: Ignore, Overlap or Block.
A query uses either a trace channel (Visibility, Camera, plus your own, such as Weapon) or a list of object types. Two moving objects collide only if both block each other; if either side says Overlap (and neither ignores), they overlap; if either ignores, nothing happens. The least blocking response wins.
Custom channels are created in Project Settings → Engine → Collision: New Trace Channel (for queries such as Weapon) or New Object Channel (for new kinds of objects such as Projectile). They show up in C++ as ECC_GameTraceChannel1…18, in creation order, which you can see in Config/DefaultEngine.ini. Give them readable names in a header:
// ArenaCollision.h
#pragma once
#include "Engine/EngineTypes.h"
#define ECC_Weapon ECC_GameTraceChannel1 // trace channel "Weapon" (check DefaultEngine.ini)
#define ECC_Projectile ECC_GameTraceChannel2 // object channel "Projectile"Collision presets (profiles) bundle an object type and all responses under one name (BlockAll, OverlapAllDynamic, Pawn, CharacterMesh, your own ArenaPickup). Use presets instead of setting responses one by one on every component; changing a preset updates every user.
A hitscan line trace
#include "DrawDebugHelpers.h"
#include "Kismet/GameplayStatics.h"
#include "ArenaCollision.h"
void AArenaCharacter::FireHitscan()
{
FVector Start;
FRotator ViewRot;
Controller->GetPlayerViewPoint(Start, ViewRot); // from the camera, not the gun
const FVector End = Start + ViewRot.Vector() * 10000.f; // 100 m
FCollisionQueryParams Params(SCENE_QUERY_STAT(ArenaHitscan), /*bTraceComplex*/ false, /*IgnoreActor*/ this);
FHitResult Hit;
const bool bHit = GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Weapon, Params);
#if ENABLE_DRAW_DEBUG
DrawDebugLine(GetWorld(), Start, bHit ? Hit.ImpactPoint : End, bHit ? FColor::Red : FColor::Green, false, 1.f);
#endif
if (bHit && IsValid(Hit.GetActor()))
{
UGameplayStatics::ApplyPointDamage(Hit.GetActor(), 20.f, ViewRot.Vector(), Hit,
GetController(), this, UDamageType::StaticClass());
}
}The FHitResult carries what you need: ImpactPoint and ImpactNormal (for decals and effects), GetActor() and GetComponent(), BoneName (for headshots on skeletal meshes), PhysMaterial (surface type for footsteps and impact sounds, when you pass Params.bReturnPhysicalMaterial = true), Distance.
The trace family
| Function | Question |
|---|---|
LineTraceSingleByChannel / …ByObjectType / …ByProfile |
first blocking hit along a ray |
LineTraceMultiByChannel |
all overlaps up to and including the first block |
SweepSingleByChannel (with FCollisionShape::MakeSphere(r) / capsule / box) |
a moving shape: melee swings, thick bullets, "will I fit?" |
OverlapMultiByChannel |
everything inside a shape right now: explosions, area auras |
UKismetSystemLibrary::LineTraceSingle etc. |
Blueprint-friendly versions with built-in debug drawing |
Multi traces return overlaps until the first block: a trace channel that overlaps glass and blocks walls returns every pane of glass in front of the wall plus the wall.
Async traces (AsyncLineTraceByChannel) return the result next frame, which is useful for hundreds of AI line-of-sight checks.
A model of the response matrix
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
enum Response { Ignore = 0, Overlap = 1, Block = 2 };
const char* Name[] = {"ignore", "overlap", "BLOCK"};
struct Primitive { std::string Name, ObjectType; std::map<std::string, Response> Responses; double Distance; };
// Two objects: the weaker response wins.
Response Between(const Primitive& a, const Primitive& b) {
return std::min(a.Responses.at(b.ObjectType), b.Responses.at(a.ObjectType));
}
int main() {
std::map<std::string, Response> pawnR = {{"WorldStatic", Block}, {"Pawn", Block}, {"Pickup", Overlap}, {"Weapon", Block}};
std::vector<Primitive> world = {
{"Glass", "WorldStatic", {{"Pawn", Block}, {"Pickup", Ignore}, {"Weapon", Overlap}}, 300},
{"Enemy", "Pawn", pawnR, 600},
{"Wall", "WorldStatic", {{"Pawn", Block}, {"Pickup", Ignore}, {"Weapon", Block}}, 900},
{"Coin", "Pickup", {{"Pawn", Overlap}, {"WorldStatic", Ignore}, {"Weapon", Ignore}}, 450},
};
Primitive player{"Player", "Pawn", pawnR, 0};
std::cout << "Player vs each object:\n";
for (const auto& p : world) std::cout << " " << p.Name << ": " << Name[Between(player, p)] << "\n";
std::cout << "LineTraceMulti on channel Weapon (sorted by distance):\n";
std::vector<const Primitive*> sorted;
for (const auto& p : world) sorted.push_back(&p);
std::sort(sorted.begin(), sorted.end(), [](auto a, auto b) { return a->Distance < b->Distance; });
for (const Primitive* p : sorted) {
Response r = p->Responses.at("Weapon");
if (r == Ignore) continue;
std::cout << " " << p->Name << " at " << p->Distance << " (" << Name[r] << ")\n";
if (r == Block) break; // multi traces stop at the first block
}
}Player vs each object:
Glass: BLOCK
Enemy: BLOCK
Wall: BLOCK
Coin: overlap
LineTraceMulti on channel Weapon (sorted by distance):
Glass at 300 (overlap)
Enemy at 600 (BLOCK)
The glass let the weapon trace through as an overlap, the coin was ignored entirely, and the enemy stopped the trace, so the wall behind it was never reported.
Test yourself
Object A blocks Pawn; the Pawn overlaps A's object type. What happens when they meet?
- They block
- They overlap
- Nothing
- It depends on mass
What does LineTraceMultiByChannel return?
- Every object along the ray
- Overlapping hits up to and including the first blocking hit
- Only blocking hits
- Only the last hit
A custom trace channel named Weapon appears in C++ as…
- ECC_Weapon automatically
- One of ECC_GameTraceChannel1..18, in creation order
- A string
- ECC_Visibility