Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide alternate tick registration method #3022

Draft
wants to merge 3 commits into
base: develop-2.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ public override void OnNetworkDespawn()
if (UseRigidBodyForMotion && HasAuthority)
{
DetachFromFixedJoint();
NetworkRigidbodyConnections.Clear();
NetworkTransformConnections.Clear();
}

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

internal System.Collections.Generic.List<NetworkRigidbodyBase> NetworkRigidbodyConnections = new System.Collections.Generic.List<NetworkRigidbodyBase>();
internal System.Collections.Generic.List<NetworkTransform> NetworkTransformConnections = new System.Collections.Generic.List<NetworkTransform>();
internal NetworkRigidbodyBase ParentBody;

private bool m_FixedJoint2DUsingGravity;
Expand Down Expand Up @@ -808,7 +808,7 @@ public bool AttachToFixedJoint(NetworkRigidbodyBase objectToConnectTo, Vector3 p
}

ParentBody = objectToConnectTo;
ParentBody.NetworkRigidbodyConnections.Add(this);
ParentBody.NetworkTransformConnections.Add(NetworkTransform);
if (teleportObject)
{
NetworkTransform.SetState(teleportDisabled: false);
Expand All @@ -821,7 +821,7 @@ public bool AttachToFixedJoint(NetworkRigidbodyBase objectToConnectTo, Vector3 p
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RemoveFromParentBody()
{
ParentBody.NetworkRigidbodyConnections.Remove(this);
ParentBody.NetworkTransformConnections.Remove(NetworkTransform);
ParentBody = null;
}

Expand Down Expand Up @@ -870,6 +870,80 @@ public void DetachFromFixedJoint()
}
}
}

/// <summary>
/// For custom joint (i.e. <see cref="UnityEngine.FixedJoint"/>, <see cref="ConfigurableJoint"/>, etc) solutions where you
/// want to keep the state updates of joint attached bodies tick synchronized with the root (parent).
/// </summary>
/// <remarks>
/// Requires that both bodies be:
/// - Spawned
/// - Owned by the same client
/// - Have <see cref="UseRigidBodyForMotion"/> set to true
/// - Non-kinematic
/// To avoid recursion, the body being added cannot already have a reference to the body it is trying to be
/// added to for tick synchronization.
/// </remarks>
/// <param name="networkRigidbodyBase">the <see cref="NetworkRigidbodyBase"/> component of the attached body</param>
/// <returns>true if added and false if there was an issue while trying to add</returns>
public bool AddToTickSynchronizedUpdates(NetworkRigidbodyBase networkRigidbodyBase)
{
if (!IsSpawned)
{
Debug.LogError($"[{GetType().Name}] Adding to tick synchronization requires that {name} be spawned first.");
return false;
}

if (!networkRigidbodyBase.IsSpawned)
{
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}] Adding to tick synchronization requires that {networkRigidbodyBase.name} be spawned first.");
return false;
}

if (OwnerClientId != networkRigidbodyBase.OwnerClientId)
{
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}][Not Implemented] Tick synchronizing two bodies owned by different clients is not yet supported!");
return false;
}

if (!UseRigidBodyForMotion)
{
Debug.LogError($"[{GetType().Name}] {name} does not have {nameof(UseRigidBodyForMotion)} set and this action requires it to be set first!");
return false;
}

if (IsKinematic())
{
Debug.LogError($"[{GetType().Name}] Requires {name} to be non-kinematic but it is currently kinematic!");
return false;
}

if (networkRigidbodyBase.IsKinematic())
{
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}] Requires {networkRigidbodyBase.name} to be non-kinematic but it is currently kinematic!");
return false;
}

if (networkRigidbodyBase.NetworkTransformConnections.Contains(NetworkTransform))
{
Debug.LogError($"[{networkRigidbodyBase.GetType().Name}][Circular Reference] {networkRigidbodyBase.name} already has {name} included for tick synchronization! Remove {name} prior to adding {networkRigidbodyBase.name}.");
return false;
}

if (!NetworkTransformConnections.Contains(networkRigidbodyBase.NetworkTransform))
{
NetworkTransformConnections.Add(networkRigidbodyBase.NetworkTransform);
}
return true;
}

public void RemoveFromTickSynchronizedUpdates(NetworkRigidbodyBase networkRigidbodyBase)
{
if (networkRigidbodyBase && networkRigidbodyBase.NetworkTransform)
{
NetworkTransformConnections.Remove(networkRigidbodyBase.NetworkTransform);
}
}
}
}
#endif // COM_UNITY_MODULES_PHYSICS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1631,11 +1631,11 @@ private void TryCommitTransform(ref Transform transformToCommit, bool synchroniz

#if COM_UNITY_MODULES_PHYSICS
// We handle updating attached bodies when the "parent" body has a state update in order to keep their delta state updates tick synchronized.
if (m_UseRigidbodyForMotion && m_NetworkRigidbodyInternal.NetworkRigidbodyConnections.Count > 0)
if (m_UseRigidbodyForMotion && m_NetworkRigidbodyInternal.NetworkTransformConnections.Count > 0)
{
foreach (var childRigidbody in m_NetworkRigidbodyInternal.NetworkRigidbodyConnections)
foreach (var child in m_NetworkRigidbodyInternal.NetworkTransformConnections)
{
childRigidbody.NetworkTransform.OnNetworkTick(true);
child.OnNetworkTick(true);
}
}
#endif
Expand Down
Loading