Skip to content

Commit 5c4b36c

Browse files
committed
fix: Fixed NetworkedTransform not working for non host servers
Fixes: #329
1 parent 0fbe3e9 commit 5c4b36c

File tree

1 file changed

+61
-117
lines changed

1 file changed

+61
-117
lines changed

MLAPI/Prototyping/NetworkedTransform.cs

Lines changed: 61 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,11 @@ private void Update()
148148
lastSendTime = NetworkingManager.Singleton.NetworkTime;
149149
lastSentPos = transform.position;
150150
lastSentRot = transform.rotation;
151-
using (PooledBitStream stream = PooledBitStream.Get())
152-
{
153-
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
154-
{
155-
writer.WriteSinglePacked(transform.position.x);
156-
writer.WriteSinglePacked(transform.position.y);
157-
writer.WriteSinglePacked(transform.position.z);
158-
159-
writer.WriteSinglePacked(transform.rotation.eulerAngles.x);
160-
writer.WriteSinglePacked(transform.rotation.eulerAngles.y);
161-
writer.WriteSinglePacked(transform.rotation.eulerAngles.z);
162-
163-
if (IsServer)
164-
InvokeClientRpcOnEveryoneExceptPerformance(ApplyTransform, OwnerClientId, stream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
165-
else
166-
InvokeServerRpcPerformance(SubmitTransform, stream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
167-
}
168-
}
169151

152+
if (IsServer)
153+
InvokeClientRpcOnEveryoneExcept(ApplyTransform, OwnerClientId, transform.position, transform.rotation, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
154+
else
155+
InvokeServerRpc(SubmitTransform, transform.position, transform.rotation, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
170156
}
171157
}
172158
else
@@ -199,111 +185,82 @@ private void Update()
199185
}
200186

201187
[ClientRPC]
202-
private void ApplyTransform(ulong clientId, Stream stream)
188+
private void ApplyTransform(Vector3 position, Quaternion rotation)
203189
{
204190
if (!enabled) return;
205-
using (PooledBitReader reader = PooledBitReader.Get(stream))
206-
{
207-
208-
float xPos = reader.ReadSinglePacked();
209-
float yPos = reader.ReadSinglePacked();
210-
float zPos = reader.ReadSinglePacked();
211-
212-
float xRot = reader.ReadSinglePacked();
213-
float yRot = reader.ReadSinglePacked();
214-
float zRot = reader.ReadSinglePacked();
215191

216-
if (InterpolatePosition)
217-
{
218-
lastRecieveTime = Time.unscaledTime;
219-
lerpStartPos = transform.position;
220-
lerpStartRot = transform.rotation;
221-
lerpEndPos = new Vector3(xPos, yPos, zPos);
222-
lerpEndRot = Quaternion.Euler(xRot, yRot, zRot);
223-
lerpT = 0;
224-
}
225-
else
226-
{
227-
transform.position = new Vector3(xPos, yPos, zPos);
228-
transform.rotation = Quaternion.Euler(new Vector3(xRot, yRot, zRot));
229-
}
192+
if (InterpolatePosition && (!IsServer || InterpolateServer))
193+
{
194+
lastRecieveTime = Time.unscaledTime;
195+
lerpStartPos = transform.position;
196+
lerpStartRot = transform.rotation;
197+
lerpEndPos = position;
198+
lerpEndRot = rotation;
199+
lerpT = 0;
200+
}
201+
else
202+
{
203+
transform.position = position;
204+
transform.rotation = rotation;
230205
}
231206
}
232207

233208
[ServerRPC]
234-
private void SubmitTransform(ulong clientId, Stream stream)
209+
private void SubmitTransform(Vector3 position, Quaternion rotation)
235210
{
236211
if (!enabled) return;
237-
using (PooledBitReader reader = PooledBitReader.Get(stream))
238-
{
239-
float xPos = reader.ReadSinglePacked();
240-
float yPos = reader.ReadSinglePacked();
241-
float zPos = reader.ReadSinglePacked();
242212

243-
float xRot = reader.ReadSinglePacked();
244-
float yRot = reader.ReadSinglePacked();
245-
float zRot = reader.ReadSinglePacked();
213+
if (IsMoveValidDelegate != null && !IsMoveValidDelegate(lerpEndPos, position))
214+
{
215+
//Invalid move!
216+
//TODO: Add rubber band (just a message telling them to go back)
217+
return;
218+
}
246219

247-
if (IsMoveValidDelegate != null && !IsMoveValidDelegate(lerpEndPos, new Vector3(xPos, yPos, zPos)))
248-
{
249-
//Invalid move!
250-
//TODO: Add rubber band (just a message telling them to go back)
251-
return;
252-
}
220+
if (!IsClient)
221+
{
222+
// Dedicated server
223+
ApplyTransform(position, rotation);
224+
}
253225

254-
using (PooledBitStream writeStream = PooledBitStream.Get())
226+
if (EnableRange)
227+
{
228+
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
255229
{
256-
using (PooledBitWriter writer = PooledBitWriter.Get(writeStream))
230+
if (!clientSendInfo.ContainsKey(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
257231
{
258-
writer.WriteSinglePacked(xPos);
259-
writer.WriteSinglePacked(yPos);
260-
writer.WriteSinglePacked(zPos);
261-
262-
writer.WriteSinglePacked(xRot);
263-
writer.WriteSinglePacked(yRot);
264-
writer.WriteSinglePacked(zRot);
265-
266-
if (EnableRange)
232+
clientSendInfo.Add(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo()
267233
{
268-
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
269-
{
270-
if (!clientSendInfo.ContainsKey(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
271-
{
272-
clientSendInfo.Add(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo()
273-
{
274-
clientId = NetworkingManager.Singleton.ConnectedClientsList[i].ClientId,
275-
lastMissedPosition = null,
276-
lastMissedRotation = null,
277-
lastSent = 0
278-
});
279-
}
234+
clientId = NetworkingManager.Singleton.ConnectedClientsList[i].ClientId,
235+
lastMissedPosition = null,
236+
lastMissedRotation = null,
237+
lastSent = 0
238+
});
239+
}
280240

281-
ClientSendInfo info = clientSendInfo[NetworkingManager.Singleton.ConnectedClientsList[i].ClientId];
282-
Vector3? receiverPosition = NetworkingManager.Singleton.ConnectedClientsList[i].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClientsList[i].PlayerObject.transform.position);
283-
Vector3? senderPosition = NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position);
241+
ClientSendInfo info = clientSendInfo[NetworkingManager.Singleton.ConnectedClientsList[i].ClientId];
242+
Vector3? receiverPosition = NetworkingManager.Singleton.ConnectedClientsList[i].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClientsList[i].PlayerObject.transform.position);
243+
Vector3? senderPosition = NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position);
284244

285-
if ((receiverPosition == null || senderPosition == null && NetworkingManager.Singleton.NetworkTime - info.lastSent >= (1f / FixedSendsPerSecond)) || NetworkingManager.Singleton.NetworkTime - info.lastSent >= GetTimeForLerp(receiverPosition.Value, senderPosition.Value))
286-
{
287-
info.lastSent = NetworkingManager.Singleton.NetworkTime;
288-
info.lastMissedPosition = null;
289-
info.lastMissedRotation = null;
245+
if ((receiverPosition == null || senderPosition == null && NetworkingManager.Singleton.NetworkTime - info.lastSent >= (1f / FixedSendsPerSecond)) || NetworkingManager.Singleton.NetworkTime - info.lastSent >= GetTimeForLerp(receiverPosition.Value, senderPosition.Value))
246+
{
247+
info.lastSent = NetworkingManager.Singleton.NetworkTime;
248+
info.lastMissedPosition = null;
249+
info.lastMissedRotation = null;
290250

291-
InvokeClientRpcOnClientPerformance(ApplyTransform, NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, writeStream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
292-
}
293-
else
294-
{
295-
info.lastMissedPosition = new Vector3(xPos, yPos, zPos);
296-
info.lastMissedRotation = Quaternion.Euler(xRot, yRot, zRot);
297-
}
298-
}
299-
}
300-
else
301-
{
302-
InvokeClientRpcOnEveryoneExceptPerformance(ApplyTransform, OwnerClientId, writeStream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
303-
}
251+
InvokeClientRpcOnClient(ApplyTransform, NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, position, rotation, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
252+
}
253+
else
254+
{
255+
info.lastMissedPosition = position;
256+
info.lastMissedRotation = rotation;
304257
}
305258
}
306259
}
260+
else
261+
{
262+
InvokeClientRpcOnEveryoneExcept(ApplyTransform, OwnerClientId, position, rotation, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
263+
}
307264
}
308265

309266
private void CheckForMissedSends()
@@ -327,29 +284,16 @@ private void CheckForMissedSends()
327284
if ((receiverPosition == null || senderPosition == null && NetworkingManager.Singleton.NetworkTime - info.lastSent >= (1f / FixedSendsPerSecond)) || NetworkingManager.Singleton.NetworkTime - info.lastSent >= GetTimeForLerp(receiverPosition.Value, senderPosition.Value))
328285
{
329286
Vector3? pos = NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position);
330-
Vector3? rot = NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.rotation.eulerAngles);
287+
Quaternion? rot = NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Quaternion?(NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.rotation);
331288

332289
if (pos != null && rot != null)
333290
{
334291
info.lastSent = NetworkingManager.Singleton.NetworkTime;
335292
info.lastMissedPosition = null;
336293
info.lastMissedRotation = null;
337294

338-
using (PooledBitStream stream = PooledBitStream.Get())
339-
{
340-
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
341-
{
342-
writer.WriteSinglePacked(pos.Value.x);
343-
writer.WriteSinglePacked(pos.Value.y);
344-
writer.WriteSinglePacked(pos.Value.z);
345-
346-
writer.WriteSinglePacked(rot.Value.x);
347-
writer.WriteSinglePacked(rot.Value.y);
348-
writer.WriteSinglePacked(rot.Value.z);
349295

350-
InvokeClientRpcOnClientPerformance(ApplyTransform, NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, stream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
351-
}
352-
}
296+
InvokeClientRpcOnClient(ApplyTransform, NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, pos.Value, rot.Value, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel);
353297
}
354298
}
355299
}

0 commit comments

Comments
 (0)