Skip to content

Commit 390c332

Browse files
fix: player spawn not applying position and rotation via connection approval (#3078)
* fix Apply the position and rotation set by the NetworkManager.ConnectionApprovalResponse when connection approval is enabled and auto-spawn player prefabs is enabled. * test Validation test for this PR * update adding changelog entry
1 parent ef9e1c1 commit 390c332

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1212

1313
### Fixed
1414

15+
- Fixed issue where applying the position and/or rotation to the `NetworkManager.ConnectionApprovalResponse` when connection approval and auto-spawn player prefab were enabled would not apply the position and/or rotation when the player prefab was instantiated. (#3078)
1516
- Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. (#3075)
1617

1718
### Changed

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,8 @@ internal NetworkObject GetNetworkObjectToSpawn(uint globalObjectIdHash, ulong ow
752752
}
753753
else
754754
{
755-
// Create prefab instance
756-
networkObject = UnityEngine.Object.Instantiate(networkPrefabReference).GetComponent<NetworkObject>();
755+
// Create prefab instance while applying any pre-assigned position and rotation values
756+
networkObject = UnityEngine.Object.Instantiate(networkPrefabReference, position, rotation).GetComponent<NetworkObject>();
757757
networkObject.NetworkManagerOwner = NetworkManager;
758758
networkObject.PrefabGlobalObjectIdHash = globalObjectIdHash;
759759
}

com.unity.netcode.gameobjects/Tests/Runtime/ConnectionApproval.cs

+38-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using NUnit.Framework;
66
using Unity.Netcode.TestHelpers.Runtime;
7+
using UnityEngine;
78
using UnityEngine.TestTools;
89

910
namespace Unity.Netcode.RuntimeTests
@@ -12,9 +13,10 @@ namespace Unity.Netcode.RuntimeTests
1213
[TestFixture(PlayerCreation.PrefabHash)]
1314
[TestFixture(PlayerCreation.NoPlayer)]
1415
[TestFixture(PlayerCreation.FailValidation)]
15-
internal class ConnectionApprovalTests : NetcodeIntegrationTest
16+
internal class ConnectionApprovalTests : IntegrationTestWithApproximation
1617
{
1718
private const string k_InvalidToken = "Invalid validation token!";
19+
1820
public enum PlayerCreation
1921
{
2022
Prefab,
@@ -24,6 +26,8 @@ public enum PlayerCreation
2426
}
2527
private PlayerCreation m_PlayerCreation;
2628
private bool m_ClientDisconnectReasonValidated;
29+
private Vector3 m_ExpectedPosition;
30+
private Quaternion m_ExpectedRotation;
2731

2832
private Dictionary<ulong, bool> m_Validated = new Dictionary<ulong, bool>();
2933

@@ -43,6 +47,12 @@ protected override bool ShouldCheckForSpawnedPlayers()
4347

4448
protected override void OnServerAndClientsCreated()
4549
{
50+
if (m_PlayerCreation == PlayerCreation.Prefab || m_PlayerCreation == PlayerCreation.PrefabHash)
51+
{
52+
m_ExpectedPosition = GetRandomVector3(-10.0f, 10.0f);
53+
m_ExpectedRotation = Quaternion.Euler(GetRandomVector3(-359.98f, 359.98f));
54+
}
55+
4656
m_ClientDisconnectReasonValidated = false;
4757
m_BypassConnectionTimeout = m_PlayerCreation == PlayerCreation.FailValidation;
4858
m_Validated.Clear();
@@ -104,11 +114,36 @@ private bool ClientAndHostValidated()
104114
return true;
105115
}
106116

117+
private bool ValidatePlayersPositionRotation()
118+
{
119+
foreach (var playerEntries in m_PlayerNetworkObjects)
120+
{
121+
foreach (var player in playerEntries.Value)
122+
{
123+
if (!Approximately(player.Value.transform.position, m_ExpectedPosition))
124+
{
125+
return false;
126+
}
127+
if (!Approximately(player.Value.transform.rotation, m_ExpectedRotation))
128+
{
129+
return false;
130+
}
131+
}
132+
}
133+
return true;
134+
}
135+
107136
[UnityTest]
108137
public IEnumerator ConnectionApproval()
109138
{
110139
yield return WaitForConditionOrTimeOut(ClientAndHostValidated);
111140
AssertOnTimeout("Timed out waiting for all clients to be approved!");
141+
142+
if (m_PlayerCreation == PlayerCreation.Prefab || m_PlayerCreation == PlayerCreation.PrefabHash)
143+
{
144+
yield return WaitForConditionOrTimeOut(ValidatePlayersPositionRotation);
145+
AssertOnTimeout("Not all player prefabs spawned in the correct position and/or rotation!");
146+
}
112147
}
113148

114149
private void NetworkManagerObject_ConnectionApprovalCallback(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
@@ -127,8 +162,8 @@ private void NetworkManagerObject_ConnectionApprovalCallback(NetworkManager.Conn
127162
}
128163

129164
response.CreatePlayerObject = ShouldCheckForSpawnedPlayers();
130-
response.Position = null;
131-
response.Rotation = null;
165+
response.Position = m_ExpectedPosition;
166+
response.Rotation = m_ExpectedRotation;
132167
response.PlayerPrefabHash = m_PlayerCreation == PlayerCreation.PrefabHash ? m_PlayerPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash : null;
133168
}
134169

0 commit comments

Comments
 (0)