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:
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.
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.
answered Dec 21 '18 at 11:50 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here