Replace stripped-player spectator with a minimal dedicated prefab
Mid-match joiners now spawn a dedicated empty Spectator prefab (NetworkObject + SpectatorSetup only) instead of a fully stripped Player prefab. The FreeCam rig is a standalone scene object in Main, so the spectator needs no avatar body, renderer, collider, or CharacterController -- eliminating the leftover-body artifact and the fragile per-peer strip path entirely. - SpectatorSetup.cs (new): owner binds GameModeManager.BindSpectator (FreeCam-only) - Spectator.prefab (new): empty avatar (Transform + NetworkObject + SpectatorSetup), registered in DefaultNetworkPrefabs, wired on MatchManager in Main - GameModeManager: add BindSpectator/BuildSpectatorModes; drop the spectator branch from BindLocalPlayer; ReleaseToNoPlayer frees the cursor on teardown - MatchManager: independent entropy-seeded RNG for fair hunter pick (was sharing the global engine RNG and always landing on the same client); spawn the spectator prefab for mid-match joiners; cursor-safe return-to-Main teardown - FreeCamController: guard IgnorePlayerCollision against a disabled capsule Also bundled (unrelated user asset/UI edits): PaintHudPanel layout tuning, font atlas (arialroundedmtprocyr_bold SDF), forestpack_foliage material. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
be6db5b0a9
commit
91ce8ff329
12 files changed
+227
-1284
No files matched your search
@@ -39,8 +39,9 @@ using UnityEngine.InputSystem;
|
||||
/// End / join / teardown:
|
||||
/// - The host presses Esc -> <see cref="EndMatch"/>: Test Map is swapped back to Lobby and everyone is
|
||||
/// respawned as the neutral Player. Server-gated, so only the host can end the match.
|
||||
/// - A client that joins MID-MATCH is spectator-held (no avatar) - NGO still streams the running map to
|
||||
/// it, but it doesn't play; it spawns into the lobby when the match ends.
|
||||
/// - A client that joins MID-MATCH spawns as a SPECTATOR: a dedicated EMPTY prefab (no body/collider)
|
||||
/// bound to free-cam-only, so it flies around the running map and watches but does not play. It
|
||||
/// respawns as a neutral lobby Player when the match ends.
|
||||
/// - On shutdown / disconnect (<see cref="ReturnToMainOnly"/>) every gameplay scene is unloaded so only
|
||||
/// Main is left loaded & active. NGO's Dispose does NOT unload additively-synced scenes, so we do it.
|
||||
///
|
||||
@@ -68,6 +69,10 @@ public class MatchManager : MonoBehaviour
|
||||
[SerializeField] private GameObject hunterPrefab;
|
||||
[SerializeField] private GameObject hiderPrefab;
|
||||
|
||||
[Tooltip("Empty free-cam-only avatar spawned for a client that joins MID-MATCH. No body/collider - the " +
|
||||
"free-cam rig is a separate scene object. Must be a registered network prefab.")]
|
||||
[SerializeField] private GameObject spectatorPrefab;
|
||||
|
||||
[Header("Spawn")]
|
||||
[Tooltip("World position lobby avatars are spawned at (raised a touch above the floor so gravity " +
|
||||
"settles them). Per-client ring fan-out is applied on top (NetworkPlayerSetup.PlaceOnSpawnRing).")]
|
||||
@@ -86,6 +91,12 @@ public class MatchManager : MonoBehaviour
|
||||
private bool _sceneReady; // server-only: a gameplay scene (lobby/map) is loaded & active, so it is safe to spawn avatars
|
||||
private bool _cleanedUp; // guards the shutdown scene-cleanup so a host (server+client stop) doesn't run it twice
|
||||
|
||||
// Role pick uses its OWN generator, seeded once from real entropy. UnityEngine.Random is a single
|
||||
// engine-wide generator whose state at match-start can sit at the same point every fresh play
|
||||
// session (nothing random consumed before it), which made the "random" hunter land on the same
|
||||
// client every time. An independent System.Random decouples the pick from that shared global state.
|
||||
private readonly System.Random _roleRng = new System.Random(System.Guid.NewGuid().GetHashCode());
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance && Instance != this) { Destroy(this); return; }
|
||||
@@ -211,39 +222,38 @@ public class MatchManager : MonoBehaviour
|
||||
// Lobby is loaded and active on the server: now it is safe to spawn the already-connected
|
||||
// peers (the host, plus anyone who connected while the load was in flight).
|
||||
_sceneReady = true;
|
||||
foreach (ulong id in new List<ulong>(_net.ConnectedClientsIds)) SpawnPlayerFor(id, lobbySpawnCenter);
|
||||
foreach (ulong id in new List<ulong>(_net.ConnectedClientsIds)) SpawnFor(id, _playerPrefab, lobbySpawnCenter);
|
||||
}
|
||||
|
||||
// Server-side: a peer finished connecting. If a gameplay scene is already up, spawn it now;
|
||||
// otherwise LoadLobbyRoutine (lobby) or RespawnAll (mid-transition) covers it (the PlayerObject
|
||||
// guard in SpawnPlayerFor prevents a double spawn).
|
||||
// guard in SpawnFor prevents a double spawn).
|
||||
private void OnClientConnected(ulong clientId)
|
||||
{
|
||||
if (_net == null || !_net.IsServer) return;
|
||||
// During a Lobby<->Test Map swap the active scene briefly falls back to Main (no floor); defer
|
||||
// and let the respawn loop spawn this joiner once the target scene is loaded.
|
||||
if (!_sceneReady || Phase == MatchPhase.Transitioning) return;
|
||||
// Spectator-hold: a client that joins MID-MATCH gets NO avatar - they wait. NGO still streams the
|
||||
// running map to their client (unavoidable), but they don't play in it. When the host ends the
|
||||
// match, EndMatchSequence's respawn loop covers every connected client, so they spawn into the
|
||||
// lobby with everyone else then.
|
||||
if (Phase == MatchPhase.InMatch) return;
|
||||
SpawnPlayerFor(clientId, lobbySpawnCenter);
|
||||
// Mid-match joiner: spawn an empty SPECTATOR avatar (free-cam only) into the running map. They can
|
||||
// fly around and watch but don't play. When the host ends the match, EndMatchSequence's respawn
|
||||
// loop covers every connected client, so they turn into a neutral lobby Player with everyone else.
|
||||
if (Phase == MatchPhase.InMatch) { SpawnFor(clientId, spectatorPrefab, matchSpawnCenter); return; }
|
||||
SpawnFor(clientId, _playerPrefab, lobbySpawnCenter);
|
||||
}
|
||||
|
||||
// Spawns the neutral Player avatar for a client into the currently active gameplay scene. No-op if
|
||||
// the client already owns a player object (double-spawn guard) or has since disconnected.
|
||||
private void SpawnPlayerFor(ulong clientId, Vector3 at)
|
||||
// Spawns the given prefab as a client's player object into the currently active gameplay scene. No-op
|
||||
// if the client already owns a player object (double-spawn guard) or has since disconnected.
|
||||
private void SpawnFor(ulong clientId, GameObject prefab, Vector3 at)
|
||||
{
|
||||
if (!_playerPrefab)
|
||||
if (!prefab)
|
||||
{
|
||||
Debug.LogError("[MatchManager] Player prefab missing (was NetworkConfig.PlayerPrefab assigned?) - cannot spawn.", this);
|
||||
Debug.LogError("[MatchManager] Spawn prefab missing - cannot spawn.", this);
|
||||
return;
|
||||
}
|
||||
if (!_net.ConnectedClients.TryGetValue(clientId, out var client)) return;
|
||||
if (client.PlayerObject) return;
|
||||
|
||||
var go = Instantiate(_playerPrefab, at, Quaternion.identity);
|
||||
var go = Instantiate(prefab, at, Quaternion.identity);
|
||||
go.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
|
||||
}
|
||||
|
||||
@@ -263,7 +273,7 @@ public class MatchManager : MonoBehaviour
|
||||
if (ids.Count == 0) return;
|
||||
|
||||
Phase = MatchPhase.Transitioning;
|
||||
ulong hunterId = ids[Random.Range(0, ids.Count)];
|
||||
ulong hunterId = ids[_roleRng.Next(ids.Count)];
|
||||
Debug.Log($"[MatchManager] Starting match. Hunter = client {hunterId}, {ids.Count} player(s).", this);
|
||||
|
||||
StartCoroutine(MatchSequence(hunterId));
|
||||
@@ -376,6 +386,12 @@ public class MatchManager : MonoBehaviour
|
||||
|
||||
UnloadLocalIfLoaded(lobbySceneName);
|
||||
UnloadLocalIfLoaded(matchSceneName);
|
||||
|
||||
// The local avatar is gone; drop the mode cycle back to a player-less state so the persistent
|
||||
// orbit input stops re-locking the cursor (and any FreeCam/First-Person takeover is undone). Without
|
||||
// this the OS cursor stays captured back in the empty Main scene.
|
||||
var modes = FindFirstObjectByType<GameModeManager>(FindObjectsInactive.Include);
|
||||
if (modes) modes.ReleaseToNoPlayer();
|
||||
}
|
||||
|
||||
private void UnloadLocalIfLoaded(string sceneName)
|
||||
|
||||
Reference in new issue
Block a user