CharacterControllerSamples
CharacterControllerSamples copied to clipboard
Netcode tutorial doesn't handle multiple MainEntityCamera from different clients but uses HasSingleton<MainEntityCamera>
If you follow the directions in https://github.com/Unity-Technologies/CharacterControllerSamples/blob/master/_Documentation/Tutorial/tutorial-netcodecharacters.md
Everything works in the editor fine, that is until a second client connects. That's when that world creates a 2nd MainEntityCamera entity and the MainCameraSystem breaks.
Considering it's a tutorial for netcode it should be assumed it's for multiplayer and multiple clients.
[UpdateInGroup(typeof(PresentationSystemGroup))]
public partial class MainCameraSystem : SystemBase
{
private Entity _mainCameraEntity;
private bool _initialized;
protected override void OnUpdate()
{
if (!_initialized)
{
Initialize();
}
else
{
LocalToWorld targetLocalToWorld = SystemAPI.GetComponent<LocalToWorld>(_mainCameraEntity);
MainGameObjectCamera.Instance.transform.SetPositionAndRotation(targetLocalToWorld.Position, targetLocalToWorld.Rotation);
}
}
private void Initialize()
{
foreach (var (mainCameraEntity, entity) in SystemAPI.Query<RefRO<MainEntityCamera>>().WithAll<GhostOwnerIsLocal>().WithEntityAccess())
{
_mainCameraEntity = entity;
_initialized = true;
}
}
}
I modified the default MainCameraSystem.cs file in this way to get around it. Seems to work fine for me in the short term.