[4.7.6] - Line Trace Sometimes Fails

Hey Guys,

I have an issue on a project right now where I am running a Line Trace through code and sometimes it happens to miss the Actor in question. I am doing a trace to center screen and it works roughly 90% but there is some flickering and sometimes it passes right through the object to the one behind it.

It was mentioned before that this could be a bug here: Line Trace sometimes misses landscape - World Creation - Unreal Engine Forums.

Let me know if this is not just with Landscapes and if a fix is inbound, thanks again guys!

,

When you are aiming at the landscape, is it potentially hitting a landscape seam when it returns null?

Hey ! Thanks for the quick response.

I actually have a test mesh that I’m testing against and below that is a BSP. I actually am not using a Landscape object in the scene in question. I have a dynamic material that changes the color once the trace succeeds but I notice that sometimes the color flickers and when I put a breakpoint, it does fall through the StaticMesh every so often.

When the static mesh flickers is when it falls through? Are you using a static mesh that you made or one that was from the editor?

Correct. The flickering is my material instance changing colors to show that it has been either selected or deselected. The StaticMesh was one that was previously made as a debug mesh but I’m another mesh we created and that seems to be working now in my new test level. I’m not noticing it now. I only noticed it on a test map that had the default setup with the Sky Dome and BSP Floor.

EDIT: If it were the mesh, and we had a closed mesh that was a Cylinder, what case would that cause incorrect traces?

Thanks again !

UPDATE: It works real well when you are up close but when you are farther away, it doesn’t work to well. My trace distance is set to 10,000 units btw.

QUESTION: In C++, how do I draw a debug line to visualize my trace?

UPDATE 2: Now it seems to work fine from a distance

This is how I am setting up my trace…
FVector FocalPoint = PlayerPawn->GetCamera()->GetComponentLocation();
FVector EndPoint = (PlayerPawn->GetCamera()->GetForwardVector() * 10000.f) + FocalPoint;
TraceResult = PC->GetWorld()->LineTraceSingle(HitResult, FocalPoint, EndPoint, ECC_Camera, TraceParams);

I tried using PlayerController->GetFocalLocation() as the starting point but that doesn’t seem to work correctly. The only location that seems to be correct is getting the current camera’s location and using that.

EDIT: I also notice when my character is running (usually backwards), the trace is off until they stop.

,

I had a chat with , one of our primary coders on the team, and he provided me with a set of code for a line trace that seems to work really well. Give this a shot and see if it works for you (make sure to include Engine.h in the .cpp you are using this in):

if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::, TEXT("Starting Trace"));
	}

	FVector CamLoc;
	FRotator CamRot;
	this->GetController()->GetPlayerViewPoint(CamLoc, CamRot);
	const FVector TraceDirection = CamRot.Vector();

	FVector StartTrace = FVector::ZeroVector;
	FRotator UnusedRotation;
	this->GetController()->GetPlayerViewPoint(StartTrace, UnusedRotation);

	StartTrace = StartTrace + TraceDirection*((GetActorLocation() - StartTrace) | TraceDirection);

	const float TraceRange = 4096.f;
	const FVector EndTrace = StartTrace + TraceDirection * TraceRange;

	static FName TraceIdent = FName(TEXT("TheTrace"));
	FCollisionQueryParams TraceParams(TraceIdent, true, this);

	FHitResult TheHitResult;

	GetWorld()->LineTraceSingle(TheHitResult, StartTrace, EndTrace, ECC_WorldStatic, TraceParams);
	
	DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Yellow, true, 5.f, 0, 1.f);

	if (TheHitResult.Actor != NULL)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TheHitResult.GetActor()->GetHumanReadableName());
	}

Thanks and !!!

I’ll give this a shot, just getting the morning started and gearing up for the AI stream today. I’ll test this out and let you know how it works for me.

Thanks!

Hey , I haven’t implemented the code you provided yet but I noticed something odd. The reason I am not seeing flickering is I implemented a check to see if it fails, wait until the next update before checking if the object is really deselected (not traced to).

