Syncing Xevious enemies with MLAPI

The basic synchronisation

The core of sync’ing enemies with MLAPI is to spawn them.

Network Object Component

Transformation Synchronisation

MLAPI has automatic support for transformation synchronisation via the Network Transform component. Although I have shown in Syncing Positions that sometimes it might be better to be less, “network chatty” I did use this automatic support for my Toroid Enemy.

Network Transform.
Basic Sync demo

Pooling Objects

Whilst the initial demo is okay, it is generating and destroying objects on the fly. We know this is not a great idea, fortunately MLAPI does provide an object pooling mechanism to avoid angering the Garbage Collecting Gods.

Object Pooling
  1. The handler only fires on the Client. I admit this confused for me for a while.
  2. The RegisterSpawnHandler does NOT have any concept of type. Therefore when you RegisterSpawnHandler is invoked you have no idea what prefab it has been invoked for. Therefore writing a single delegate to handle all the calls for any pooled object type is not possible. MLAPI wants you to have a pool per object, rather than a pool that can manage multiple objects. Therefore in the above code I am using closures to create a delegate per pooled object type, the prefab name is captured locally before the above snippet.
Pooled Demo

Animations and Fly-ins

There are two obvious flaws in the previous demos. If you compare the Toroids in Player 2 with Player 1, you can see that they are not spinning, i.e. they are not animating. MLAPI contains Network Animator which is a sync for the animation controller, although it does not have the same granularity of control as the Photon PUN version.

Network Animator Component
gameObject.SetActive(false);
gameObject.transform.position += Vector3.up * 100f;
Final Demo

Where next?

The next challenges will be implementing:

  • the enemy bullets — should these still be just sync’ed tranforms & animations?
  • basic ground targets. — ground targets must be a child of the scrolling background and must move using local rather than world position.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Paul Marsh

Paul Marsh

Unity, VR, Enterprise and .Net Developer