Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by tcz8 · Nov 30, 2016 at 07:37 AM · multiplayermultiplayer-networkingarchitectureframework

How do you add Networked Multiplayer to a game while preserving single player?

I understand how to implement multiplayer. What I am not sure about is how I add it to my game wihtout butchering the single player mode.

All those extra network componenets and script modifications are getting in the way. Seems like the simplest way would be to make 2 games in one by using two sets of scripts and gameobjects but that feels wrong to me.

There must be a better way, how do you pros do it?

To give you an Idea, I am working on a simple game is based on pong. It currently has couch multiplayer implemented using multiple controlers and I have an AI for single player.

Thank you for reading!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bryangs · Nov 30, 2016 at 10:17 PM 0
Share

Up, i really wanted to know this too

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tcz8 · Dec 01, 2016 at 05:15 PM

I did some testing and I think It's much faster to develop for network multiplayer all the way and simply enable an AI or second player object for local games. Actually got that running in no time. I kinda get why a lot of games are built that way now.

Here is what I did for the 2nd local player:

  1. Added the player prefab as an object in the hiearchy, renamed it Player2 and disabled it. Do not remove its network components

  2. Duplicated the player controller script, renamed it and replaced the script on the Player2 obj.

  3. Modified the script to work localy by removing all the isLocalPlayer checks

  4. Modified the script to use different inputs than the player prefab

  5. Added code to my Game Manager script that enables the Player2 object when localGame = true (it's a custom var, I set it to true when launching the game in local mode)

  6. With a little extra modifications you could even have the 2 local players join a network match!

I will do something similar for the AI player. This setup has the advantage of everything else working as is without modifications.

All is left to do is to launch the local network game via script instead of using the network hud, effectively hiding that it's running a local server in the background. I remember reading how its done in the manual.

The proxy solution would work too, modifying the player controller script to work with another gameobject instead of the object its attached too is easy but that's were the simplicity ends.

It doesn't take care of all the network components and scripts that uses NetworkBehaviour instead of MonoBehaviour. A lot of gamobjects would need to be duplicated and modified. It's almost like building two games.

The proxy concept would be excellent when you want network users to control player objects allready in the scene instead of having the network manager spawn them. You could set the Network Manager to spawn empty prefabs with a controller script attached that finds an unused player object in the scene and start controlling it via a gameobject variable.

Hope this answer can help others.

@christoph_r Thank you for getting my brain thinking! Ill upvote you. If you dont agree with my choice of answer please share your thoughts.

EDIT

So I ended up being able to merge all the player controller scripts into one, makes it much easier to maintain things.

@christoph_r 's suggestion of a proxy control scheme is a very good idea, I will definitly use it for vehicules or other controllable objects in the future. There may even be a use for player controls depending on the type of game.

As for starting the game in local mode while being coded as a networked game, all you need to do is put this into a script in your scene and make sure isLocalGame is set to true. Dont forget to put using UnityEngine.Networking; at the top of the script.

     void Start () {
         if (isLocalGame) {
             NetworkManager.singleton.StartHost(); // Starts the game in local host mode
         }
     }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image christoph_r · Dec 01, 2016 at 08:27 PM 1
Share

I'd be a bit tentative to declare what worked best for you to be the general answer to such a broad question. For example, by having two scripts that do very similar / the same things but with different input scales very badly. While this might not be a big issue with small projects you're working on by yourself, this is a huge disadvantage in larger projects. While the setup of the scene may be a bit more complex when separating these two, code maintenance is much, much easier.

avatar image tcz8 christoph_r · Dec 02, 2016 at 05:51 PM 0
Share

Actually, after prototyping the solution with a duplicate player controller script I merged the modifications and now use only one script.

I think I understand what you mean, it allows changing the control scheme easily depending on what you are controlling. I guess for games with vehicules or other controllable objects it's the way to go.

The question was "how to add network multi and preserve singleplayer" and your answer only deals with controls that's why I chose my answer.

avatar image
2

Answer by Yay_For_Humzah · Dec 02, 2016 at 06:20 PM

I agree and don't really agree with @christoph_r. Yes there should be different inputs, no...it shouldn't be different scripts. You should basically have an if else statement in your update that chooses what input you have. Then have functions in those if statements. So if (you are using network){//Control with network}, etc.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by christoph_r · Nov 30, 2016 at 10:25 PM

Consider which aspects of your game may be driven/affected by other players. Make sure to create interfaces for those aspects that allow you to easily switch between having AI/other scripts or in fact other players control them. In the example of a pong game, your controller for the racket should take in a very generic input (such as a float for going up or down). Then you can create multiple scripts that drive that controller, one that takes input from a local user's keyboard, one that is AI and one that is driven by a remote player.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tcz8 · Dec 01, 2016 at 02:57 AM 0
Share

So If I understand this right, I need to set the paddle control script to be a kind of "proxy" and use a different script in multiplayer and singleplayer to take control via that proxy.

That makes sense, but what about the whole network manager that wants to instance player prefabs, the network transforms, the network identity and all other network components that require to be on different game objects?

I remember seeing games that in singleplayer were actually running in multiplayer in the background but on a local server with AI. They were simply disabling match making. Is that possible with Unity and should I even go that route?

avatar image christoph_r · Dec 01, 2016 at 09:37 AM 1
Share

Yes, that's basically it. As for setting up a system running on a local server: Consider what works better for your individual project. With a relatively simple game like pong swapping out the scripts seems like the quicker and easier option. Your game's focus should also be an important factor, if single player mode is more of a gimmick for when the connection is out, doing it via local server would probably make debugging and testing easier. Either way, abstracting game logic from input, be it local user input, AI or remote input is generally desirable, no matter which path you take.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Synchronize damage dealing and block in melee combat multiplayer game 2 Answers

RPC Calls in 5.1 0 Answers

Multiplayer - How to assign different sprites to players? 0 Answers

I am using NetworkDiscovery class of Unity to host and listen for a game session. The server broadcasts the game and the client is able to find a game hosted by the server, but am not able to connect to the server at all 0 Answers

Multiplayer advanced - Single Player in Multiplayer game, using another Hosting services. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges