Authoritative all in one FPS network solution

Hello everyone,
Today I would like to share with you something I have been working on for quite a bit. This is an authoritative FPS controller with all sorts of features like prediction and client-side reconciliation. The system works pretty well now, good enough for my needs. There are many things to add, but the current state is great.
The code is on GitHub, you can use it anyhow you like as long as it follows the MIT license. Any feedback would be appreciated.

Youtube video:

Features:

  • Client-side prediction.
  • Client-side reconciliation.
  • Interpolation.
  • Snapping to grid in order to suppress floating point nature.
  • Inputs and results storing to combat big latency changes.
  • Relatively low overhead.
  • Quake-like strafing. But most likely very incomparable.
  • Crouching.
  • Animations.
  • Toleration values, because the floating point errors.
  • Network data analyzer to compare the results between the server and the client.
  • First person and third person camera.
  • Gameplay recording to a file. Works with all objects, easily extensible.
  • AI targets (moves towards the point).
  • Sliding of steep surfaces.
  • Foot IK.
  • Ragdoll.
6 Likes

New update. Added quake-like strafing. As well as cleaned up the code. The strafing part probably is nothing like in actual quake or valve games but makes it a little bit more fun to play around.
My thoughts about the GC allocations were correct. I will probably write byte serializer next week to make the situation better.

this is really great, any plan on doing a simple Third person version ?

Yes, I plan. It has been in the TODO list for ages and I think it is about time to make it happen.

1 Like

A new update to the repository. Now the speed is stored in world space coordinates. This is really important to do more advanced stuff. Also all the player settings are stored in a scriptable object. This might help with memory. Also, the camera now rotates in x axis.

About the third person mode. I will do it later. First I will add a normal human model with animations, then crouching and other features. And finally third person code. This is because at this stage the code might change quite a lot and third person movement can break, even though it should be really easy to implement.

Sounds great… I wish Unity provided an updated fps controller with unet support, I’m sure they’ll get around to in another 5years … until then will check this out :smile:

ok… well the networking side works… I’ll check it out remote server to see how it actually players

but right now the actual movement/controller default settings are damn awful… and no amount of tweaking the default data could get it feeling better… the biggest problem is deacceleration… while it works in foward movement… if you gain some speed, jump, 180, the controller doesn’t deaccelerate as fast in reverse while you slide backwards… so lots of slipping and sliding…if you increase the speed, so yeh like I said the default movement is pretty awful… strafing left/right even without holding shift is like baby steps… i dunno thats just not right… even Rust’s awful player controller isn’t that bad :smile:

I’ll take more of a look at it some more later… the nice thing is hey its a working unet fps controller with “features like prediction and client-side reconciliation” … will see how those work in a real world test with 40-100ms+ ping etc.

I know I will tweak it. Now I am trying to mimic what quake movement does, in the strafing part. Probably, the acceleration and deceleration should be much faster.
After all, it is easily tweakable. Just make sure you keep everything inside the movement function and do not set external variables.

I haven’t played quake in a long time to remember what its strafing was like, personally only liked quake2+3 but I do think the current strafing doesn’t feel good.

If you ever played brutal doom, the movement controller for that is pretty nice :smile: its just missing some floor sliding …its something I’d like anyway.

New update. Added crouching and animations. I changed the default values as well. The controller should feel much better now. No side walking animations. If someone could link any free to use ones that would be great. The default unity animations are used.

Next I will probably add third person movement. Later, I will work on foot IK placement.

If you’re looking for optimization suggestions:
CharacterController component, StartCoroutine(), using Lists, etc… are all things that potentially generate lots of garbage and/or have performance issues

That means you could do the following:

  • Create your own CharacterController component using a kinematic rigidbody, CapsuleCastAllNonAlloc, OverlapCapsule, Physics,ComputePenetration, etc… (this is a lot of work)
  • Handle delayed actions (coroutines) manually by storing start times in class variables and checking on Update()
  • Use pre-initialized arrays instead of Lists

Rigidbody will never work in this situation. Because you can not tell it to move separately from the main physics stuff. Even after a lot of work, it is simply not worth it, because it already runs good enough. And even then, most likely the character controller will work better.
There is not much garbage collection now. All of it comes from network sending and recently, from event invoke. I will look into both to solve them.
I will probably switch to pre-initialized arrays and I only do coroutines when simulate is defined which is purely for debugging.
Anyways, thanks for suggestions.

So it seems movement prediction becomes much worse after the commit where I moved the data from the player to scriptable objects. The good thing is that I will probably implement a feature which would allow to set what position difference the server would tolerate in order to prevent these issues on high latencies. Even if the current code is quite deterministic, due to floating point it will not be 100% accurate.

Here we go, a new update!

No third person movement because I was working on tracking down the problems with my code. But the good news is that I wrote some tools for comparing client and server inputs and results. The analyzer can be accessed in Window\UNet Controller\Network Data Analyzer. Read the commit message for more info.

So after fixing it I tested it with simulated latency (using some powerful linux tools) at 150ms latency with 30ms variation and 1.7% packet drop rate so I believe this was pretty much a real life bad case scenario. Got 1 serious prediction error (others were purely small floating point errors) over a minute of playing.

Next update will be the third-person movement and I will also look into some issues happening with high rate movement (40+ Hz).

Just ran a test with the server running on my phone with 4G cellular and the client on my PC with additional 450ms of latency (20ms variation). Rock solid. At 40ms variation the connection got dropped, most likely because it exceeded the 500ms limit, but I am quite happy with the results.

After 5 minutes of playing around with 3 clients total connected to my phone, got 13M data usage. I would say it can be improved a lot.

Again, a new update! Now I added sliding of steep slopes and finally - third person movement. There are other fixes and possibly other breakages. But so far, it is looking good!

So there is some work on minimizing GC allocations. Currently it seems there is no way of actually removing all GC allocations. There is a NetworkWriter used inside UNet to write the message and you it still allocates even if you pass raw bytes. At this moment I made some progress in getting a raw byte array. The length of Results will be 69. And I also managed to reuse the same byte array over and over so no GC allocations occur. The only part is the Network Writer where GC allocations are inevitable. I am looking into making my own implementation of the part where NetworkWriter is used. Maybe even reusing the byte array.

2 Likes

Thank you. I am really inspired.
However, I am concerned about the case where sometime few packets sent to the server are congested, as a result the few inputs are processed in successive fixedupdate calls so that the corresponding character is moved extremely fast even in the server side client.

Basically why this happens is because in the last update, the server did not receive any information and in the next one it got 2 inputs. And this is only server side, not client side (well, the clients get the positions from the server). After removing all GC allocations I will work on fixing this. A side note: this is pretty much the way source engine multiplayer works. It moves once it gets inputs, not necessarily if they come in at the same time. As long as the inputs are not ahead of the server it is like that. My bigger priority is smoothness for the client and security than smoothness for others, or smoothness for all with no security.

1 Like

Do you have any idea for implementing mechanisms that are related to animation heavily like vaulting or climbing?