x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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: https://youtu.be/ygV52pmMRm4 (Note how the debug lines lags behind when the Pawn moves)

Video 2: https://youtu.be/KtQtTqfWM2I (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

Product Version: UE 4.20
Tags:
more ▼

asked Dec 19 '18 at 05:46 AM in C++ Programming

avatar image

Headstub
10 2 2 5

avatar image Headstub Dec 19 '18 at 02:45 PM

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);

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Dec 21 '18 at 11:50 PM

avatar image

Headstub
10 2 2 5

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question