Skip to content

Commit 26d7d67

Browse files
fix: non-authority player instance spawns with no motion (#281)
* fix Fixes issue where a non-authority player instance could improperly initialize when two clients are connecting to the same session at the "relative" same time. Removing spam logging on collision messages. * update Added change log entries. --------- Co-authored-by: fernando-cortez <[email protected]>
1 parent 484c41c commit 26d7d67

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

Basic/DistributedAuthoritySocialHub/Assets/Scripts/Gameplay/PlayerSpawner.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,32 @@ class PlayerSpawner : NetworkBehaviour
88
[SerializeField]
99
NetworkObject m_PlayerPrefab;
1010

11+
protected override void OnNetworkPostSpawn()
12+
{
13+
if (NetworkManager.LocalClient.IsSessionOwner)
14+
{
15+
SpawnPlayer();
16+
}
17+
base.OnNetworkPostSpawn();
18+
}
19+
1120
protected override void OnNetworkSessionSynchronized()
1221
{
1322
Debug.Assert(m_PlayerPrefab != null, $"Prefab reference '{nameof(m_PlayerPrefab)}' is missing or not assigned.");
23+
if (!NetworkManager.LocalClient.IsSessionOwner)
24+
{
25+
SpawnPlayer();
26+
}
27+
base.OnNetworkSessionSynchronized();
28+
}
1429

30+
private void SpawnPlayer()
31+
{
1532
if (m_PlayerPrefab != null)
1633
{
1734
var spawnPoint = PlayerSpawnPoints.Instance.GetRandomSpawnPoint();
1835
m_PlayerPrefab.InstantiateAndSpawn(networkManager: NetworkManager, ownerClientId: NetworkManager.LocalClientId, isPlayerObject: true, position: spawnPoint.position, rotation: spawnPoint.rotation);
1936
}
20-
21-
base.OnNetworkSessionSynchronized();
2237
}
2338
}
2439
}

Basic/DistributedAuthoritySocialHub/Assets/Scripts/Physics/BaseObjectMotionHandler.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ class BaseObjectMotionHandler : NetworkTransform, ICollisionHandler, IContactEve
2727

2828
protected NetworkRigidbody NetworkRigidbody { get; private set; }
2929

30-
[Tooltip("Enables/Disables collision logging (based on per derived type)")]
30+
[Tooltip("Enables/Disables collision logging (based on per derived type).")]
3131
[SerializeField]
3232
protected bool m_DebugCollisions;
3333

34-
[Tooltip("Enables/Disables damage logging (based on per derived type)")]
34+
[Tooltip("Enables/Disables damage logging (based on per derived type).")]
3535
[SerializeField]
3636
protected bool m_DebugDamage;
3737

38+
[Tooltip("Enables/Disables general message logging (it is verbose and can generate a lot of messages).")]
39+
[SerializeField]
40+
protected bool m_GeneralLogMessages;
41+
3842
[Tooltip("Add all colliders to this list that will be used to detect collisions (exclude triggers).")]
3943
[SerializeField]
4044
List<Collider> m_Colliders;
@@ -193,7 +197,10 @@ public void HandleCollisionRpc(CollisionMessageInfo collisionMessage, RpcParams
193197
// If authority changes while this message is in flight, forward it to the new authority
194198
if (!HasAuthority)
195199
{
196-
LogMessage($"[HandleCollisionRpc][Not Owner][Routing Collision][{name}] Routing to Client-{OwnerClientId}");
200+
if (m_GeneralLogMessages)
201+
{
202+
LogMessage($"[HandleCollisionRpc][Not Owner][Routing Collision][{name}] Routing to Client-{OwnerClientId}");
203+
}
197204
SendCollisionMessage(m_CollisionMessage);
198205
return;
199206
}

Basic/DistributedAuthoritySocialHub/Assets/Scripts/Player/AvatarTransform.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ class AvatarTransform : PhysicsObjectMotion, INetworkUpdateSystem
2727
NetworkVariable<FixedString32Bytes> m_PlayerName = new NetworkVariable<FixedString32Bytes>(string.Empty, readPerm: NetworkVariableReadPermission.Everyone, writePerm: NetworkVariableWritePermission.Owner);
2828
NetworkVariable<FixedString32Bytes> m_PlayerId = new NetworkVariable<FixedString32Bytes>(string.Empty, readPerm: NetworkVariableReadPermission.Everyone, writePerm: NetworkVariableWritePermission.Owner);
2929

30-
3130
public override void OnNetworkSpawn()
3231
{
32+
// Always invoke base.OnNetworkSpawn before any other script.
33+
base.OnNetworkSpawn();
3334
gameObject.name = $"[Client-{OwnerClientId}]{name}";
3435

3536
m_TopUIController = FindFirstObjectByType<PlayersTopUIController>();
3637
m_PlayerName.OnValueChanged += OnPlayerNameChanged;
3738
m_PlayerId.OnValueChanged += OnPlayerIdChanged;
3839
OnPlayerNameChanged(string.Empty, m_PlayerName.Value);
3940

40-
if (!HasAuthority)
41+
if (!CanCommitToTransform)
4142
{
42-
base.OnNetworkSpawn();
4343
return;
4444
}
4545

@@ -66,8 +66,6 @@ public override void OnNetworkSpawn()
6666
{
6767
Debug.LogError("CameraControl not found on the Main Camera or Main Camera is missing.");
6868
}
69-
70-
base.OnNetworkSpawn();
7169
}
7270

7371
public override void OnNetworkDespawn()
@@ -114,12 +112,12 @@ void OnTransformUpdate()
114112

115113
void OnPlayerNameChanged(FixedString32Bytes previousValue, FixedString32Bytes newValue)
116114
{
117-
m_TopUIController.AddOrUpdatePlayer(gameObject, newValue.Value,m_PlayerId.Value.Value);
115+
m_TopUIController.AddOrUpdatePlayer(gameObject, newValue.Value, m_PlayerId.Value.Value);
118116
}
119117

120118
void OnPlayerIdChanged(FixedString32Bytes previousValue, FixedString32Bytes newValue)
121119
{
122-
m_TopUIController.AddOrUpdatePlayer(gameObject, m_PlayerName.Value.Value,newValue.Value);
120+
m_TopUIController.AddOrUpdatePlayer(gameObject, m_PlayerName.Value.Value, newValue.Value);
123121
}
124122

125123
public void NetworkUpdate(NetworkUpdateStage updateStage)

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [unreleased] yyyy-mm-dd
44

5+
### Distributed Authority Social Hub
6+
7+
#### Fixed
8+
- Fixed issue where a non-authority player instance could improperly initialize when two clients are connecting to the same session at the "relative" same time. (#281)
9+
- Fixed issue with spam logging on collision messages. (#281)
10+
511
### Multiplayer Use Cases
612

713
#### Added

0 commit comments

Comments
 (0)