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