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:
- Added the player prefab as an object in the hiearchy, renamed it Player2 and disabled it. Do not remove its network components
- Duplicated the player controller script, renamed it and replaced the script on the Player2 obj.
- Modified the script to work localy by removing all the isLocalPlayer checks
- Modified the script to use different inputs than the player prefab
- 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)
- 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
}
}