Hey there,
My goal has been to develop a co-op multiplayer game in Unity using the Netcode for GameObjects package. I discovered along the way that there are latency problems when using the NetworkTransform and NetworkRigidbody. When it comes to real time physics interactions, particularly when bullet impacts are supposed to affect the knockback of enemies, these components do not offer a real time influence on such objects. So, this led me to research client prediction and server reconciliation. If i’m missing something about these components, then please educate me, however I do not believe they provide a solution to the real-time physics problem.
Since I do not care much for cheaters, my network reconciliation approach became a simple problem. All I have to do is spawn my enemy once on each client, check for differences in position and velocity between client and sever, then correct the differences on all client instances when variance becomes too large. I do not do any prediction at this moment, so when I re-adjust a clients’ position to match the servers’, enemies will sometimes lerp “backwards” in time to a previous place, causing a little jitter.
My code is not very clean, but I have provided my class, ReconTransform, which does the “reconciliation” for enemies. In short, ReconTransform achieves sub-optimal synchronization by:
- CheckForMovementReconciliation(…) is called on an interval and takes a function. This function determines the rules which dictate the need to resynchronize. For example, this function can tell the ReconTransform that a resync needs to occur when client and server representation are X distance apart. The function also provides a float that determines the ideal distance the the objects should be from each other when recon is complete.
- AddForce(…) is a function that mimmics Rigidbody.AddForce(…) but will cache physics interactions during the lerping phase. When resynchronization occurs, all physics interactions are paused. During this time, physics interactions are cached and re-applied when the lerping/recon is complete. Lerping just means smoothly repositioning the client representation to match the servers representation. I use smoothdamp now instead of lerp.
Please help me make this package better. I don’t know what I’m doing wrong. The recon action is too jittery. In a real time situation where I am shooting a bunch a bugs for example, as a client I want to be able to aim at the enemy without it suddenly lerping to the correct position breaking emersion and frustrating players by ruining their expectations of enemy behavior. When I am up against an enemy that can kill me in two hits, I do not want to abruptly lerp the enemy away from a potential player projectile that could kill it. Anyway, here’s the repo: