# Line Tracing Help, Thank you!

**URL:** <https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 17, 2014, 1:26am UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178 "2014-06-17T01:26:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_James\_G](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mark\_James\_G](https://forums.unrealengine.com/u/Mark_James_G)\
**Post date:** [June 17, 2014, 1:26am UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178/1 "2014-06-17T01:26:45Z")

</div>

Hey Community, just popping in with an important question… Ray Casting or as it’s called here; Line Tracing. Can someone maybe explain it a little better to me? I’d be very grateful. If I understand it correctly this means it’s basically “shooting” from a start position to an end position, an insanely accurate ray. If it collides with whatever it is you wanted then you can have it return that information back to you.

I’ve noticed a bunch of threads and a wiki page regarding it, all of which I have favorited for experimentation and use but alas i’m still stuck.

> **[\[Tutorial\] Detecting the slope on which your character is standing](https://forums.unrealengine.com/showthread.php?7512-Tutorial-Detecting-the-slope-on-which-your-character-is-standing)**
>
> Today I needed to get the angle of the surface on which my character is standing on. It was surprisingly easy. FHitResult hit; auto c = FCollisionQueryParams(false); c.AddIgnoredActor(this); auto start = GetActorLocation(); auto end = start +...

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

[https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UWorld/LineTraceSingle/1/index.html](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UWorld/LineTraceSingle/1/index.html)

So let’s say for example I wanted to cast a ray at the ground beneath my character and grab it’s current world position along with what material it’s currently using then how would I go about grabbing that information from a new method? Assuming the ground is either BSP or Static Mesh, if they differ Line Tracing wise then explain the difference please.

Anyways, assuming I get Ray Tracing to work is there a way to actually visualize it? It’d probably help out to see WHERE the darned thing is going and would probably help with the params. 🆒

Thanks a bunch fellows!

P.S

I’m a C++ sort of fellow so i’d prefer the answers to be done via code as opposed to Blueprint. Thanks! Also there aren’t too many threads for something so important! Either i’m completely dumb or it isn’t asked all that often. So I think this thread will help others who are also curious about Line Tracing.

---

<div class="post-metadata">

**Author:** ![Alderbit](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Alderbit](https://forums.unrealengine.com/u/Alderbit)\
**Post date:** [June 17, 2014, 1:25pm UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178/2 "2014-06-17T13:25:51Z")

</div>

Hi there! Your understanding of ray tracing is correct. It detects geometry between two points in space and returns information about what was hit. Traces work just the same against meshes and BSP surfaces, but the ray will only collide with objects that are set to block the same collision channel specified in the trace call. Some information on collision filtering [can be found here](https://www.unrealengine.com/blog/collision-filtering).

Here is an example of finding the material below a character’s feet.

```auto

UMaterialInterface* MyPlayerCharacter::GetMaterialBelowFeet()
{
	auto StartTrace = GetActorLocation();
	auto EndTrace = StartTrace + FVector(0, 0, -1000);

	FCollisionQueryParams TraceParams(
		FName(TEXT("TraceGround")),	// tag name (for debugging)
		false, // trace against simple collision primitives only
		this // ignore this actor during the trace
	);

	FHitResult HitResult;
	UMaterialInterface* Material = NULL;
	if( GetWorld()->LineTraceSingle(HitResult, StartTrace, EndTrace, ECC_Visibility, TraceParams) )
	{
		if( HitResult.Component.IsValid() && HitResult.Component.Get()->GetNumMaterials() > 0 )
		{
			Material = HitResult.Component.Get()->GetMaterial(0);
		}
	}

	return Material;
}

```

I’m still a little fuzzy on the ins and outs of collision channels but I believe that using the ECC\_Visibility channel tells the ray to return a hit against anything visible in the level.

To get a visual representation of the trace you can use the AActor::DrawDebugLine() method.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Mark\_James\_G](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mark\_James\_G](https://forums.unrealengine.com/u/Mark_James_G)\
**Post date:** [June 17, 2014, 6:36pm UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178/3 "2014-06-17T18:36:58Z")

</div>

> [@Alderbit;78387](#):
>
> Hi there! Your understanding of ray tracing is correct. It detects geometry between two points in space and returns information about what was hit. Traces work just the same against meshes and BSP surfaces, but the ray will only collide with objects that are set to block the same collision channel specified in the trace call. Some information on collision filtering [can be found here](https://www.unrealengine.com/blog/collision-filtering).
> 
> Here is an example of finding the material below a character’s feet.
> 
> ```auto
> 
> UMaterialInterface* MyPlayerCharacter::GetMaterialBelowFeet()
> {
> auto StartTrace = GetActorLocation();
> auto EndTrace = StartTrace + FVector(0, 0, -1000);
> 
> FCollisionQueryParams TraceParams(
> FName(TEXT("TraceGround")),	// tag name (for debugging)
> false, // trace against simple collision primitives only
> this // ignore this actor during the trace
> );
> 
> FHitResult HitResult;
> UMaterialInterface* Material = NULL;
> if( GetWorld()->LineTraceSingle(HitResult, StartTrace, EndTrace, ECC_Visibility, TraceParams) )
> {
> if( HitResult.Component.IsValid() && HitResult.Component.Get()->GetNumMaterials() > 0 )
> {
> Material = HitResult.Component.Get()->GetMaterial(0);
> }
> }
> 
> return Material;
> }
> 
> ```
> 
> I’m still a little fuzzy on the ins and outs of collision channels but I believe that using the ECC\_Visibility channel tells the ray to return a hit against anything visible in the level.
> 
> To get a visual representation of the trace you can use the AActor::DrawDebugLine() method.
> 
> Hope this helps!

This most certainly helps thank you so much for your time. I’m sure others reading this will also find it just as useful. I’m wondering now… if anybody knows all of the collision channels could they provide a reference in this thread? That would be swell! (Who says swell these days?! Sheesh)

Also if anybody else has additional information they would like to share regarding Line Tracing this would probably be the best place to do so. It can serve as a great reference for other community members looking for something like this. (Myself included.)

Thanks.

---

<div class="post-metadata">

**Author:** ![Mark\_James\_G](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mark\_James\_G](https://forums.unrealengine.com/u/Mark_James_G)\
**Post date:** [June 20, 2014, 2:38am UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178/4 "2014-06-20T02:38:15Z")

</div>

Okay now that i’ve pretty much have Line Tracing figured out i’ll be working on a tutorial after a few more discoveries. I would once again advise that anybody else who has any information about Line Tracing that they would share that information here in this thread.

Line Tracing without a doubt is absolutely critical information!

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [June 21, 2014, 12:14pm UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178/5 "2014-06-21T12:14:18Z")

</div>

I have a question regarding this, I want to do a simple line trace to check if whatever I hit has a collision, if it does then return the end point, if it doesn’t then return nothing. Currently the only options seem to be visibility or camera, am I missing something?

---

<div class="post-metadata">

**Author:** ![Mark\_James\_G](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mark\_James\_G](https://forums.unrealengine.com/u/Mark_James_G)\
**Post date:** [June 21, 2014, 7:00pm UTC](https://forums.unrealengine.com/t/line-tracing-help-thank-you/6178/6 "2014-06-21T19:00:55Z")

</div>

> [@TheJamsh;81007](#):
>
> I have a question regarding this, I want to do a simple line trace to check if whatever I hit has a collision, if it does then return the end point, if it doesn’t then return nothing. Currently the only options seem to be visibility or camera, am I missing something?

> [@](#):
>
> “UE4 has a few ‘built in’ Trace Channels (Visibility, Camera) and Object Channels (WorldStatic, WorldDynamic, Pawn, PhysicsBody, Vehicle, Destructible), but you can easily add your own under Edit -\> Project Settings -\> Collision, though you are limited to 32 in total.”

> **[Collision Filtering in Unreal Engine 4](https://www.unrealengine.com/de/blog/collision-filtering)**
>
> Choosing what collides is obviously very important, but it can be tricky, and it’s a problem that we have spent quite a while discussing while developing UE4. The system we have can seem a little complex at first, but it is very powerful and...

Hopefully that helps. 🙂