I just was doing a test for work and I am printing out some results and I am noticing with the Trace I am doing that one frame, it hits the actor, the next frame, the actor is NULL but a “Model Component” came back. Every frame it literally alternates perfectly. I think the engine has some issue where it’s missing every other frame.

I’m guessing the Model Component is for BSP since I have in my test level 6 BSP walls including floor and ceiling.

I’ll try out your code and see if it helps while the Character is moving but I wanted to share those results as it could be something internal with the engine.

Hope this helps and I’ll get back to you to let you know if I get better results with your and 's code. Thanks again buddy!

The bsp returning null is expected. BSPs are hollow and the trace passes through. Try placing a blocking volume inside of your bsp to get the trace to correctly register.

Hey !

I’m going to test your code today to see if that makes the moving trace better. In regards to this issue, I’m wondering if my trace is erratic because of how the character mesh “backpedals” or strafes. I notice some “jittery-ness” in the character and since the trace is coming from the current selected camera attached to the Pawn, that it could be why when I move back, I see the tracing going nuts. I’m sure we’ll figure it out though.

As far as the BSP, the issue was more specifically that my trace (when pointing at a StaticMesh w/ Collision), that every frame it alternates from getting the Actor back from the trace. One trace, it has the actor w/ the SM the other trace is NULL and it goes back and forth between the two each frame. Hope that clears that up :slight_smile:

I’ll let you know if I have some time to test some more results today, otherwise, I’ll catch up with you after the morning stream tomorrow.

Take Care and thanks again!

Hey ,

Let me know when you get a chance to update me on my previous response but tomorrow I’ll try your code to see if that happens to resolve any of the issues we are having. My code has been working except for the jitteriness when the player backpedals and the traces going between caught/missed every other frame.

Other than that, hope you are having a great week! :slight_smile:

,

Where are you implementing this trace specifically? Is it tied to your movement at all?

Hey ,

Sort of. Right now I have a custom C++ component that is attached to the Pawn that handles the tracing…

In my Pawn in the Tick function, I update the trace to see if a unit has been selected or deselected.

In the component, here is the following code I wrote to perform the trace. Thanks again for the help.

bool UUnitManagementComponent::UnitTrace(FHitResult& HitResult, AActor* IgnoredActor, ECollisionChannel CollisionChannel, float Distance, FVector Offset, FVector EndLocation, bool Directed)
{
	FCollisionQueryParams TraceParams(FName(TEXT("Unit Trace")), true, nullptr);
	TraceParams.bTraceComplex = true;
	TraceParams.AddIgnoredActor(GetOwner());
	if (IgnoredActor) TraceParams.AddIgnoredActor(IgnoredActor);

	HitResult = FHitResult(ForceInit);

	// Get World Context
	ACoreCharacter* PlayerPawn = (ACoreCharacter*)GetOwner();
	TObjectIterator<APlayerController> PC;

	bool TraceResult = false;
	if (PC && PlayerPawn)
	{
		FVector FocalPoint = PlayerPawn->GetCamera()->GetComponentLocation();
		//FVector FocalPoint = PC->GetFocalLocation();
		FVector EndPoint = (PlayerPawn->GetCamera()->GetForwardVector() * Distance) + FocalPoint + Offset;

		TraceResult = PC->GetWorld()->LineTraceSingle(HitResult, FocalPoint, Directed ? EndLocation : EndPoint, CollisionChannel, TraceParams);
	}

	return TraceResult;
}

,

From looking at your code, one thing that could possibly be causing the problem is the “CollisionChannel” part of the line trace function. Can you try explicitly typing out the collision channel instead of using the variable that you passed in for it? Otherwise, would it be possible to have a copy of your project so that I can see the issue in action and debug it hands-on? If the project is too large to be uploaded here, please feel free to upload it to DropBox and either link it here or send it to me in a private message on the forums ().

Hey ,

Hope you are having a great day and thanks for the response! You really think it’s the enumeration?

Here is my function prototype/declaration…

bool UnitTrace(FHitResult& HitResult, AActor* IgnoredActor, ECollisionChannel CollisionChannel = ECollisionChannel::ECC_Camera, float Distance = 10000.f, FVector Offset = FVector(0.f, 0.f, 0.f), FVector EndLocation = FVector(0.f, 0.f, 0.f), bool Directed = false);

