Netcode for gameobjects Multiplayer¶
If you wish to use Prime Ball Controller in Netcode for gameobjects there are a few changes that must be made. This is in no way an all inclusive guide but will get you started with a usable prefab. This guide does not teach Netcode, just the basic changes needed for Prime Ball Controller, it could still be optimized further.
Code changes¶
With Netcode, every player has every other player loaded in their scene. As a result the code runs for every Prefab even when you aren't the owner of that player. This is mainly an issue with the Cameras because each player has every other players' cameras and we need to make sure only your cameras are active.
- Begin by opening the SphereDirector script located at Magaeric Solutions->Prime Ball Controller->Player->Scripts->Directors
- Add the following code, this code effectively disables all cameras that don't belong to that player and then disables the director script itself preventing the controller factory scripts from running at all.
public override void OnNetworkSpawn()
{
if (!IsOwner)
{
DataManager.Cameras.PlayerCamera.gameObject.SetActive(false);
DataManager.Cameras.FreeLookCamera.gameObject.SetActive(false);
DataManager.Cameras.SideScrollCamera.gameObject.SetActive(false);
DataManager.Cameras.MagnetCamera.gameObject.SetActive(false);
DataManager.Cameras.ProjectileCamera.gameObject.SetActive(false);
DataManager.Cameras.OverheadCamera.gameObject.SetActive(false);
DataManager.Cameras.MagnetCeilingCamera.gameObject.SetActive(false);
DataManager.Cameras.StationaryCamera.gameObject.SetActive(false);
DataManager.Cameras.FirstPersonCamera.gameObject.SetActive(false);
}
enabled = IsOwner;
}
- Create a new script named Client Network Transform with the following contents, this will get assigned to the prefab in the next section:
using Unity.Netcode.Components;
public class ClientNetworkTransform : NetworkTransform
{
protected override bool OnIsServerAuthoritative()
{
return false;
}
}
Prefab changes¶
- Open the Player Prefab located at Magaeric Solutions->Prime Ball Controller->Prefabs
- Add a Network Object to the Top level Player component, the inspector should look something like this:
- Add the Client Network Transform Script you created above to the the Player Rigidbody Component, be sure to only sync the position on the x, y, and z axis, uncheck rotation and scale the inspector should look something like this:
- Also add the Client Network Transform Script to the Outer Mesh Component, be sure to sync the position and rotation, uncheck scale. It should look something like this:
Your prefab is now ready to be used with Netcode.