@@ -499,6 +499,10 @@ public Quaternion GetRotation()
499
499
/// <remarks>
500
500
/// When there is no change in an updated state's position then there are no values to return.
501
501
/// Checking for <see cref="HasPositionChange"/> is one way to detect this.
502
+ /// When used with half precision it returns the half precision delta position state update
503
+ /// which will not be the full position.
504
+ /// To get a NettworkTransform's full position, use <see cref="GetSpaceRelativePosition(bool)"/> and
505
+ /// pass true as the parameter.
502
506
/// </remarks>
503
507
/// <returns><see cref="Vector3"/></returns>
504
508
public Vector3 GetPosition ( )
@@ -1110,7 +1114,16 @@ public Vector3 GetSpaceRelativePosition(bool getCurrentState = false)
1110
1114
}
1111
1115
else
1112
1116
{
1113
- return m_CurrentPosition ;
1117
+ // When half float precision is enabled, get the NetworkDeltaPosition's full position
1118
+ if ( UseHalfFloatPrecision )
1119
+ {
1120
+ return m_HalfPositionState . GetFullPosition ( ) ;
1121
+ }
1122
+ else
1123
+ {
1124
+ // Otherwise, just get the current position
1125
+ return m_CurrentPosition ;
1126
+ }
1114
1127
}
1115
1128
}
1116
1129
@@ -1393,6 +1406,8 @@ protected virtual void OnAuthorityPushTransformState(ref NetworkTransformState n
1393
1406
{
1394
1407
}
1395
1408
1409
+ // Tracks the last tick a state update was sent (see further below)
1410
+ private int m_LastTick ;
1396
1411
/// <summary>
1397
1412
/// Authoritative side only
1398
1413
/// If there are any transform delta states, this method will synchronize the
@@ -1411,11 +1426,27 @@ private void TryCommitTransform(ref Transform transformToCommit, bool synchroniz
1411
1426
if ( ApplyTransformToNetworkStateWithInfo ( ref m_LocalAuthoritativeNetworkState , ref transformToCommit , synchronize ) )
1412
1427
{
1413
1428
m_LocalAuthoritativeNetworkState . LastSerializedSize = m_OldState . LastSerializedSize ;
1414
- OnAuthorityPushTransformState ( ref m_LocalAuthoritativeNetworkState ) ;
1415
1429
1430
+ // Make sure our network tick is incremented
1431
+ if ( m_LastTick == m_LocalAuthoritativeNetworkState . NetworkTick && ! m_LocalAuthoritativeNetworkState . IsTeleportingNextFrame )
1432
+ {
1433
+ // When running in authority and a remote client is the owner, the client can hit a perfect window of time where
1434
+ // it is still on the previous network tick (as a count) but still have had the tick event triggered.
1435
+ // (This is cheaper than calculating the exact tick each time and only can occur on clients)
1436
+ if ( ! IsServer )
1437
+ {
1438
+ m_LocalAuthoritativeNetworkState . NetworkTick = m_LocalAuthoritativeNetworkState . NetworkTick + 1 ;
1439
+ }
1440
+ else
1441
+ {
1442
+ NetworkLog . LogError ( $ "[NT TICK DUPLICATE] Server already sent an update on tick { m_LastTick } and is attempting to send again on the same network tick!") ;
1443
+ }
1444
+ }
1445
+ m_LastTick = m_LocalAuthoritativeNetworkState . NetworkTick ;
1416
1446
// Update the state
1417
1447
UpdateTransformState ( ) ;
1418
1448
1449
+ OnAuthorityPushTransformState ( ref m_LocalAuthoritativeNetworkState ) ;
1419
1450
m_LocalAuthoritativeNetworkState . IsTeleportingNextFrame = false ;
1420
1451
}
1421
1452
}
@@ -2209,6 +2240,7 @@ private void ApplyUpdatedState(NetworkTransformState newState)
2209
2240
m_HalfPositionState . HalfVector3 . Axis = m_LocalAuthoritativeNetworkState . NetworkDeltaPosition . HalfVector3 . Axis ;
2210
2241
// and update our target position
2211
2242
m_TargetPosition = m_HalfPositionState . ToVector3 ( newState . NetworkTick ) ;
2243
+ m_LocalAuthoritativeNetworkState . NetworkDeltaPosition . CurrentBasePosition = m_HalfPositionState . CurrentBasePosition ;
2212
2244
m_LocalAuthoritativeNetworkState . CurrentPosition = m_TargetPosition ;
2213
2245
}
2214
2246
@@ -2455,6 +2487,9 @@ private void NetworkTickSystem_Tick()
2455
2487
// Update any changes to the transform
2456
2488
var transformSource = transform ;
2457
2489
OnUpdateAuthoritativeState ( ref transformSource ) ;
2490
+
2491
+ m_CurrentPosition = GetSpaceRelativePosition ( ) ;
2492
+ m_TargetPosition = GetSpaceRelativePosition ( ) ;
2458
2493
}
2459
2494
else
2460
2495
{
@@ -2466,9 +2501,6 @@ private void NetworkTickSystem_Tick()
2466
2501
}
2467
2502
}
2468
2503
2469
-
2470
-
2471
-
2472
2504
/// <inheritdoc/>
2473
2505
public override void OnNetworkSpawn ( )
2474
2506
{
@@ -2510,24 +2542,25 @@ public override void OnDestroy()
2510
2542
}
2511
2543
2512
2544
/// <inheritdoc/>
2513
- public override void OnGainedOwnership ( )
2545
+ public override void OnLostOwnership ( )
2514
2546
{
2515
- // Only initialize if we gained ownership
2516
- if ( OwnerClientId == NetworkManager . LocalClientId )
2517
- {
2518
- Initialize ( ) ;
2519
- }
2547
+ base . OnLostOwnership ( ) ;
2520
2548
}
2521
2549
2522
2550
/// <inheritdoc/>
2523
- public override void OnLostOwnership ( )
2551
+ public override void OnGainedOwnership ( )
2524
2552
{
2525
- // Only initialize if we are not authority and lost
2526
- // ownership
2527
- if ( OwnerClientId != NetworkManager . LocalClientId )
2553
+ base . OnGainedOwnership ( ) ;
2554
+ }
2555
+
2556
+ protected override void OnOwnershipChanged ( ulong previous , ulong current )
2557
+ {
2558
+ // If we were the previous owner or the newly assigned owner then reinitialize
2559
+ if ( current == NetworkManager . LocalClientId || previous == NetworkManager . LocalClientId )
2528
2560
{
2529
2561
Initialize ( ) ;
2530
2562
}
2563
+ base . OnOwnershipChanged ( previous , current ) ;
2531
2564
}
2532
2565
2533
2566
/// <summary>
@@ -2552,6 +2585,7 @@ protected virtual void OnInitialize(ref NetworkVariable<NetworkTransformState> r
2552
2585
2553
2586
}
2554
2587
2588
+
2555
2589
/// <summary>
2556
2590
/// Initializes NetworkTransform when spawned and ownership changes.
2557
2591
/// </summary>
@@ -2572,7 +2606,8 @@ protected void Initialize()
2572
2606
{
2573
2607
m_HalfPositionState = new NetworkDeltaPosition ( currentPosition , NetworkManager . NetworkTickSystem . ServerTime . Tick , math . bool3 ( SyncPositionX , SyncPositionY , SyncPositionZ ) ) ;
2574
2608
}
2575
-
2609
+ m_CurrentPosition = currentPosition ;
2610
+ m_TargetPosition = currentPosition ;
2576
2611
// Authority only updates once per network tick
2577
2612
NetworkManager . NetworkTickSystem . Tick -= NetworkTickSystem_Tick ;
2578
2613
NetworkManager . NetworkTickSystem . Tick += NetworkTickSystem_Tick ;
@@ -2835,6 +2870,13 @@ public bool IsServerAuthoritative()
2835
2870
/// <param name="messagePayload">serialzied <see cref="NetworkTransformState"/></param>
2836
2871
private void TransformStateUpdate ( ulong senderId , FastBufferReader messagePayload )
2837
2872
{
2873
+ if ( ! OnIsServerAuthoritative ( ) && IsServer && OwnerClientId == NetworkManager . ServerClientId )
2874
+ {
2875
+ // Ownership must have changed, ignore any additional pending messages that might have
2876
+ // come from a previous owner client.
2877
+ return ;
2878
+ }
2879
+
2838
2880
// Forward owner authoritative messages before doing anything else
2839
2881
if ( IsServer && ! OnIsServerAuthoritative ( ) )
2840
2882
{
@@ -2856,6 +2898,7 @@ private void TransformStateUpdate(ulong senderId, FastBufferReader messagePayloa
2856
2898
/// <param name="messagePayload">the owner state message payload</param>
2857
2899
private unsafe void ForwardStateUpdateMessage ( FastBufferReader messagePayload )
2858
2900
{
2901
+ var serverAuthoritative = OnIsServerAuthoritative ( ) ;
2859
2902
var currentPosition = messagePayload . Position ;
2860
2903
var messageSize = messagePayload . Length - currentPosition ;
2861
2904
var writer = new FastBufferWriter ( messageSize , Allocator . Temp ) ;
@@ -2867,7 +2910,7 @@ private unsafe void ForwardStateUpdateMessage(FastBufferReader messagePayload)
2867
2910
for ( int i = 0 ; i < clientCount ; i ++ )
2868
2911
{
2869
2912
var clientId = NetworkManager . ConnectionManager . ConnectedClientsList [ i ] . ClientId ;
2870
- if ( ! OnIsServerAuthoritative ( ) && ( NetworkManager . ServerClientId == clientId || clientId == OwnerClientId ) )
2913
+ if ( NetworkManager . ServerClientId == clientId || ( ! serverAuthoritative && clientId == OwnerClientId ) )
2871
2914
{
2872
2915
continue ;
2873
2916
}
0 commit comments