I am 99% of the time going w/ ECC_Camera but in one instance, I go with ECC_Visibility. Let me ask you, is there a way I can create a custom channel and assign the traces to that?

Now if it were the function parameter, how would that be an issue if ECC_Camera or ECC_Visibility were passed through?

Thanks and looking forward to figuring this one out.

I apologize but without being able to reproduce your exact issue or having your project to tinker with, most of my solutions that I’m able to offer will be shots in the dark.

The suggestion about the CollisionChannel variable was a guess as I was unsure what type of variable it was. It could’ve been the case if you were to of stored the name of the collision channel as a string but in your case, there shouldn’t be an issue with it.

I’m happy to help you solve this issue but I will need to see your project or a test project that reproduces the issue to able to provide any helpful solutions.

Hey ,

Sorry about the delayed response. Been really sick and trying to push through.

Let me have a chat w/ the Creative Director. Right now, this project is not revealed and if we did this, are their mutual NDA’s in place?

I fully understand that it might be difficult to find the issue if I can’t provide you anything further. I’ll check in to see if this is going to be okay .

In the meantime, I wanted to inquire about paid support. I remember Noland mentioning at one point Epic has a direct support program that game devs can pay for. Out of curiosity, do you have any details regarding this (or email address) and also, what does it add that I wouldn’t get normally from AnswerHub aside from more direct support?

UPDATE: I just thought about it and it might be possible for you to repro this before I inquire about sending you the direct source and/or content used. For the trace code, do the following to repro…

  • Create a C++ ActorComponent and use the trace function I posted above
  • Attach the component to a Character
  • In the Character’s Tick, call the trace each time Tick gets called
  • Write the log to the screen w/ GEngine->AddOnScreenDebug… referencing the HitActor coming back

The above should at least in 4.7.6, cause the flip between null and not every other frame. I am making the assumption it doesn’t have to do with the mesh’s collision since the trace that flip flops is not moving and in testing another model, got the same results. But then again, you never know so I’ll hold off on that unless needed.

When I originally posted this, the level I was testing on was using the default sky, bsp floor, directional light, etc when creating a new level.

I was aimed at the mesh which was sitting on that BSP floor.

I hope this helps and if not , no worries buddy and I’ll check in to see if its okay to send you more to test with.

Thanks again for the assistance and hope you are enjoying your Friday!

Hey MC -

Thanks for the steps in reproducing the line trace problem, we are still investigating that at the moment. For your questions about other support options please visit this page for more information: Lizenzierungsoptionen für Unreal Engine (UE5) - Unreal Engine

Cheers,

,

What version of the engine are you using? I used your code but had to change a couple things because I didn’t have your custom character class. I wasn’t able to reproduce the issue, but I will explain everything I did in case I’m missing something that may be causing the issue. You could also try this on your own as it takes just a few moments to set up to see if you still get the same results. I used the First Person Shooter template in version 4.7.6 and added the component you suggested with the class. The lines I had to change were:

ACoreCharacter* PlayerPawn = (ACoreCharacter*)GetOwner();

to

ACharacter* PlayerPawn = (ACharacter*)GetOwner();

FVector FocalPoint = PlayerPawn->GetCamera()->GetComponentLocation();
FVector EndPoint = (PlayerPawn->GetCamera()->GetForwardVector() * Distance) + FocalPoint + Offset;

to

FVector FocalPoint = PlayerPawn->GetActorLocation();
FVector EndPoint = (PlayerPawn->GetActorFowardVector * Distance) + FocalPoint + Offset;

After this, I added a call to the function in blueprints using the On Tick function. I didn’t supply any inputs other than the distance, to make it actually trace outward. I then dragged on Hit Result and made a ‘Break Hit Result’ node. I then dragged off ‘Hit Actor’ and make a Get Display Name node and printed the result. I then attempted putting 2 different Editor Cubes (part of the First Person Shooter Template) beside each other, it would never go through the first one and display the name of the other.