Hello everyone,
So I’ve tried several times to find some information or examples on this topic but with no luck.
Found some legacy implementation but it looks like that there is no open implementation/tutorial how to do such functionality using brand new UNET.
So I’ve decided to write it myself. Thankfully I’ve found this awesome post. And it helped me a lot.
I decided to share my code with the community so hopefully no one is going to feel the pain of writing it from ground up
Here is the link to github repo.
I’m open for suggestions and pull requests.
Cheers
Thanks for this, I’ve been struggling with client side prediction and it looks like this will be a big help. I’ll try and report back once I’ve got into the code a bit more.
Just a quick suggestion but would it not be more efficient to separate position and rotation as the entire struct is marked as dirty if only one of them changes?
edit:
Lines 105 - 114. That if statement will never return true surely?
//Sending results to other clients(state sync)
Vector3 lastPosition = transform.position;
Quaternion lastRotation = transform.rotation;
if(Vector3.Distance(transform.position,lastPosition) > 0 || Quaternion.Angle(transform.rotation,lastRotation) > 0){
Results results;
results.rotation = transform.rotation;
results.position = transform.position;
results.timeStamp = inputs.timeStamp;
_results = results;
}
Thanks very much for this, I implemented my own solution for this (for a prototype) and it was my first time doing so, so it will be greatly appreciated having another frame of reference.
Hello.
I hope it will help you.
About if statement. Correct, that’s a dump mistake :). I’ll upload fixed version shortly.
And about separation of rotation and position.
I’ve tried to have two different scripts for that. But there are few problems with that. For example. There would be two [Command] call in one frame and from what I’ve discovered calling [Command] 50 times per second is approximately 1 KBs of traffic. So it would be at least 2.5-3KBs of traffic if using this approach. And I’m trying to keep traffic down. Another problem is a bit more complicated. Rotation must be updated before position. Because you know it’s moving related transform.forward . And there is no guaranty that rotation would be applied before position. It’s internet after all.
I hope those answers are helpful.
Cheers
You’re welcome. I hope it would be helpful.
you are a life saver, better than unity team with their docs.
Thanks mate
I hope it would be helpful.
I’ve added possibility to override Move/Rotate and GetInputs functions. So custom movements mechanics can be easily implemented in an inherited class. It would be possible to use UE-like approach with pawns and player controllers. Also I’ve created two new functions for position and rotation update on non-owner clients.
Cheers
Thanks for the code.
I refactored my own Client Prediction/Reconciliation/Lag Compensation system from old Networking to UNET, but it is not fully complete yet.
May I ask how you measured that? I remember with old networking I ended up at 1kB/s with ~ 20 RPC calls a second, but I haven’t found a proper method yet to meaure in UNET.
Sure. I’m on OS X. I use nettop utility to monitor my network traffic.
Ok, external tool. That was also what I was planning for now (Windows user, though).
Thanks for reply.
You’re welcome
Thanks a lot for sharing your code GenaSG!
BTW, I think that UNET should support server-authoritative movement with client-side prediction and reconciliation out of the box as this seems to be a fundamental requirement for so many multiplayer games. I hope that they improve on this and bring in some nice docs soon.
That’s why I’ve shared this code. It’s a must have functionality for realtime multiplayer games. I hope it would be useful.
P.S. I’ve found a bug in interpolation functionality. So wait for update))
Crouch and sprint related functionality added.
Add “Sprint” and “Crouch” bindings to project settings.
Cheers
Playing with jumping mechanics. This one is a bit tricky. I’ve successfully synced falling through network but jumping itself is a bit jerky at the moment.
Hm, I have a bug with interpolation. List isn’t cleared as it suppose to. And still can’t add jumping.
Hello everyone,
I’ve pushed new update to the repo.
There is a fix for the interpolation bug. Also I’ve changed arguments types for virtual functions.
If you’d find any bugs please give me a shout.
Cheers
I’ve just stumbled over an interesting article that seems to fit within this thread. While it seems to use legacy networking, I guess it still can be helpful: http://www.playblack.net/zee-code/unity-3d-authoritative-server-networking-pt3