Search Unity

Unity Multiplayer Best practices for multiplayer in a racing game without collisions between cars

Discussion in 'Connected Games' started by newlife, Jun 13, 2019.

  1. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    Hello,
    I would like to implement multiplayer in my mobile racing game (Real Drift Car Racing).
    Collisions between cars are disabled. Some kind of matchmaking is required in order to match players with the same level of expertise.
    Basically, something like the multiplayer in Assoluto Racing or Carx.

    Any advice?
     
    Last edited: Jun 13, 2019
  2. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    You may check this tutorial here: http://docs2x.smartfoxserver.com/ExamplesFlash/space-race
    It was developed on the Flash platform (not Unity), but you should get the basic concepts of client synchronization. On the server-side the game uses SmartFoxServer and the code is written in Java, while the client-side code is written in ActionScript. In both cases it shouldn't be too difficult to figure out what is going on.

    The game doesn't feature a specific match-making system (other than a Lobby), but you will find details on how to implement it in other sections of the SmartFoxServer documentation.

    Hope this helps.
     
  3. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    Hello g_a_p,
    thank you for your reply. Which is the suggested "setup" for my requirements?
    As far as I understand, the best solution should be the hybrid solution (SFS2X + server side Unity). Is this correct?
    Also, is there a tutorial about client synchronization and matchmaking made for Unity?
    How SmartFoxServer compares with Unity Multiplay and Amazon GameLift?
     
  4. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    I had a look to the page of the live demos, and the only Unity demo is made with a web player, so its not working.
    Do you have plan to update the demo to WebGL?
     
  5. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    You can find all the latest versions of the demos in this section of the SFS2X documentation: http://docs2x.smartfoxserver.com/ExamplesUnity/introduction

    We also have a demo package (containing the same examples) published in the Asset Store.
     
  6. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    I guess the need for server side Unity depends on your game requirements. If you just need to synchronize positions and you don't need a physics engine (or if it's simple physics every client can handle on their own), you don't even need Unity on the server side.
    For example our Object Movement, First Person Shooter, MMO Demo and SpaceWar examples show a few different ways to synchronize positions.

    For client synchronization, see my previous answer. For the matchmaking we don't have a specific example, but here you can find the documentation to implement it: http://docs2x.smartfoxserver.com/AdvancedTopics/game-api

    About Unity Multiplay, it is not yet clear how the service will work, so it is impossible to compare it to SmartFoxServer now. About GameLift, from what we know it is a sort of "self-service" in which you assemble your gaming infrastructure. From our understanding, both services will be something different from SmartFoxServer, so hardly comparable.
     
  7. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    I was referring to the live demos
     
  8. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    The physics is not simple (full realistic vehicle simulation). Keep in mind that this is my first time in multiplayer developing, my knowledge is very limited.
    So, since we dont need collision between cars, we also dont need a phisic engine on the server.
    In this scenario every client will calculate the physic only for the local vehicle (user vehicle) and send its position to the server, that will send the position to the other player. Is this correct?
    If yes, which is the best architecture? Which is the best way to synchronize positions, knowing that its a racing game (so with fast paced object)?

    Strange cause matchmaking is a very basic feature of any multiplayer game..
     
  9. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    For this reason I think you should start from the basic concepts and then move on. Multiplayer development can be hard to learn, and there's no magic recipe. Every single game has its own requirements and therefore solutions.
    If you are interested in working with SmartFoxServer, we actually recommend reading the general documentation (so you learn how it works, then proceed through the various examples, from the basic one showing how to connect a client, to the most complex ones showing synchronization and more.
     
  10. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    Sure, in fact this is a well supported feature of SmartFoxServer. We just don't have a Unity example showcasing it at the moment. We will take this as a suggestion to improve our set of demo projects then. Thank you.
     
  11. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    Well im writing here cause I need some advice in order to understand which is the best architecture for this kind of game, which represent a very common scenario (even simpler than average cause there is no collision between cars and there should be two player at the same time, no more).
    I dont think its a good idea to reinvent the wheel.
    Btw, I had a look to your showcase page and I found a game that has EXACTLY the same architecture than our (Drift Mania: Street Outlaws).
    Do you know how multiplayer has been implemented in this game?
     
  12. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    Unfortunately not, we don't have the technical details about most of the projects based on SmartFoxServer.
     
  13. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    Any other suggestion? Is it still possible to use Unity Multiplayer?
     
  14. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    69
    I'll try not to recommend anything specific but I can tell you what I've looked at and am currently using and why.

    I'm using UNET and the HLAPI but NOT the peer to peer hosting solution called "Unity Mutiplayer". I decided to go with a dedicated server option, even though my game is only 1v1. Best case scenarios it was going to be cheaper than than the peer to peer solutions, and worst case scenario it was close enough that I didn't care about the pricing difference with the added benefit of dedicated servers.

    I originally went with this asset store pack: https://assetstore.unity.com/packages/tools/network/master-server-kit-71604

    It's self hosted so you need to manage your own cloud droplet on Digital Ocean (I've confirmed it does not work on AWS last I used it anyways). It's fairly straight forward, and decently documented. The developer also usually gets back to any questions within a day. As someone with no Linux or cloud experience prior I was still able to get something working although I had to learn a lot of things.

    It's dirt cheap, gives you a basic for hosting multiplayer and runs a straight linux headless build of your unity project. Downsides are it doesn't auto scale to demand. If you are expecting more than a few dozens players you are going to have to scale it manually by setting up more cloud droplets. It also does not have a skill based matchmaker. It can have players find and join servers fine though.


    I decided to go with AWS GameLift as a final solution. It's a dedicated server option and cheaper than all other cloud hosting solutions I've found with the exception of doing it yourself like above. It runs a straight Linux headless build of my Unity project. It auto scales to player demand and comes with a skill based matchmaker. Downsides are you need some sort of profile / master server framework as a middle man to talk to the service. Your players should login to something like PlayFab, GameSparks, Chilli Connect etc then have that talk to AWS api's. That also costs money though some have free tiers unless you want to manage that part yourself.

    Be-aware as someone who was not familiar with AWS when I started, that they split their services out. So if you want GameLift to send a message to GameSparks, you're gonna have to use another AWS service to do so, and it's going to cost you as well. AWS is not an all in one solution, it's all divided up so just be aware of all the pieces you are going to need before you start.

    If you use spot instances it's cheaper than their competitors. It got a great toolset and their technical support is super helpful even to solo developers.

    Also I would look into PlayFab and their hosting solutions they've been moving towards with Microsoft Azure. They are similar priced to GameLift on average, but don't have equivalent to spot instances so end up being much more money. But they do appear to give you more of an all in one solution with the user accounts and hosting all bundled together so you don't have to piecemeal a bunch of services together yourself.

    I also looked into Multiplay which were recently acquired by Unity. Unfortunately their pricing is orders of magnitude more and not really geared towards indie developers.

    Last but no least there is Photon which I have not used but I know some who do and used to be fairly popular around here.



    For a racing game without player collisions I think it shouldn't be too difficult as far as the game code goes. Player collisions probably being the harder part so if you are skipping on that it'll make things easier.

    If you do client side prediction and have clients run the game as normal while sending all inputs to the server to be verified. This way you can prevent cheating and re-update the client if their are any discrepancies.

    Additionally, you take those server inputs and send the resulting positions back for all opposing players as "Ghosts" so you can see their positions in real time.

    I imagine there might have some trickiness though, especially with any close races at the finish line. If the local player has client side prediction, and they see a ghost of an opposing player. They are going to see themselves ahead of where they are on the server, and the ghost behind where it actually is. So you could end up with a case of the player seeing themselves cross the finish line first, even though according to the server the opposing player won. Not sure the best way to resolve that. Might be some white papers or GDC talks from racing game networking around that might give some clues.

    Alternatively depending on the handling response of the vehicles you could try and do it all server side and update the player positions that way. Latency would effect input, but you would solve the above problem as everyone would see the same state for all players including themselves. Additionally this would allow you to do player collision without any issues since it's all handled on the server. I know Unreal Tournament 2004 did all multiplayer vehicles server side since no one noticed the latency with the way vehicles handled anyways. But that's not a racing game and vehicles might handle different than your game.

    anyways, sorry for the long post, hope it helps and good luck!
     
    newlife likes this.
  15. g_a_p

    g_a_p

    Joined:
    Mar 16, 2015
    Posts:
    112
    I take a cue from the above comment to talk about the "authoritative server" approach. This is the one I favored when developing a space-combat demo game a few years ago. The following is extracted from an article I wrote to discuss the techniques used for players synchronization.

    Even if it refers to spaceships, I think the logic in a car racing game is quite similar, supposed you only have to deal with a trajectory altered by user input. If the trajectory can be altered by the environment too (for example bumping against obstacles), there would be an added layer of complexity which would require additional refinement of this approach.

    In any case I hope this helps, as the concepts can be applied to any solution based on an authoritative server.

    » Clients synchronization
    Maybe the most complex topic when developing a realtime multiplayer game like this example, it's to deal with the clients synchronization. Each game type has its own strategies and techniques which include movement prediction, interpolation, latency compensation and what more.

    When developing a strategy for synchronization we should always aim at minimizing the amount of data exchanged between the clients and the server. The reason is not to overwhelm the client and the server with messages to be processed, allowing more concurrent users on a single server instance and reducing hosting costs (less server instances, less CPU power per instance, less consumed bandwidth). For this purpose the mechanics of each game must be analyzed in depth to choose the best approach depending on its characteristics.

    Our SpaceWar game is a simplified space simulation in which starships and weapons move on an immutable trajectory. This trajectory is a straight line (but the same concepts also apply in case of curved trajectories, for example due to a gravity effect) which, in the case of starships, can be modified by the engine thrust or a collision with a weapon shot, which both alter the ship velocity (speed and direction). Due to the very simple maths involved (essentially we have to sum vectors representing the speed), we decided that the best approach was to run the same simulation routine on both the server and the clients on a time basis: we set a framerate of 25 fps in the Flash client and a corresponding scheduled task (running every 1000 / 25 = 40 milliseconds) on the server, and execute the same simulation logic in each frame.
    With this approach, the server continuously updates the position of all entities (starships and weapon shots) existing in the MMORoom and the same does each client on its own (of course taking into account the "known" entities only — in other words those falling within the user's Area of Interest). If no alterations to the state of such entities happen, the server and client simulations keep running in parallel without the need to exchange data between them (for example the clients declaring their position to the server continuously). We reduced the data exchanged between the server and the clients to zero (PROXIMITY_LIST_UPDATE event apart).

    Of course this is not realistic, because the players interact with the game rotating their starships, turning the thruster on and firing shots: in other words altering the starships trajectories (not to mention possible computer slowdowns, in particular on the client side, which can make the two simulations diverge even if the player doesn't interact with the game).
    In order to keep the simulations in-synch and still exchange a small amount of data, we promoted the server simulation to "master" (in other words we have an authoritative server): all actions causing a trajectory change, for example the player pressing or releasing the thrust key, must be communicated to the server, which updates the entities state accordingly and sends the updates to the clients which in turn reset their local simulations, ensuring an high degree of synchronization.

    » The lag problem

    The client synchronization mechanism described above works perfectly in a local scenario, where no latency exist between the server and the client. But what if we introduce the communication lag? Let's take a look at the following picture:



    1. The starship is moving along direction d0 at a velocity represented by the blue vector.
    2. At time t0 the player presses the thrust key after a 90 degrees rotation of the starship, sending a request to the server only (no changes in client simulation).
    3. At time t1 the server receives the request and processes it, summing the current velocity vector and the thrust vector. Now the resulting velocity direction is d1 and the new vector is sent to the client.
    4. At time t2 the client receives the update, but in the meanwhile (t2–t1) the starship in the client simulation moved to this new position: setting the new velocity now places the starship on the resulting d2 direction, making it out-of-synch with respect to the server (on the server the starship kept moving along direction d1in fact ).
    The t1–t0 latency isn't very important and in any case we can think about some tricks to reduce the lag perceived by the player between the key press and the action being executed (we will discuss this again later in this document).
    The t2–t1 lag is instead what causes the client and server simulations to diverge, and we have to take actions to compensate it. The solution adopted in our example is based on measuring the mean lag between the client and the server using the built-in feature of the SFS2X API. Given this value (which approximately indicates the t2–t1 time span), the position the starship had at time t1 and the new velocity vector (both saved in User Variables, as explained later), it is possible to calculate the actual position of the starship on the server side and reset the client simulation to align it with the server one.

    The final result is pretty good, even in case of extreme (for a realtime game) latencies of 200 milliseconds or so.

    The above is taken from this tutorial: http://docs2x.smartfoxserver.com/ExamplesFlash/spacewar-p1
    The tutorial discusses the Flash version of the game, but the same concepts have been applied in the Unity version available here: http://docs2x.smartfoxserver.com/ExamplesUnity/spacewar
     
  16. Stanchion

    Stanchion

    Joined:
    Sep 30, 2014
    Posts:
    247
    @TheLastVertex How it your progress with GameLift? Do you have it working with your game now?
     
  17. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    69
    Yes I have it working with my game at the moment. I'm happy with it but haven't gotten around to an external play test on it yet. The previous play test I did was using the old solution.

    I should clarify some comments I made about it earlier. It has a "Matchmaker" that is completely open to set the rules as you please, but not necessarily a skill based matchmaker out of the box. They give code examples for varies rule sets, one of which is how to set up the rule set for a skill based matchmaker. Now given this is a huge part and very valuable it is also only a portion of the solution. The other part of the battle is how do you track skill? Where do you store players skill values? How are the skill values increased or decreased based on wins and loses? Unfortunately this portion is not provided. It'll take whatever values you give it and match make wonderfully based on any rule set you want to make, but you are responsible for how those values are tracked.

    I believe PlayFab / Microsoft game stack / xbox live have some interesting stuff coming down the pipe on this front which is probably worth checking out.

    The SDK setup for Unity was pretty straight forward. I did get bogged down on some error messages that were apparently an SDK issue and not my fault as I assumed. I believe this may be addressed in the newest SDK but haven't checked yet.

    I also had to manually set up port settings for the network manager. Since you can run multiple servers on a single instance they need to use different ports. Unfortunately my efforts to do this dynamically on server start were unsuccessful. Instead I have to pass them via command line and set them manually. It's only a one time setup but still requires me to manually enter command line arguments and port numbers on the AWS portal anytime I setup a new fleet.
     
  18. Stanchion

    Stanchion

    Joined:
    Sep 30, 2014
    Posts:
    247
    @TheLastVertex Can you link me to the "SDK for Unity" you're referring to?
     
  19. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    69
    Hey sorry for late reply.

    You'll want the "Amazon GameLift Server SDK" from here: https://aws.amazon.com/gamelift/getting-started/

    And the following docs:
    General: https://docs.aws.amazon.com/gamelift/latest/developerguide/gamelift-sdk-server-api.html
    Unity Specific: https://docs.aws.amazon.com/gamelift/latest/developerguide/integration-engines-unity-using.html

    You will have to compile the SDK first, then add it to Unity project.

    Also be aware. I remember there being some confusion on my part with the documentation. When they talk about "clients" they are not talking about the end user or player (aka unity networking client). They are talking AWS clients, which are probably closer to "master server" type logic I believe.

    Using that script in the Unity specific example should get you the bare minimum for your game server if I remember correctly. Then you can test it with AWS command line to get it to spin up servers and shut them down etc.

    Once you have that working you should be able to connect to the game server from the game using the network manager / network manage hud manually by entering ip and port info.

    Downside I found is googling for info is hard to find other than the official docs. GameLift forums are pretty bare as well. But I have found GameLift technical support to be phenomenal. I've had numerous calls and lots of emails with them that have been extremely helpful and willing to answer any questions I had.
     
  20. Stanchion

    Stanchion

    Joined:
    Sep 30, 2014
    Posts:
    247
    @TheLastVertex Are you manually connecting manually to the servers on GameLift rather than using the API for CreateGameSession, CreatePlayerSession, etc?
    Where is this email/phone contact available?
     
  21. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    @TheLastVertex, thank you for the comprehensive reply.
    What Im searching for is a solution as complete and simple as possible, even if it costs more. Im a single developer with some sporadic collaborators that works only on the visual aspect, so I dont have time and resources to reinvent something that has been already implemented successfully by some of our competitors (Carx Drift Racing, Assoluto Racing, Racing Fever).
    As far as I understand, Gamelift is a bit more difficult to set up and its focused on realtime multiplayer, Playfab is a more complete service (it includes multiplayer, analytics, player authentication etc.) and it takes advantage of Xbox Live developers experience, Photon is very complete too and it seems to be very tight to unity (it has been used is several successful unity multiplayer racing games).
    Now, to me the best solutions appear to be Playfab and Photon.
    Btw, what do you mean with " spot instances"?
     
  22. Stanchion

    Stanchion

    Joined:
    Sep 30, 2014
    Posts:
    247
    @newlife GameLift, PlayFab, and Photon are all very different. They have some overlap, but you could use them all in combination, like GameLift for server hosting, PlayFab for login/social, and Photon Bolt for server/client networking.

    I am currently working on samples for GameLift and PlayFab (and work on Photon Bolt)

    On-demand instances
    - They let you pay for your computing capacity needs by the hour. There is not much planning required from the user's end and no one time cost that you need to pay upfront like in case of reserved instances.

    Suitable for use cases where you do not want any long term commitment like testing and POCs, spiky, not to be interrupted workloads.

    Spot Instances - These are spare unused Amazon EC2 instances that you can bid for. Once your bid exceeds the current spot price (which fluctuates in real time based on demand-and-supply) the instance is launched. The instance can go away anytime the spot price becomes greater than your bid price.
     
  23. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    69
    I am using the API calls you mentioned, but those are only for setting and spinning up the game server on AWS and I do not call those from the end user but instead from GameSparks at the moment. You don't have to use GameSparks but this should ideally not be called from the end user due to security concerns with needing API keys and AWS privileges etc. At the end of the day though the end user still needs to connect directly via IP and Port information.

    I'm actually moving away from GameSparks due to their price change, so this could be Gamesparks, PlayFab (doesn't play nice with AWS for the moment), ChilliConnect, another service, or even your own master server if you wanna roll your own.

    I'm not sure if I should derail this thread with purely AWS stuff but I'm happy to answer your questions, via a thread, forum conversation, or even discord or steam chat if you like. I'm starting to sound like an aws sales person :p

    I don't actually remember how I got into initial contact now, but I'm assuming it was through the sales page located here: https://pages.awscloud.com/Gamelift-Contact-Us.html


    If they have the features you are after I think they are both popular for a reason.

    I am in no way pitching for AWS. I've just had to mess around with so much stuff and I encourage anyone to make sure you fully investigate every piece you need because it's a pain to get part way through and find out something you didn't even think about is missing and have to switch.

    Even right now trying to switch away from GameSparks I found out PlayFab and ChilliConnect are missing websocket feature (it's on their roadmaps though) which is a bummer and not something I even thought I would have to think about.

    To answer your question, if you have a look at their pricing page located here: https://aws.amazon.com/gamelift/pricing/

    They have "Spot" pricing and "On Demand" pricing. Take note of the instance type "c4.large". This is the most standard instance type. It has 2 Cpu's and 3.75 gb of memory. The monthly price for this spot instance is $7.92 and $46.35 for on demand. A pretty huge difference.

    AWS spot instance are essentially "left over" resources on AWS and as such they offer them for super cheap with the draw back that they could potentially be shut down if needed with a 3 minute warning(It's not as bad as it sounds). If you look up AWS spot instances they will have a whole spiel on this and how it's unlikely to happen and how they allocate the least likely to terminate etc etc. I can't seem to find the page I was looking with the stats though :(

    The downsides for gaming isn't as bad if you are planning on having a session based game and don't need a perpetual server. If your matches are only going to be run for X amount of time, then the server is shutdown, and a new server is started for each new match, this could be a cheaper solution. Since you will get fresh cloud resources every time you start a match, your chances of ever getting terminated are pretty close to 0.

    On the other hand if you want On-Demand their pricing is going to comparable to PlayFab servers for example.

    And then on top of all this you've got data transfer / bandwidth costs as well.
     
    Last edited: Jun 22, 2019 at 10:23 PM
  24. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    @TheLastVertex if you think that AWS is a better and more complete solution just tell, you seem to me someone who investigated a lot on the matter. I read your post carefully.
    For example, why do you think that this websocket feature is fundamental?
    Also, did you know that the matchmaking feature in playfab is still in preview state? https://docs.microsoft.com/en-us/gaming/playfab/features/multiplayer/matchmaking/ (!!!!)
    Thats crazy. On the playfab home page they market it as a full and ready to be used feature...
    For what concern ChilliConnect, do you think that it is an option too?
     
  25. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725
    I just had a look to a video where its stated that photon PUN 2 is not authoritative server.

    is this true? If it is, PUN is useless for me (too easy to hack a mobile game, we are already facing this issue with the leaderboard).
     
  26. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    725