How do I setup server-side physics and replicate them correctly to the clients?
There’s a “replicate physics to autonomous proxies” checkbox, however it doesn’t seem to be that easy:
when I enable it, the spawned actor will be teleported away to whatever location, I’ve no idea what I’m doing wrong.
I’m starting a dedicated server from within the editor.
Events and player-movement are replicated as expected.
Replicating physics can be extremely expensive, so in some cases, it is more appropriate to fake physics replication using interpolation, especially if the application is very physics heavy. Just something to keep a note of.
Anyway, to answer your question:
Replicating physics is all down to the actor setup. Child components from an actor class will not replicate their transform data (even if bComponentReplicates is true). The component which contains the physics data you want to replicate (such as a static mesh), must be the root component. It is a good idea to use StaticMeshActors for generic physics replication.
Once your hierarchy is setup, make sure your actor is set to replicate appropriately. In the class defaults:
bAlwaysRelevant = true
bReplicateMovement = true
bNetLoadOnClient = true
bReplicates = true
Next, we will look at the interactions themselves. You only want the physics interaction to happen on the server otherwise the object on the client will get two different collision responses and you will see the object fighting with its own transform data. Note objects will replicate their physics state as well. So setting bSimulatePhysics on the client may not disable physics. I would disable collisions entirely using the “SetCollisionsEnabled” from the static mesh component.
Physics replication can be very tricky to get the hang of at first, but it should be fairly quick to start seeing results.
thanks, that helped a lot!
this saved me a lot of time, thank you
This issue is related. The physics weren’t working because of project config file network settings, after upgrading a project to 4.25.