Thursday, March 31, 2016

JetMechs: A Cross Platform Multiplayer Prototype using Unity uNet

A Cross Platform Multiplayer Prototype

I wanted to try Unity's networking system uNet.

So I dug up an old project, and went to work making it have multiplayer support!

Unity's uNet system allows users on Mac, Windows and Android all to join the same match and play together.

This prototype is playable on Android, by using an Xbox 360 controller and an OnTheGo cable and a Google Cardboard or equivalent.

I got stuck on a few bottlenecks along the way!
I mainly had trouble because I had two overlapping problems!

The aiming of the player's weapon was not syncing across the network, so the bullet was shooting straight.  This was caused by the local rotation of the weapon was not being synced across the server.  This can be fixed by adding a Network Transform Child to the root parent object (the Player for example, which needs to have its own "master" Network Transform on it), and drag and drop the child (eg: the weapon) onto the available slot on the Network Transform Child component.  Add a Network Identity to the child (weapon) as well.  And now it should sync across the network.

But my bug was also being confused by the fact that I had used Unity's FirstPersonController to base my original single player mech prototype on.  So I had to spend some time digging around 4 or 5 scripts to find all the local player specific code, such as input and movement, and applying the (isLocalPlayer) if statements around the appropriate stuff!

I'd done a pass at the player controls, but only later I realised that I hadn't completely separated out the MouseLook and SimpleMouseRotator snippets.  That had meant that the player prefabs had been stomping on each other's mouse look values, causing problems with weapon firing. 

Once I'd sorted out both those issues, I got it all working!  Hooray!

I'm pleased to announce that tonight I finally got all the basic multiplayer networking correctly set up!

The players can join and jetpack and shoot, across the network!

Also, part of the challenge I'd set myself was to build the foundations that would support Google Cardboard, Oculus Rift, SteamVR, etc.  So I detect what platform you're on and activate the appropriate camera system.

Each player has a torch attached, which moves with the way their head is facing.

I'm thinking of making the levels darker, so the torch is more important and relevant.