Line Tracing Help, Thank you!

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.

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. :cool:

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.

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.

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



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.

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!

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?

Hopefully that helps. :slight_smile: