Unreal C++ Linetrace Logic Lags Behind [Help/Question]

Hello. This might seem like a simple question, but I haven’t been able to find anything about this anywhere and it is super frustrating.

I have had an issue for the past year or so with doing any kind of line/sphere tracing in C++ as well as doing any sort of DebugShape. They all seem to lag behind my Pawn with around 9-10 frames from where the Pawn actually is. At first I thought it was only cosmetic, with the problem laying with the DrawDebug command while the actual trace would be fine. But that all changed yesterday when I discovered that the linetrace logic itself also lags behind with the same amount. I discovered this when I was doing a linetrace underneath my character to place them on the ground. Whenever I would land, my character would first penetrate into the ground before popping back out after 9-10 frames. I recorded my game and played it back frame by frame to see more clearly what was going on.

I have included two Youtube links bellow showcasing it a bit more(First one shows the debug lines lagging behind and the second one shows how this delay also affects the HitResult Location.) Also note that the video is 60fps, but OBS only recorded 30 fps for some reason. So there are double frames.

Video 1: Unreal Engine C++ Linetrace Lag Example 1 - YouTube (Note how the debug lines lags behind when the Pawn moves)

Video 2: Unreal Engine C++ Linetrace Lag Example 2 - YouTube (Note how the debug line underneath say there is a hit, but it takes a few frames for bBlockingHit to become true)

Does anyone know what could be causing this? Is there something I’m doing wrong? I have Googled around for hours and haven’t found anyone having the same problem. I have both tried using the GetWorld()->LineTraceSingleByChannel() and the Blueprint version UKismetSystemLibrary::LineTraceSingle() with the same results. I have also tried using both the .Location and .ImpactPoint from the HitResult. Here’s my code:

UKismetSystemLibrary::LineTraceSingle():

FVector SmoothCrouchStart = CapsuleComponentMC->GetComponentLocation();
FVector SmoothCrouchEnd = SmoothCrouchStart - FVector(0.f, 0.f, CapsuleDefaultHeightMC * 2.5f);

FHitResult SmoothCrouchHit;
TArray<AActor*> ActorsToIgnore;

// Perform LineTrace
UKismetSystemLibrary::LineTraceSingle(
	UpdatedComponent,
	SmoothCrouchStart,
	SmoothCrouchEnd,
	UEngineTypes::ConvertToTraceType(ECC_Visibility),
	false,
	ActorsToIgnore,
	EDrawDebugTrace::Type(1),
	LineHit,
	true,
	FLinearColor(1.f, 0.f, 0.f, 0.f),
	FLinearColor(0.f, 1.f, 0.f, 0.f),
	deltaTime
);

if (SmoothCrouchHit.bBlockingHit)
{
	PlayerModelNull->SetWorldLocation(SmoothCrouchHit.ImpactPoint);
}

GetWorld()->LineTraceSingleByChannel():

	FVector SmoothCrouchStart = CapsuleComponentMC->GetComponentLocation();
	FVector SmoothCrouchEnd = SmoothCrouchStart - FVector(0.f, 0.f, CapsuleDefaultHeightMC * 2.5f);

	FCollisionQueryParams TraceCollisionParams;
	FHitResult SmoothCrouchHit;
	GetWorld()->LineTraceSingleByChannel(SmoothCrouchHit, SmoothCrouchStart, SmoothCrouchEnd, ECC_Visibility, TraceCollisionParams);


	if (SmoothCrouchHit.bBlockingHit)
	{
		PlayerModelNull->SetWorldLocation(SmoothCrouchHit.ImpactPoint);
	}

In addition to that I have tried playing around with PrimaryComponentTick.TickGroup, but they all seem to have no effect on the actual lag. I’m using TG_PrePhysics in the videos above.

Any help would be much appreciated! Thanks for your time.

Best Regards

-Sven

After more digging I found that doing Linetraces on my Pawn has no lag, but doing them on my PawnMovementComponent lags. It seems like the tick function in my PawnMovementComponent is not being called at the same frame as my Pawn. How do I fix this? I have tried different Tick Groups and functions like : PawnMovementComponent->bTickBeforeOwner = true;
PawnMovementComponent->bAllowConcurrentTick = true;
PawnMovementComponent->SetComponentTickEnabledAsync(true);

So I figured out what the problem was. If you do a LineTrace inside of your MovementComponent, the trace lags behind, but if you do it inside the Pawn the MovementComponent is attached to, it does not lag. It seems that it either doesn’t get the correct location from GetComponentLocation() for your trace Start and End coordinates or the LineTrace function is not called on the correct frame. Either way it seems that I have misunderstood how to structure my Pawn with an attached MovementComponent. It seems that the only thing you should use the MovementComponent for is to actually move the player and do all your world checks with LineTraces inside the Pawn. Sound obvious now, but since there is no documentation or tutorials telling you this I had to just figure it out myself through trial and error.

Hope this helps anyone else who runs into a similar problem that I had.

Best Regards

-Sven