Haya,
I am trying to position a mesh relative to the camera in a first person game every frame as an off screen indicator, and I’m having a lot of trouble with it while moving around. When I am standing still the positions are totally correct, but while I’m moving the positions seem to lag 1-3 frames behind my movement. I’m wondering if anybody can maybe see where in my setup things are getting messed up, because I would have thought this would be much simpler.
The basic idea is that I’m trying to create an off screen indicator to point at locations in world space.
What I do is in my tick function I get the ScreenLocation of my target world location using PlayerController::ProjectWorldLocationToScreen, restrict that to the screen bounds (this part works fine, I can see the values make sense in debug), then I project that adjusted screen position back to world space, and set the mesh’s relative position based off that world position.
Judging by debug output, it looks like my relative positions end up being off of what they should be, which leads me to believe that in my tick function my character’s/component’s world transform is different from the one being used for the World->Screen transform.
I have no idea what I can do to avoid this. My only idea is to get the relative screen bounds in BeginPlay and use those as the basis of setting my component’s relative position instead of trying to do it every frame, but I still want to know why updating things in tick the way I do seems to be messed up.
Any ideas much appreciated.
edit Update: So it looks like the lag comes almost entirely from using the PlayerController deproject functions in tick. An easy way to repro is just to call something like this in your tick function:
FVector worldPoint, worldOrigin, worldDir;
controller->DeprojectScreenPositionToWorld(200.0f, 200.0f, worldOrigin, worldDir);
worldPoint = worldOrigin + worldDir * 50.0f;
DrawDebugSphere(GetWorld(), worldPoint, 10.0f, 10, FColor::Green);
It draws perfectly when not moving, but once you start rotating or anything it clearly drops behind by 1-3 frames.
edit Update 2: Confirmed this also happens when you do the same in Blueprint with ConvertScreenLocationToWorldSpace on epic content examples/sample projects.