[Tutorial] Proper Multiplayer Linetrace from camera

When I code multiplayer mechanics I do so with a few base rules.

  1. Don’t ever trust the client.
  2. 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 referenceGet world Location. We know it’s correct and we want our traces to go straight toward what we are looking at.

image

For the server we use Player Camera ManagerGet 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…

5 Likes

For those wanting to see the full trace setup.

This setup prevents client key spamming (interact key) from saturating the network (mostly). It’ll only call the server if conditional criteria are met.

Could beef this up a bit by calling a simple event in the door class that turns interactive collisions on/off. Call it locally on successful trace hit. Re-enable via automation in the class when the door finishes opening/closing. Could use BP interface for this. I’d look real hard at a potential backup plan if the servers hit misses or heavy packet loss. Might end up with a locked door.

Edit… Example disable w/backup plan

1 Like

I’ve been converting my project to be multiplayer friendly the past few weeks and was wondering why my line traces were functioning so differently on the server vs standalone. This really helped me a lot. Thanks for this.

Awesome.
I will be publishing a tutorial specific to this trace later tonight. It’ll be part 1 of a series that eventually handles doors (standard, bi-directional) and elevators. Fully networked with a bit of optimization and gameplay functionality tweaks.

The final version of that trace function has a bit more hit criteria implementation and interaction control.

Part 1 of the Multiplayer Interaction System is up.
I’m in the process of recording part 2. As I get them published I’ll link them in the vids descriptions.

1 Like

I saw this this morning and have been fixing item interaction all day. It didn’t hit me 'til now this is one of the big hurdles I’ve been dealing with.

“Sometimes it works and sometimes it doesn’t, WHATS GOING ON” :disguised_face:

So this showed up at a good time!

1 Like

Part 2 is up… 3 and 4 on the way.
I’m in the process of adding all the BP’s to blueprintue.com, I’ll eventually go back through and link them in each Vid.

2 Likes