Is it possible to add multiplayer to an already existing game?
I have a game which I've been working on for about 17 hours and I really want to add multiplayer (it is envisioned as a multiplayer game). Is there a way I could do this or is it just best for me to start from scratch and just use the same assets and scripts but with one of the many YouTube tutorials on how to make one from scratch. I'm pretty well-versed on Unity, but I've only ever made singleplayer games before.
Answer by GetLitGames · May 26 at 10:19 PM
Yes, a single player is just a multiplayer game waiting to be enabled. If you are doing Co-op, it's quite easy. If you are doing competitive/PvP then there are more considerations, and it is different from the way you build a single player game (in order to prevent cheating). If you are doing a game where you want people to drop in/out, and not have a "host" necessarily and just want it to handle "host migration" then PUN2 is great. If you want a little more defined "host" and are doing a character controller driven game with more animations etc then use Photon Fusion or Bolt. If you are doing a competitive game with possible cheating, you will want to do Fusion or Bolt also.
Typically you will write one code base, the exact same as your single player game currently does, but have IF() statements in some places to detect if you are the Host/Server/MasterClient and add remote RPC calls in other places to get either the Clients or Server to execute code in certain instances. You do have to learn the concept of Client/Host (Server) and understand how to control where code runs. 90% of code runs on the clients, and all machines but you will need to understand the concept and be able to have the Host/Server order Clients to do things, or update things.
One advice I can give, get used to thinking "My Host/Server/Masterclient orders every player's game to do things" and it will be easier if you use that mindset rather than "how do i decide when clients run code versus host/server/master".
In multiplayer, the Host/Server/Master ORDERS clients to do things, and they do it. Think of it that way whenever you say "how do I get the game to do this".
"How do I get the game to share a variable?" Order your clients to set the variable to the value you pass via RPC. (some networking solutions might make it easier with a State, or SyncVar, or other easy way)
"How do I get the game to show that one player picked up an item and equipped it?" Order your clients to show that the player picked up the item and equipped it. Some networking solutions already have an easy way to add a component that will synchronize the AnimatorController for you, otherwise you will need to send an RPC and make the clients set the Animator's parameters to play the animation. In either case, your RPC call would make it equip the item. If you code well, you can reuse the same functions like EquipItem() for both local and RPC calls.
Your answer
Follow this Question
Related Questions
How to find local player gameobject? c# 3 Answers
How to decide an asynchronous multiplayer solution for a turn based mobile chess game? 0 Answers
Multiplayer Networking Help 0 Answers
Calling NetworkManager.ServerChangeScene() Destroys Player 0 Answers
UNET NetIDs for Scene Objects Mismatched Between Client and Server 1 Answer