When I code multiplayer mechanics I do so with a few base rules.
- Don’t ever trust the client.
- Don’t waste bandwidth replicating data you don’t need to.
In many multiplayer scenarios we need to execute line traces. One of the more common is for interaction. Interacting with doors, elevators, pickup items, and other gameplay actors.
For these types of traces we have the client execute a line trace from their camera. If we get a proper hit result (interactive collision), then we RPC the server to execute it’s own line trace. Obviously you’d want the servers line-trace start to be from the same component location, e.g. the camera.
Problem is Camera components do not replicate their locations correctly or if at all in some scenarios. Many have decided, or have been told to replicate needed info via the Servers RPC. This breaks both rules.
“Hey server, run your Authoritative line trace from this start point.”
What stops me from shooting (hit scan) and or interacting with an actor on the other side of a wall, door, map? Nothing, other than adding more code to scrutinize the location, which gets complex quickly.
So the best approach is to just not trust the client to begin with, and use functionality, actors, classes already setup in the engine to give the info you need.
Player Camera Manager!
For local camera world location we simply use a camera component reference
→ Get world Location
. We know it’s correct and we want our traces to go straight toward what we are looking at.
For the server we use Player Camera Manager
→ Get Camera Location
.
For the trace…
If you use the Camera’s location on the server you could get results like the following (server trace is Purple).
Whereas using PCM->Camera Location results in…