com.unity.netcode.gameobjects@2.0.0-pre.1
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com). ## [2.0.0-pre.1] - 2024-06-17 ### Added - Added event `NetworkManager.OnSessionOwnerPromoted` that is invoked when a new session owner promotion occurs. (#2948) - Added `NetworkRigidBodyBase.GetLinearVelocity` and `NetworkRigidBodyBase.SetLinearVelocity` convenience/helper methods. (#2948) - Added `NetworkRigidBodyBase.GetAngularVelocity` and `NetworkRigidBodyBase.SetAngularVelocity` convenience/helper methods. (#2948) ### Fixed - Fixed issue when `NetworkTransform` half float precision is enabled and ownership changes the current base position was not being synchronized. (#2948) - Fixed issue where `OnClientConnected` not being invoked on the session owner when connecting to a new distributed authority session. (#2948) - Fixed issue where Rigidbody micro-motion (i.e. relatively small velocities) would result in non-authority instances slightly stuttering as the body would come to a rest (i.e. no motion). Now, the threshold value can increase at higher velocities and can decrease slightly below the provided threshold to account for this. (#2948) ### Changed - Changed the client's owned objects is now returned (`NetworkClient` and `NetworkSpawnManager`) as an array as opposed to a list for performance purposes. (#2948) - Changed `NetworkTransfrom.TryCommitTransformToServer` to be internal as it will be removed by the final 2.0.0 release. (#2948) - Changed `NetworkTransformEditor.OnEnable` to a virtual method to be able to customize a `NetworkTransform` derived class by creating a derived editor control from `NetworkTransformEditor`. (#2948)
This commit is contained in:
@@ -45,6 +45,9 @@ namespace Unity.Netcode.Components
|
||||
private Rigidbody m_Rigidbody;
|
||||
private Rigidbody2D m_Rigidbody2D;
|
||||
internal NetworkTransform NetworkTransform;
|
||||
private float m_TickFrequency;
|
||||
private float m_TickRate;
|
||||
|
||||
private enum InterpolationTypes
|
||||
{
|
||||
None,
|
||||
@@ -120,6 +123,129 @@ namespace Unity.Netcode.Components
|
||||
}
|
||||
}
|
||||
|
||||
internal Vector3 GetAdjustedPositionThreshold()
|
||||
{
|
||||
// Since the threshold is a measurement of unity world space units per tick, we will allow for the maximum threshold
|
||||
// to be no greater than the threshold measured in unity world space units per second
|
||||
var thresholdMax = NetworkTransform.PositionThreshold * m_TickRate;
|
||||
// Get the velocity in unity world space units per tick
|
||||
var perTickVelocity = GetLinearVelocity() * m_TickFrequency;
|
||||
// Since a rigid body can have "micro-motion" when allowed to come to rest (based on friction etc), we will allow for
|
||||
// no less than 1/10th the threshold value.
|
||||
var minThreshold = NetworkTransform.PositionThreshold * 0.1f;
|
||||
|
||||
// Finally, we adjust the threshold based on the body's current velocity
|
||||
perTickVelocity.x = Mathf.Clamp(Mathf.Abs(perTickVelocity.x), 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);
|
||||
}
|
||||
|
||||
return perTickVelocity;
|
||||
}
|
||||
|
||||
internal Vector3 GetAdjustedRotationThreshold()
|
||||
{
|
||||
// Since the rotation threshold is a measurement pf degrees per tick, we get the maximum threshold
|
||||
// by calculating the threshold in degrees per second.
|
||||
var thresholdMax = NetworkTransform.RotAngleThreshold * m_TickRate;
|
||||
// Angular velocity is expressed in radians per second where as the rotation being checked is in degrees.
|
||||
// Convert the angular velocity to degrees per second and then convert that to degrees per tick.
|
||||
var rotationPerTick = (GetAngularVelocity() * Mathf.Rad2Deg) * 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.y = Mathf.Clamp(Mathf.Abs(rotationPerTick.y), minThreshold, thresholdMax);
|
||||
}
|
||||
rotationPerTick.z = Mathf.Clamp(Mathf.Abs(rotationPerTick.z), minThreshold, thresholdMax);
|
||||
|
||||
return rotationPerTick;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the linear velocity of the Rigidbody.
|
||||
/// </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)
|
||||
{
|
||||
if (m_IsRigidbody2D)
|
||||
{
|
||||
m_Rigidbody2D.velocity = linearVelocity;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Rigidbody.linearVelocity = linearVelocity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the linear velocity of the Rigidbody.
|
||||
/// </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>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vector3 GetLinearVelocity()
|
||||
{
|
||||
if (m_IsRigidbody2D)
|
||||
{
|
||||
return m_Rigidbody2D.velocity;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_Rigidbody.linearVelocity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the angular velocity for the Rigidbody.
|
||||
/// </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>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetAngularVelocity(Vector3 angularVelocity)
|
||||
{
|
||||
if (m_IsRigidbody2D)
|
||||
{
|
||||
m_Rigidbody2D.angularVelocity = angularVelocity.z;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Rigidbody.angularVelocity = angularVelocity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the angular velocity for the Rigidbody.
|
||||
/// </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>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vector3 GetAngularVelocity()
|
||||
{
|
||||
if (m_IsRigidbody2D)
|
||||
{
|
||||
return Vector3.forward * m_Rigidbody2D.velocity;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_Rigidbody.angularVelocity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position of the Rigidbody
|
||||
/// </summary>
|
||||
@@ -210,6 +336,9 @@ namespace Unity.Netcode.Components
|
||||
}
|
||||
}
|
||||
|
||||
// Used for Rigidbody only (see info on normalized below)
|
||||
private Vector4 m_QuaternionCheck = Vector4.zero;
|
||||
|
||||
/// <summary>
|
||||
/// Rotatates the Rigidbody towards a specified rotation
|
||||
/// </summary>
|
||||
@@ -227,6 +356,17 @@ namespace Unity.Netcode.Components
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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.
|
||||
m_QuaternionCheck.x = rotation.x;
|
||||
m_QuaternionCheck.y = rotation.y;
|
||||
m_QuaternionCheck.z = rotation.z;
|
||||
m_QuaternionCheck.w = rotation.w;
|
||||
// If the magnitude is greater than 1.0f (even by a very small fractional value), then normalize the quaternion
|
||||
if (m_QuaternionCheck.magnitude != 1.0f)
|
||||
{
|
||||
rotation.Normalize();
|
||||
}
|
||||
m_Rigidbody.MoveRotation(rotation);
|
||||
}
|
||||
}
|
||||
@@ -501,6 +641,8 @@ namespace Unity.Netcode.Components
|
||||
/// <inheritdoc />
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
m_TickFrequency = 1.0f / NetworkManager.NetworkConfig.TickRate;
|
||||
m_TickRate = NetworkManager.NetworkConfig.TickRate;
|
||||
UpdateOwnershipAuthority();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user