Skip to content

Commit 4b5fd69

Browse files
update
For customized solutions, I am adding an additional method that allows users to add a NetworkRigidbodyBase to the tick synchronized udpates.
1 parent 99dc876 commit 4b5fd69

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

com.unity.netcode.gameobjects/Runtime/Components/NetworkRigidBodyBase.cs

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ public override void OnNetworkDespawn()
660660
if (UseRigidBodyForMotion && HasAuthority)
661661
{
662662
DetachFromFixedJoint();
663-
NetworkRigidbodyConnections.Clear();
663+
NetworkTransformConnections.Clear();
664664
}
665665

666666
// If we are automatically handling the kinematic state...
@@ -682,7 +682,7 @@ public override void OnNetworkDespawn()
682682
public FixedJoint FixedJoint { get; private set; }
683683
public FixedJoint2D FixedJoint2D { get; private set; }
684684

685-
internal System.Collections.Generic.List<NetworkRigidbodyBase> NetworkRigidbodyConnections = new System.Collections.Generic.List<NetworkRigidbodyBase>();
685+
internal System.Collections.Generic.List<NetworkTransform> NetworkTransformConnections = new System.Collections.Generic.List<NetworkTransform>();
686686
internal NetworkRigidbodyBase ParentBody;
687687

688688
private bool m_FixedJoint2DUsingGravity;
@@ -808,7 +808,7 @@ public bool AttachToFixedJoint(NetworkRigidbodyBase objectToConnectTo, Vector3 p
808808
}
809809

810810
ParentBody = objectToConnectTo;
811-
ParentBody.NetworkRigidbodyConnections.Add(this);
811+
ParentBody.NetworkTransformConnections.Add(NetworkTransform);
812812
if (teleportObject)
813813
{
814814
NetworkTransform.SetState(teleportDisabled: false);
@@ -821,7 +821,7 @@ public bool AttachToFixedJoint(NetworkRigidbodyBase objectToConnectTo, Vector3 p
821821
[MethodImpl(MethodImplOptions.AggressiveInlining)]
822822
private void RemoveFromParentBody()
823823
{
824-
ParentBody.NetworkRigidbodyConnections.Remove(this);
824+
ParentBody.NetworkTransformConnections.Remove(NetworkTransform);
825825
ParentBody = null;
826826
}
827827

@@ -870,6 +870,80 @@ public void DetachFromFixedJoint()
870870
}
871871
}
872872
}
873+
874+
/// <summary>
875+
/// For custom joint (i.e. <see cref="UnityEngine.FixedJoint"/>, <see cref="ConfigurableJoint"/>, etc) solutions where you
876+
/// want to keep the state updates of joint attached bodies tick synchronized with the root (parent).
877+
/// </summary>
878+
/// <remarks>
879+
/// Requires that both bodies be:
880+
/// - Spawned
881+
/// - Owned by the same client
882+
/// - Have <see cref="UseRigidBodyForMotion"/> set to true
883+
/// - Non-kinematic
884+
/// To avoid recursion, the body being added cannot already have a reference to the body it is trying to be
885+
/// added to for tick synchronization.
886+
/// </remarks>
887+
/// <param name="networkRigidbodyBase">the <see cref="NetworkRigidbodyBase"/> component of the attached body</param>
888+
/// <returns>true if added and false if there was an issue while trying to add</returns>
889+
public bool AddToTickSynchronizedUpdates(NetworkRigidbodyBase networkRigidbodyBase)
890+
{
891+
if (!IsSpawned)
892+
{
893+
Debug.LogError($"[{GetType().Name}] Adding to tick synchronization requires that {name} be spawned first.");
894+
return false;
895+
}
896+
897+
if (!networkRigidbodyBase.IsSpawned)
898+
{
899+
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}] Adding to tick synchronization requires that {networkRigidbodyBase.name} be spawned first.");
900+
return false;
901+
}
902+
903+
if (OwnerClientId != networkRigidbodyBase.OwnerClientId)
904+
{
905+
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}][Not Implemented] Tick synchronizing two bodies owned by different clients is not yet supported!");
906+
return false;
907+
}
908+
909+
if (!UseRigidBodyForMotion)
910+
{
911+
Debug.LogError($"[{GetType().Name}] {name} does not have {nameof(UseRigidBodyForMotion)} set and this action requires it to be set first!");
912+
return false;
913+
}
914+
915+
if (IsKinematic())
916+
{
917+
Debug.LogError($"[{GetType().Name}] Requires {name} to be non-kinematic but it is currently kinematic!");
918+
return false;
919+
}
920+
921+
if (networkRigidbodyBase.IsKinematic())
922+
{
923+
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}] Requires {networkRigidbodyBase.name} to be non-kinematic but it is currently kinematic!");
924+
return false;
925+
}
926+
927+
if (networkRigidbodyBase.NetworkTransformConnections.Contains(NetworkTransform))
928+
{
929+
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}][Circular Reference] {networkRigidbodyBase.name} already has {name} included for tick synchronization! Remove {name} prior to adding {networkRigidbodyBase.name}.");
930+
return false;
931+
}
932+
933+
if (!NetworkTransformConnections.Contains(networkRigidbodyBase.NetworkTransform))
934+
{
935+
NetworkTransformConnections.Add(networkRigidbodyBase.NetworkTransform);
936+
}
937+
return true;
938+
}
939+
940+
public void RemoveFromTickSynchronizedUpdates(NetworkRigidbodyBase networkRigidbodyBase)
941+
{
942+
if (networkRigidbodyBase && networkRigidbodyBase.NetworkTransform)
943+
{
944+
NetworkTransformConnections.Remove(networkRigidbodyBase.NetworkTransform);
945+
}
946+
}
873947
}
874948
}
875949
#endif // COM_UNITY_MODULES_PHYSICS

0 commit comments

Comments
 (0)