Remove 2D physics support.

This commit is contained in:
2024-10-22 05:05:54 +02:00
parent 016788c21e
commit 887ec37b67
3 changed files with 69 additions and 347 deletions

View File

@@ -1,15 +1,15 @@
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D #if COM_UNITY_MODULES_PHYSICS
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
namespace Unity.Netcode.Components namespace Unity.Netcode.Components
{ {
/// <summary> /// <summary>
/// NetworkRigidbodyBase is a unified <see cref="Rigidbody"/> and <see cref="Rigidbody2D"/> integration that helps to synchronize physics motion, collision, and interpolation /// NetworkRigidbodyBase is a <see cref="Rigidbody"/> integration that helps to synchronize physics motion, collision, and interpolation
/// when used with a <see cref="NetworkTransform"/>. /// when used with a <see cref="NetworkTransform"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// For a customizable netcode Rigidbody, create your own component from this class and use <see cref="Initialize(RigidbodyTypes, NetworkTransform, Rigidbody2D, Rigidbody)"/> /// For a customizable netcode Rigidbody, create your own component from this class and use <see cref="Initialize(NetworkTransform, Rigidbody)"/>
/// during instantiation (i.e. invoked from within the Awake method). You can re-initialize after having initialized but only when the <see cref="NetworkObject"/> is not spawned. /// during instantiation (i.e. invoked from within the Awake method). You can re-initialize after having initialized but only when the <see cref="NetworkObject"/> is not spawned.
/// </remarks> /// </remarks>
public abstract class NetworkRigidbodyBase : NetworkBehaviour public abstract class NetworkRigidbodyBase : NetworkBehaviour
@@ -21,7 +21,7 @@ namespace Unity.Netcode.Components
#endif #endif
/// <summary> /// <summary>
/// When enabled, the associated <see cref="NetworkTransform"/> will use the Rigidbody/Rigidbody2D to apply and synchronize changes in position, rotation, and /// When enabled, the associated <see cref="NetworkTransform"/> will use the Rigidbody to apply and synchronize changes in position, rotation, and
/// allows for the use of Rigidbody interpolation/extrapolation. /// allows for the use of Rigidbody interpolation/extrapolation.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@@ -44,13 +44,10 @@ namespace Unity.Netcode.Components
/// </summary> /// </summary>
public bool AutoSetKinematicOnDespawn = true; public bool AutoSetKinematicOnDespawn = true;
// Determines if this is a Rigidbody or Rigidbody2D implementation
private bool m_IsRigidbody2D => RigidbodyType == RigidbodyTypes.Rigidbody2D;
// Used to cache the authority state of this Rigidbody during the last frame // Used to cache the authority state of this Rigidbody during the last frame
private bool m_IsAuthority; private bool m_IsAuthority;
protected internal Rigidbody m_InternalRigidbody { get; private set; } protected internal Rigidbody m_InternalRigidbody { get; private set; }
protected internal Rigidbody2D m_InternalRigidbody2D { get; private set; }
internal NetworkTransform NetworkTransform; internal NetworkTransform NetworkTransform;
private float m_TickFrequency; private float m_TickFrequency;
@@ -64,18 +61,6 @@ namespace Unity.Netcode.Components
} }
private InterpolationTypes m_OriginalInterpolation; private InterpolationTypes m_OriginalInterpolation;
/// <summary>
/// Used to define the type of Rigidbody implemented.
/// <see cref=""/>
/// </summary>
public enum RigidbodyTypes
{
Rigidbody,
Rigidbody2D,
}
public RigidbodyTypes RigidbodyType { get; private set; }
/// <summary> /// <summary>
/// Initializes the networked Rigidbody based on the <see cref="RigidbodyTypes"/> /// Initializes the networked Rigidbody based on the <see cref="RigidbodyTypes"/>
/// passed in as a parameter. /// passed in as a parameter.
@@ -83,10 +68,8 @@ namespace Unity.Netcode.Components
/// <remarks> /// <remarks>
/// Cannot be initialized while the associated <see cref="NetworkObject"/> is spawned. /// Cannot be initialized while the associated <see cref="NetworkObject"/> is spawned.
/// </remarks> /// </remarks>
/// <param name="rigidbodyType">type of rigid body being initialized</param>
/// <param name="rigidbody2D">(optional) The <see cref="Rigidbody2D"/> to be used</param>
/// <param name="rigidbody">(optional) The <see cref="Rigidbody"/> to be used</param> /// <param name="rigidbody">(optional) The <see cref="Rigidbody"/> to be used</param>
protected void Initialize(RigidbodyTypes rigidbodyType, NetworkTransform networkTransform = null, Rigidbody2D rigidbody2D = null, Rigidbody rigidbody = null) protected void Initialize(NetworkTransform networkTransform = null, Rigidbody rigidbody = null)
{ {
// Don't initialize if already spawned // Don't initialize if already spawned
if (IsSpawned) if (IsSpawned)
@@ -94,17 +77,10 @@ namespace Unity.Netcode.Components
Debug.LogError($"[{name}] Attempting to initialize while spawned is not allowed."); Debug.LogError($"[{name}] Attempting to initialize while spawned is not allowed.");
return; return;
} }
RigidbodyType = rigidbodyType;
m_InternalRigidbody2D = rigidbody2D;
m_InternalRigidbody = rigidbody; m_InternalRigidbody = rigidbody;
NetworkTransform = networkTransform; NetworkTransform = networkTransform;
if (m_IsRigidbody2D && m_InternalRigidbody2D == null) if (m_InternalRigidbody == null)
{
m_InternalRigidbody2D = GetComponent<Rigidbody2D>();
}
else if (m_InternalRigidbody == null)
{ {
m_InternalRigidbody = GetComponent<Rigidbody>(); m_InternalRigidbody = GetComponent<Rigidbody>();
} }
@@ -145,11 +121,7 @@ namespace Unity.Netcode.Components
// Finally, we adjust the threshold based on the body's current velocity // Finally, we adjust the threshold based on the body's current velocity
perTickVelocity.x = Mathf.Clamp(Mathf.Abs(perTickVelocity.x), minThreshold, thresholdMax); perTickVelocity.x = Mathf.Clamp(Mathf.Abs(perTickVelocity.x), minThreshold, thresholdMax);
perTickVelocity.y = Mathf.Clamp(Mathf.Abs(perTickVelocity.y), minThreshold, thresholdMax); perTickVelocity.y = Mathf.Clamp(Mathf.Abs(perTickVelocity.y), minThreshold, thresholdMax);
// 2D Rigidbody only moves on x & y axis
if (!m_IsRigidbody2D)
{
perTickVelocity.z = Mathf.Clamp(Mathf.Abs(perTickVelocity.z), minThreshold, thresholdMax); perTickVelocity.z = Mathf.Clamp(Mathf.Abs(perTickVelocity.z), minThreshold, thresholdMax);
}
return perTickVelocity; return perTickVelocity;
} }
@@ -164,12 +136,8 @@ namespace Unity.Netcode.Components
var rotationPerTick = (GetAngularVelocity() * Mathf.Rad2Deg) * m_TickFrequency; var rotationPerTick = (GetAngularVelocity() * Mathf.Rad2Deg) * m_TickFrequency;
var minThreshold = NetworkTransform.RotAngleThreshold * m_TickFrequency; var minThreshold = NetworkTransform.RotAngleThreshold * m_TickFrequency;
// 2D Rigidbody only rotates around Z axis
if (!m_IsRigidbody2D)
{
rotationPerTick.x = Mathf.Clamp(Mathf.Abs(rotationPerTick.x), minThreshold, thresholdMax); rotationPerTick.x = Mathf.Clamp(Mathf.Abs(rotationPerTick.x), minThreshold, thresholdMax);
rotationPerTick.y = Mathf.Clamp(Mathf.Abs(rotationPerTick.y), minThreshold, thresholdMax); rotationPerTick.y = Mathf.Clamp(Mathf.Abs(rotationPerTick.y), minThreshold, thresholdMax);
}
rotationPerTick.z = Mathf.Clamp(Mathf.Abs(rotationPerTick.z), minThreshold, thresholdMax); rotationPerTick.z = Mathf.Clamp(Mathf.Abs(rotationPerTick.z), minThreshold, thresholdMax);
return rotationPerTick; return rotationPerTick;
@@ -178,89 +146,40 @@ namespace Unity.Netcode.Components
/// <summary> /// <summary>
/// Sets the linear velocity of the Rigidbody. /// Sets the linear velocity of the Rigidbody.
/// </summary> /// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, only the x and y components of the <see cref="Vector3"/> are applied.
/// </remarks>
public void SetLinearVelocity(Vector3 linearVelocity) public void SetLinearVelocity(Vector3 linearVelocity)
{
if (m_IsRigidbody2D)
{
#if COM_UNITY_MODULES_PHYSICS2D_LINEAR
m_InternalRigidbody2D.linearVelocity = linearVelocity;
#else
m_InternalRigidbody2D.velocity = linearVelocity;
#endif
}
else
{ {
m_InternalRigidbody.linearVelocity = linearVelocity; m_InternalRigidbody.linearVelocity = linearVelocity;
} }
}
/// <summary> /// <summary>
/// Gets the linear velocity of the Rigidbody. /// Gets the linear velocity of the Rigidbody.
/// </summary> /// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, the <see cref="Vector3"/> velocity returned is only applied to the x and y components.
/// </remarks>
/// <returns><see cref="Vector3"/> as the linear velocity</returns> /// <returns><see cref="Vector3"/> as the linear velocity</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3 GetLinearVelocity() public Vector3 GetLinearVelocity()
{
if (m_IsRigidbody2D)
{
#if COM_UNITY_MODULES_PHYSICS2D_LINEAR
return m_InternalRigidbody2D.linearVelocity;
#else
return m_InternalRigidbody2D.velocity;
#endif
}
else
{ {
return m_InternalRigidbody.linearVelocity; return m_InternalRigidbody.linearVelocity;
} }
}
/// <summary> /// <summary>
/// Sets the angular velocity for the Rigidbody. /// Sets the angular velocity for the Rigidbody.
/// </summary> /// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, the z component of <param name="angularVelocity"/> is only used to set the angular velocity.
/// A quick way to pass in a 2D angular velocity component is: <see cref="Vector3.forward"/> * angularVelocity (where angularVelocity is a float)
/// </remarks>
/// <param name="angularVelocity">the angular velocity to apply to the body</param> /// <param name="angularVelocity">the angular velocity to apply to the body</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetAngularVelocity(Vector3 angularVelocity) public void SetAngularVelocity(Vector3 angularVelocity)
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.angularVelocity = angularVelocity.z;
}
else
{ {
m_InternalRigidbody.angularVelocity = angularVelocity; m_InternalRigidbody.angularVelocity = angularVelocity;
} }
}
/// <summary> /// <summary>
/// Gets the angular velocity for the Rigidbody. /// Gets the angular velocity for the Rigidbody.
/// </summary> /// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, the z component of the <see cref="Vector3"/> returned is the angular velocity of the object.
/// </remarks>
/// <returns>angular velocity as a <see cref="Vector3"/></returns> /// <returns>angular velocity as a <see cref="Vector3"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3 GetAngularVelocity() public Vector3 GetAngularVelocity()
{
if (m_IsRigidbody2D)
{
return Vector3.forward * m_InternalRigidbody2D.angularVelocity;
}
else
{ {
return m_InternalRigidbody.angularVelocity; return m_InternalRigidbody.angularVelocity;
} }
}
/// <summary> /// <summary>
/// Gets the position of the Rigidbody /// Gets the position of the Rigidbody
@@ -268,16 +187,9 @@ namespace Unity.Netcode.Components
/// <returns><see cref="Vector3"/></returns> /// <returns><see cref="Vector3"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3 GetPosition() public Vector3 GetPosition()
{
if (m_IsRigidbody2D)
{
return m_InternalRigidbody2D.position;
}
else
{ {
return m_InternalRigidbody.position; return m_InternalRigidbody.position;
} }
}
/// <summary> /// <summary>
/// Gets the rotation of the Rigidbody /// Gets the rotation of the Rigidbody
@@ -285,20 +197,9 @@ namespace Unity.Netcode.Components
/// <returns><see cref="Quaternion"/></returns> /// <returns><see cref="Quaternion"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Quaternion GetRotation() public Quaternion GetRotation()
{
if (m_IsRigidbody2D)
{
var quaternion = Quaternion.identity;
var angles = quaternion.eulerAngles;
angles.z = m_InternalRigidbody2D.rotation;
quaternion.eulerAngles = angles;
return quaternion;
}
else
{ {
return m_InternalRigidbody.rotation; return m_InternalRigidbody.rotation;
} }
}
/// <summary> /// <summary>
/// Moves the rigid body /// Moves the rigid body
@@ -306,16 +207,9 @@ namespace Unity.Netcode.Components
/// <param name="position">The <see cref="Vector3"/> position to move towards</param> /// <param name="position">The <see cref="Vector3"/> position to move towards</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MovePosition(Vector3 position) public void MovePosition(Vector3 position)
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.MovePosition(position);
}
else
{ {
m_InternalRigidbody.MovePosition(position); m_InternalRigidbody.MovePosition(position);
} }
}
/// <summary> /// <summary>
/// Directly applies a position (like teleporting) /// Directly applies a position (like teleporting)
@@ -323,34 +217,19 @@ namespace Unity.Netcode.Components
/// <param name="position"><see cref="Vector3"/> position to apply to the Rigidbody</param> /// <param name="position"><see cref="Vector3"/> position to apply to the Rigidbody</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetPosition(Vector3 position) public void SetPosition(Vector3 position)
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.position = position;
}
else
{ {
m_InternalRigidbody.position = position; m_InternalRigidbody.position = position;
} }
}
/// <summary> /// <summary>
/// Applies the rotation and position of the <see cref="GameObject"/>'s <see cref="Transform"/> /// Applies the rotation and position of the <see cref="GameObject"/>'s <see cref="Transform"/>
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ApplyCurrentTransform() public void ApplyCurrentTransform()
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.position = transform.position;
m_InternalRigidbody2D.rotation = transform.eulerAngles.z;
}
else
{ {
m_InternalRigidbody.position = transform.position; m_InternalRigidbody.position = transform.position;
m_InternalRigidbody.rotation = transform.rotation; m_InternalRigidbody.rotation = transform.rotation;
} }
}
// Used for Rigidbody only (see info on normalized below) // Used for Rigidbody only (see info on normalized below)
private Vector4 m_QuaternionCheck = Vector4.zero; private Vector4 m_QuaternionCheck = Vector4.zero;
@@ -361,16 +240,6 @@ namespace Unity.Netcode.Components
/// <param name="rotation">The rotation expressed as a <see cref="Quaternion"/></param> /// <param name="rotation">The rotation expressed as a <see cref="Quaternion"/></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MoveRotation(Quaternion rotation) public void MoveRotation(Quaternion rotation)
{
if (m_IsRigidbody2D)
{
var quaternion = Quaternion.identity;
var angles = quaternion.eulerAngles;
angles.z = m_InternalRigidbody2D.rotation;
quaternion.eulerAngles = angles;
m_InternalRigidbody2D.MoveRotation(quaternion);
}
else
{ {
// Evidently we need to check to make sure the quaternion is a perfect // Evidently we need to check to make sure the quaternion is a perfect
// magnitude of 1.0f when applying the rotation to a rigid body. // magnitude of 1.0f when applying the rotation to a rigid body.
@@ -385,7 +254,6 @@ namespace Unity.Netcode.Components
} }
m_InternalRigidbody.MoveRotation(rotation); m_InternalRigidbody.MoveRotation(rotation);
} }
}
/// <summary> /// <summary>
/// Applies a rotation to the Rigidbody /// Applies a rotation to the Rigidbody
@@ -393,45 +261,15 @@ namespace Unity.Netcode.Components
/// <param name="rotation">The rotation to apply expressed as a <see cref="Quaternion"/></param> /// <param name="rotation">The rotation to apply expressed as a <see cref="Quaternion"/></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetRotation(Quaternion rotation) public void SetRotation(Quaternion rotation)
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.rotation = rotation.eulerAngles.z;
}
else
{ {
m_InternalRigidbody.rotation = rotation; m_InternalRigidbody.rotation = rotation;
} }
}
/// <summary> /// <summary>
/// Sets the original interpolation of the Rigidbody while taking the Rigidbody type into consideration /// Sets the original interpolation of the Rigidbody while taking the Rigidbody type into consideration
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetOriginalInterpolation() private void SetOriginalInterpolation()
{
if (m_IsRigidbody2D)
{
switch (m_InternalRigidbody2D.interpolation)
{
case RigidbodyInterpolation2D.None:
{
m_OriginalInterpolation = InterpolationTypes.None;
break;
}
case RigidbodyInterpolation2D.Interpolate:
{
m_OriginalInterpolation = InterpolationTypes.Interpolate;
break;
}
case RigidbodyInterpolation2D.Extrapolate:
{
m_OriginalInterpolation = InterpolationTypes.Extrapolate;
break;
}
}
}
else
{ {
switch (m_InternalRigidbody.interpolation) switch (m_InternalRigidbody.interpolation)
{ {
@@ -452,58 +290,33 @@ namespace Unity.Netcode.Components
} }
} }
} }
}
/// <summary> /// <summary>
/// Wakes the Rigidbody if it is sleeping /// Wakes the Rigidbody if it is sleeping
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WakeIfSleeping() public void WakeIfSleeping()
{
if (m_IsRigidbody2D)
{
if (m_InternalRigidbody2D.IsSleeping())
{
m_InternalRigidbody2D.WakeUp();
}
}
else
{ {
if (m_InternalRigidbody.IsSleeping()) if (m_InternalRigidbody.IsSleeping())
{ {
m_InternalRigidbody.WakeUp(); m_InternalRigidbody.WakeUp();
} }
} }
}
/// <summary> /// <summary>
/// Puts the Rigidbody to sleep /// Puts the Rigidbody to sleep
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SleepRigidbody() public void SleepRigidbody()
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.Sleep();
}
else
{ {
m_InternalRigidbody.Sleep(); m_InternalRigidbody.Sleep();
} }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsKinematic() public bool IsKinematic()
{
if (m_IsRigidbody2D)
{
return m_InternalRigidbody2D.bodyType == RigidbodyType2D.Kinematic;
}
else
{ {
return m_InternalRigidbody.isKinematic; return m_InternalRigidbody.isKinematic;
} }
}
/// <summary> /// <summary>
/// Sets the kinematic state of the Rigidbody and handles updating the Rigidbody's /// Sets the kinematic state of the Rigidbody and handles updating the Rigidbody's
@@ -523,15 +336,8 @@ namespace Unity.Netcode.Components
/// <param name="isKinematic"></param> /// <param name="isKinematic"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetIsKinematic(bool isKinematic) public void SetIsKinematic(bool isKinematic)
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.bodyType = isKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
}
else
{ {
m_InternalRigidbody.isKinematic = isKinematic; m_InternalRigidbody.isKinematic = isKinematic;
}
// If we are not spawned, then exit early // If we are not spawned, then exit early
if (!IsSpawned) if (!IsSpawned)
@@ -573,39 +379,18 @@ namespace Unity.Netcode.Components
switch (interpolationType) switch (interpolationType)
{ {
case InterpolationTypes.None: case InterpolationTypes.None:
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.interpolation = RigidbodyInterpolation2D.None;
}
else
{ {
m_InternalRigidbody.interpolation = RigidbodyInterpolation.None; m_InternalRigidbody.interpolation = RigidbodyInterpolation.None;
}
break; break;
} }
case InterpolationTypes.Interpolate: case InterpolationTypes.Interpolate:
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
}
else
{ {
m_InternalRigidbody.interpolation = RigidbodyInterpolation.Interpolate; m_InternalRigidbody.interpolation = RigidbodyInterpolation.Interpolate;
}
break; break;
} }
case InterpolationTypes.Extrapolate: case InterpolationTypes.Extrapolate:
{
if (m_IsRigidbody2D)
{
m_InternalRigidbody2D.interpolation = RigidbodyInterpolation2D.Extrapolate;
}
else
{ {
m_InternalRigidbody.interpolation = RigidbodyInterpolation.Extrapolate; m_InternalRigidbody.interpolation = RigidbodyInterpolation.Extrapolate;
}
break; break;
} }
} }
@@ -688,12 +473,10 @@ namespace Unity.Netcode.Components
// Alternately, users can affix the fixed joint to a child GameObject (without a rigid body) of the parent NetworkObject // Alternately, users can affix the fixed joint to a child GameObject (without a rigid body) of the parent NetworkObject
// and then add a NetworkTransform to that in order to get the parented child NetworkObject to move around in "local space" // and then add a NetworkTransform to that in order to get the parented child NetworkObject to move around in "local space"
public FixedJoint FixedJoint { get; private set; } 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<NetworkRigidbodyBase> NetworkRigidbodyConnections = new System.Collections.Generic.List<NetworkRigidbodyBase>();
internal NetworkRigidbodyBase ParentBody; internal NetworkRigidbodyBase ParentBody;
private bool m_FixedJoint2DUsingGravity;
private bool m_OriginalGravitySetting; private bool m_OriginalGravitySetting;
private float m_OriginalGravityScale; private float m_OriginalGravityScale;
@@ -706,44 +489,6 @@ namespace Unity.Netcode.Components
} }
/// <summary>
/// When using a custom <see cref="NetworkRigidbodyBase"/>, this virtual method is invoked when the
/// <see cref="FixedJoint2D"/> is created in the event any additional adjustments are needed.
/// </summary>
protected virtual void OnFixedJoint2DCreated()
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ApplyFixedJoint2D(NetworkRigidbodyBase bodyToConnect, Vector3 position, float connectedMassScale = 0.0f, float massScale = 1.0f, bool useGravity = false, bool zeroVelocity = true)
{
transform.position = position;
m_InternalRigidbody2D.position = position;
m_OriginalGravitySetting = bodyToConnect.m_InternalRigidbody.useGravity;
m_FixedJoint2DUsingGravity = useGravity;
if (!useGravity)
{
m_OriginalGravityScale = m_InternalRigidbody2D.gravityScale;
m_InternalRigidbody2D.gravityScale = 0.0f;
}
if (zeroVelocity)
{
#if COM_UNITY_MODULES_PHYSICS2D_LINEAR
m_InternalRigidbody2D.linearVelocity = Vector2.zero;
#else
m_InternalRigidbody2D.velocity = Vector2.zero;
#endif
m_InternalRigidbody2D.angularVelocity = 0.0f;
}
FixedJoint2D = gameObject.AddComponent<FixedJoint2D>();
FixedJoint2D.connectedBody = bodyToConnect.m_InternalRigidbody2D;
OnFixedJoint2DCreated();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ApplyFixedJoint(NetworkRigidbodyBase bodyToConnectTo, Vector3 position, float connectedMassScale = 0.0f, float massScale = 1.0f, bool useGravity = false, bool zeroVelocity = true) private void ApplyFixedJoint(NetworkRigidbodyBase bodyToConnectTo, Vector3 position, float connectedMassScale = 0.0f, float massScale = 1.0f, bool useGravity = false, bool zeroVelocity = true)
{ {
@@ -769,7 +514,6 @@ namespace Unity.Netcode.Components
/// When invoked and not already attached to a fixed joint, this will connect two rigid bodies with <see cref="UseRigidBodyForMotion"/> enabled. /// When invoked and not already attached to a fixed joint, this will connect two rigid bodies with <see cref="UseRigidBodyForMotion"/> enabled.
/// Invoke this method on the rigid body you wish to attach to another (i.e. weapon to player, sticky bomb to player/object, etc). /// Invoke this method on the rigid body you wish to attach to another (i.e. weapon to player, sticky bomb to player/object, etc).
/// <seealso cref="FixedJoint"/> /// <seealso cref="FixedJoint"/>
/// <seealso cref="FixedJoint2D"/>
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Parenting relative: /// Parenting relative:
@@ -777,7 +521,7 @@ namespace Unity.Netcode.Components
/// - The <param name="objectToConnectTo"/> can be viewed as the parent. /// - The <param name="objectToConnectTo"/> can be viewed as the parent.
/// <br/> /// <br/>
/// This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when <see cref="UseRigidBodyForMotion"/> is enabled. /// This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when <see cref="UseRigidBodyForMotion"/> is enabled.
/// For more details on using <see cref="UnityEngine.FixedJoint"/> and <see cref="UnityEngine.FixedJoint2D"/>. /// For more details on using <see cref="UnityEngine.FixedJoint"/>.
/// <br/> /// <br/>
/// This provides a simple joint solution between two rigid bodies and serves as an example. You can add different joint types by creating a customized/derived /// This provides a simple joint solution between two rigid bodies and serves as an example. You can add different joint types by creating a customized/derived
/// version of <see cref="NetworkRigidbodyBase"/>. /// version of <see cref="NetworkRigidbodyBase"/>.
@@ -805,15 +549,8 @@ namespace Unity.Netcode.Components
} }
if (objectToConnectTo != null) if (objectToConnectTo != null)
{
if (m_IsRigidbody2D)
{
ApplyFixedJoint2D(objectToConnectTo, positionOfConnection, connectedMassScale, massScale, useGravity, zeroVelocity);
}
else
{ {
ApplyFixedJoint(objectToConnectTo, positionOfConnection, connectedMassScale, massScale, useGravity, zeroVelocity); ApplyFixedJoint(objectToConnectTo, positionOfConnection, connectedMassScale, massScale, useGravity, zeroVelocity);
}
ParentBody = objectToConnectTo; ParentBody = objectToConnectTo;
ParentBody.NetworkRigidbodyConnections.Add(this); ParentBody.NetworkRigidbodyConnections.Add(this);
@@ -835,7 +572,7 @@ namespace Unity.Netcode.Components
/// <summary> /// <summary>
/// Authority Only: /// Authority Only:
/// When invoked and already connected to an object via <see cref="FixedJoint"/> or <see cref="FixedJoint2D"/> (depending upon the type of rigid body), /// When invoked and already connected to an object via <see cref="FixedJoint"/>,
/// this will detach from the fixed joint and destroy the fixed joint component. /// this will detach from the fixed joint and destroy the fixed joint component.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@@ -848,23 +585,6 @@ namespace Unity.Netcode.Components
Debug.LogError($"[{name}] Only authority can invoke {nameof(DetachFromFixedJoint)}!"); Debug.LogError($"[{name}] Only authority can invoke {nameof(DetachFromFixedJoint)}!");
} }
if (UseRigidBodyForMotion) if (UseRigidBodyForMotion)
{
if (m_IsRigidbody2D)
{
if (FixedJoint2D != null)
{
if (!m_FixedJoint2DUsingGravity)
{
FixedJoint2D.connectedBody.gravityScale = m_OriginalGravityScale;
}
FixedJoint2D.connectedBody = null;
Destroy(FixedJoint2D);
FixedJoint2D = null;
ResetInterpolation();
RemoveFromParentBody();
}
}
else
{ {
if (FixedJoint != null) if (FixedJoint != null)
{ {
@@ -879,6 +599,5 @@ namespace Unity.Netcode.Components
} }
} }
} }
}
#endif // COM_UNITY_MODULES_PHYSICS #endif // COM_UNITY_MODULES_PHYSICS

View File

@@ -17,7 +17,7 @@ namespace Unity.Netcode.Components
protected virtual void Awake() protected virtual void Awake()
{ {
Initialize(RigidbodyTypes.Rigidbody); Initialize();
} }
} }
} }

View File

@@ -26,5 +26,8 @@
"description": "A lightweight sample to get started", "description": "A lightweight sample to get started",
"path": "Samples~/Bootstrap" "path": "Samples~/Bootstrap"
} }
] ],
"author": {
"name": "Unity"
}
} }