Add NGO networking with owner-authoritative player position sync
Install Netcode for GameObjects 2.13.0 + Unity Transport 2.6.0 and wire up the first networking milestone: syncing player position/rotation across clients. - NetworkBootstrap: Host/Client/Server controls (OnGUI + H/C hotkeys) for local clone testing over loopback 127.0.0.1:7777. - NetworkPlayerSetup: per-avatar owner gating. The local owner binds the GameModeManager and cameras to itself; remote avatars disable their controller/CharacterController and receive transform via NetworkTransform. - GameModeManager.BindLocalPlayer: central runtime rebind to the locally spawned player. - FirstPersonController.SetPlayer: explicit runtime rebind hook. - Player prefab: NetworkObject + NetworkTransform (owner-authoritative, position + rotation) + NetworkPlayerSetup; registered as PlayerPrefab. - Scene: NetworkManager + UnityTransport added; the fixed scene player instance removed in favor of spawn-on-connect. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
c0ec65058b
commit
4fa443f630
13 files changed
+435
-91
No files matched your search
@@ -175,6 +175,22 @@ public class FirstPersonController : MonoBehaviour
|
||||
if (r) r.enabled = !hidden;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicitly binds this rig to the locally-owned networked player (overriding the scene-wide
|
||||
/// auto-resolve, which could otherwise latch onto a remote avatar). Called by the GameModeManager
|
||||
/// when the local player spawns.
|
||||
/// </summary>
|
||||
public void SetPlayer(PlayerCharacterController p)
|
||||
{
|
||||
if (!p) return;
|
||||
_player = p;
|
||||
_poseController = p.GetComponent<PlayerPoseController>();
|
||||
Transform t = p.transform.Find("CameraTarget");
|
||||
if (t) cameraTarget = t;
|
||||
hiddenRenderers = p.GetComponentsInChildren<SkinnedMeshRenderer>(true);
|
||||
_resolved = true;
|
||||
}
|
||||
|
||||
void ResolveReferences()
|
||||
{
|
||||
if (_resolved) return;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Unity.Cinemachine;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
@@ -108,6 +109,41 @@ public class GameModeManager : MonoBehaviour
|
||||
m.Bind(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds the scene singletons (this manager, the orbit/first-person cameras, the painter) to the
|
||||
/// locally-owned networked player after it spawns, then enters Player Control. Called from
|
||||
/// <see cref="NetworkPlayerSetup"/> on the owning client. Before this runs the scene has no player,
|
||||
/// so the cycle sits in a player-less mode (Paint); this promotes it once the avatar exists.
|
||||
/// </summary>
|
||||
public void BindLocalPlayer(PlayerCharacterController localPlayer)
|
||||
{
|
||||
if (!localPlayer) return;
|
||||
player = localPlayer;
|
||||
|
||||
// The painter lives on the player (per-avatar), so bind the local one.
|
||||
if (!painter) painter = localPlayer.GetComponentInChildren<MeshPainter>(true);
|
||||
|
||||
Transform camTarget = localPlayer.transform.Find("CameraTarget");
|
||||
|
||||
// Re-point the third-person orbit camera (Cinemachine Follow/LookAt) at the local avatar.
|
||||
if (cameraInput)
|
||||
{
|
||||
var vcam = cameraInput.GetComponent<CinemachineCamera>();
|
||||
if (vcam) { vcam.Follow = camTarget; vcam.LookAt = camTarget; }
|
||||
}
|
||||
|
||||
if (firstPerson)
|
||||
firstPerson.SetPlayer(localPlayer);
|
||||
|
||||
// Tear down the pre-spawn (player-less) mode and rebuild with Player Control available.
|
||||
ActiveMode?.Exit();
|
||||
_activeIndex = -1;
|
||||
BuildModes();
|
||||
|
||||
int idx = _modes.FindIndex(m => m is PlayerControlMode);
|
||||
SwitchTo(idx >= 0 ? idx : Mathf.Clamp(startModeIndex, 0, _modes.Count - 1));
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_modes.Count == 0) return;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Dev-only connection harness for local multiplayer testing across the junction-mirror clones:
|
||||
/// one instance clicks Host, the others click Client (loopback 127.0.0.1 via the configured
|
||||
/// UnityTransport). Draws a small on-screen panel and exposes hotkeys (H = Host, C = Client,
|
||||
/// J = Server, K = Shutdown). Purely a testing scaffold — replace with real lobby/menu UI later.
|
||||
/// Attach to the same GameObject as the NetworkManager.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(NetworkManager))]
|
||||
[DisallowMultipleComponent]
|
||||
public class NetworkBootstrap : MonoBehaviour
|
||||
{
|
||||
[Tooltip("Draw the on-screen Host/Client panel (top-left).")]
|
||||
public bool showGui = true;
|
||||
|
||||
[Tooltip("Allow the H/C/J/K keyboard shortcuts.")]
|
||||
public bool enableHotkeys = true;
|
||||
|
||||
private NetworkManager _net;
|
||||
|
||||
private void Awake() => _net = GetComponent<NetworkManager>();
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!enableHotkeys) return;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
var kb = Keyboard.current;
|
||||
if (kb == null || _net == null) return;
|
||||
if (!_net.IsListening)
|
||||
{
|
||||
if (kb.hKey.wasPressedThisFrame) _net.StartHost();
|
||||
else if (kb.cKey.wasPressedThisFrame) _net.StartClient();
|
||||
else if (kb.jKey.wasPressedThisFrame) _net.StartServer();
|
||||
}
|
||||
else if (kb.kKey.wasPressedThisFrame)
|
||||
{
|
||||
_net.Shutdown();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (!showGui || _net == null) return;
|
||||
|
||||
GUILayout.BeginArea(new Rect(10, 10, 220, 160), GUI.skin.box);
|
||||
if (!_net.IsListening)
|
||||
{
|
||||
GUILayout.Label("Netcode (dev)");
|
||||
if (GUILayout.Button("Host (H)")) _net.StartHost();
|
||||
if (GUILayout.Button("Client (C)")) _net.StartClient();
|
||||
if (GUILayout.Button("Server (J)")) _net.StartServer();
|
||||
}
|
||||
else
|
||||
{
|
||||
string role = _net.IsHost ? "Host" : _net.IsServer ? "Server" : "Client";
|
||||
GUILayout.Label($"Role: {role}");
|
||||
GUILayout.Label($"ClientId: {_net.LocalClientId}");
|
||||
GUILayout.Label($"Clients: {(_net.IsServer ? _net.ConnectedClients.Count : (_net.IsConnectedClient ? 1 : 0))}");
|
||||
if (GUILayout.Button("Shutdown (K)")) _net.Shutdown();
|
||||
}
|
||||
GUILayout.EndArea();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 519095f0598dfdf4ebe457561f77771c
|
||||
@@ -0,0 +1,32 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Per-avatar network glue on the spawned player. The local owner binds the scene singletons
|
||||
/// (GameModeManager, cameras) to itself via <see cref="GameModeManager.BindLocalPlayer"/>; remote
|
||||
/// avatars stop simulating so their transform is driven purely by the NetworkTransform replication.
|
||||
/// Position-sync milestone: remote avatars still show their mesh/idle pose, just no local control.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(PlayerCharacterController))]
|
||||
[DisallowMultipleComponent]
|
||||
public class NetworkPlayerSetup : NetworkBehaviour
|
||||
{
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
var controller = GetComponent<PlayerCharacterController>();
|
||||
|
||||
if (IsOwner)
|
||||
{
|
||||
var manager = FindFirstObjectByType<GameModeManager>(FindObjectsInactive.Include);
|
||||
if (manager)
|
||||
manager.BindLocalPlayer(controller);
|
||||
return;
|
||||
}
|
||||
|
||||
// Remote avatar: NetworkTransform owns the transform here, so disable the local
|
||||
// simulation (input/movement) and the CharacterController's own depenetration.
|
||||
if (controller) controller.enabled = false;
|
||||
var cc = GetComponent<CharacterController>();
|
||||
if (cc) cc.enabled = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fce6d601a5d475943bc43d2abd75570d
|
||||
Reference in new issue
Block a user