com.unity.netcode.gameobjects@1.0.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).

## [1.0.1] - 2022-08-23

### Changed

- Changed version to 1.0.1. (#2131)
- Updated dependency on `com.unity.transport` to 1.2.0. (#2129)
- When using `UnityTransport`, _reliable_ payloads are now allowed to exceed the configured 'Max Payload Size'. Unreliable payloads remain bounded by this setting. (#2081)
- Preformance improvements for cases with large number of NetworkObjects, by not iterating over all unchanged NetworkObjects

### Fixed

- Fixed an issue where reading/writing more than 8 bits at a time with BitReader/BitWriter would write/read from the wrong place, returning and incorrect result. (#2130)
- Fixed issue with the internal `NetworkTransformState.m_Bitset` flag not getting cleared upon the next tick advancement. (#2110)
- Fixed interpolation issue with `NetworkTransform.Teleport`. (#2110)
- Fixed issue where the authoritative side was interpolating its transform. (#2110)
- Fixed Owner-written NetworkVariable infinitely write themselves (#2109)
- Fixed NetworkList issue that showed when inserting at the very end of a NetworkList (#2099)
- Fixed issue where a client owner of a `NetworkVariable` with both owner read and write permissions would not update the server side when changed. (#2097)
- Fixed issue when attempting to spawn a parent `GameObject`, with `NetworkObject` component attached, that has one or more child `GameObject`s, that are inactive in the hierarchy, with `NetworkBehaviour` components it will no longer attempt to spawn the associated `NetworkBehaviour`(s) or invoke ownership changed notifications but will log a warning message. (#2096)
- Fixed an issue where destroying a NetworkBehaviour would not deregister it from the parent NetworkObject, leading to exceptions when the parent was later destroyed. (#2091)
- Fixed issue where `NetworkObject.NetworkHide` was despawning and destroying, as opposed to only despawning, in-scene placed `NetworkObject`s. (#2086)
- Fixed `NetworkAnimator` synchronizing transitions twice due to it detecting the change in animation state once a transition is started by a trigger. (#2084)
- Fixed issue where `NetworkAnimator` would not synchronize a looping animation for late joining clients if it was at the very end of its loop. (#2076)
- Fixed issue where `NetworkAnimator` was not removing its subscription from `OnClientConnectedCallback` when despawned during the shutdown sequence. (#2074)
- Fixed IsServer and IsClient being set to false before object despawn during the shutdown sequence. (#2074)
- Fixed NetworkList Value event on the server. PreviousValue is now set correctly when a new value is set through property setter. (#2067)
- Fixed NetworkLists not populating on client. NetworkList now uses the most recent list as opposed to the list at the end of previous frame, when sending full updates to dynamically spawned NetworkObject. The difference in behaviour is required as scene management spawns those objects at a different time in the frame, relative to updates. (#2062)
This commit is contained in:
Unity Technologies
2022-08-23 00:00:00 +00:00
parent 18ffd5fdc8
commit e15bd056c5
58 changed files with 2192 additions and 1676 deletions

View File

@@ -1,11 +1,6 @@
using System;
using System.Collections;
#if NGO_TRANSFORM_DEBUG
using System.Text.RegularExpressions;
#endif
using Unity.Netcode.Components;
using NUnit.Framework;
// using Unity.Netcode.Samples;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
@@ -14,8 +9,15 @@ namespace Unity.Netcode.RuntimeTests
{
public class NetworkTransformTestComponent : NetworkTransform
{
public bool ServerAuthority;
public bool ReadyToReceivePositionUpdate = false;
protected override bool OnIsServerAuthoritative()
{
return ServerAuthority;
}
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
@@ -23,243 +25,531 @@ namespace Unity.Netcode.RuntimeTests
ReadyToReceivePositionUpdate = true;
}
public void CommitToTransform()
{
TryCommitTransformToServer(transform, NetworkManager.LocalTime.Time);
}
public (bool isDirty, bool isPositionDirty, bool isRotationDirty, bool isScaleDirty) ApplyState()
{
return ApplyLocalNetworkState(transform);
var transformState = ApplyLocalNetworkState(transform);
return (transformState.IsDirty, transformState.HasPositionChange, transformState.HasRotAngleChange, transformState.HasScaleChange);
}
}
// [TestFixture(true, true)]
[TestFixture(true, false)]
// [TestFixture(false, true)]
[TestFixture(false, false)]
[TestFixture(HostOrServer.Host, Authority.Server)]
[TestFixture(HostOrServer.Host, Authority.Owner)]
[TestFixture(HostOrServer.Server, Authority.Server)]
[TestFixture(HostOrServer.Server, Authority.Owner)]
public class NetworkTransformTests : NetcodeIntegrationTest
{
private NetworkObject m_ClientSideClientPlayer;
private NetworkObject m_ServerSideClientPlayer;
private NetworkObject m_AuthoritativePlayer;
private NetworkObject m_NonAuthoritativePlayer;
private readonly bool m_TestWithClientNetworkTransform;
private NetworkTransformTestComponent m_AuthoritativeTransform;
private NetworkTransformTestComponent m_NonAuthoritativeTransform;
private NetworkTransformTestComponent m_OwnerTransform;
public NetworkTransformTests(bool testWithHost, bool testWithClientNetworkTransform)
private readonly Authority m_Authority;
public enum Authority
{
m_UseHost = testWithHost; // from test fixture
m_TestWithClientNetworkTransform = testWithClientNetworkTransform;
Server,
Owner
}
public enum Interpolation
{
DisableInterpolate,
EnableInterpolate
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="testWithHost">Value is set by TestFixture</param>
/// <param name="testWithClientNetworkTransform">Value is set by TestFixture</param>
public NetworkTransformTests(HostOrServer testWithHost, Authority authority)
{
m_UseHost = testWithHost == HostOrServer.Host ? true : false;
m_Authority = authority;
}
protected override int NumberOfClients => 1;
protected override void OnCreatePlayerPrefab()
{
if (m_TestWithClientNetworkTransform)
{
// m_PlayerPrefab.AddComponent<ClientNetworkTransform>();
}
else
{
var networkTransform = m_PlayerPrefab.AddComponent<NetworkTransformTestComponent>();
networkTransform.Interpolate = false;
}
var networkTransformTestComponent = m_PlayerPrefab.AddComponent<NetworkTransformTestComponent>();
networkTransformTestComponent.ServerAuthority = m_Authority == Authority.Server;
}
protected override void OnServerAndClientsCreated()
{
#if NGO_TRANSFORM_DEBUG
// Log assert for writing without authority is a developer log...
// TODO: This is why monolithic test base classes and test helpers are an anti-pattern - this is part of an individual test case setup but is separated from the code verifying it!
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
m_ClientNetworkManagers[0].LogLevel = LogLevel.Developer;
#endif
if (m_EnableVerboseDebug)
{
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.LogLevel = LogLevel.Developer;
}
}
}
protected override IEnumerator OnServerAndClientsConnected()
{
// Get the client player representation on both the server and the client side
m_ServerSideClientPlayer = m_PlayerNetworkObjects[m_ServerNetworkManager.LocalClientId][m_ClientNetworkManagers[0].LocalClientId];
m_ClientSideClientPlayer = m_PlayerNetworkObjects[m_ClientNetworkManagers[0].LocalClientId][m_ClientNetworkManagers[0].LocalClientId];
var serverSideClientPlayer = m_ServerNetworkManager.ConnectedClients[m_ClientNetworkManagers[0].LocalClientId].PlayerObject;
var clientSideClientPlayer = m_ClientNetworkManagers[0].LocalClient.PlayerObject;
m_AuthoritativePlayer = m_Authority == Authority.Server ? serverSideClientPlayer : clientSideClientPlayer;
m_NonAuthoritativePlayer = m_Authority == Authority.Server ? clientSideClientPlayer : serverSideClientPlayer;
// Get the NetworkTransformTestComponent to make sure the client side is ready before starting test
var otherSideNetworkTransformComponent = m_ClientSideClientPlayer.GetComponent<NetworkTransformTestComponent>();
m_AuthoritativeTransform = m_AuthoritativePlayer.GetComponent<NetworkTransformTestComponent>();
m_NonAuthoritativeTransform = m_NonAuthoritativePlayer.GetComponent<NetworkTransformTestComponent>();
m_OwnerTransform = m_AuthoritativeTransform.IsOwner ? m_AuthoritativeTransform : m_NonAuthoritativeTransform;
// Wait for the client-side to notify it is finished initializing and spawning.
yield return WaitForConditionOrTimeOut(() => otherSideNetworkTransformComponent.ReadyToReceivePositionUpdate == true);
yield return WaitForConditionOrTimeOut(() => m_NonAuthoritativeTransform.ReadyToReceivePositionUpdate == true);
AssertOnTimeout("Timed out waiting for client-side to notify it is ready!");
Assert.True(m_AuthoritativeTransform.CanCommitToTransform);
Assert.False(m_NonAuthoritativeTransform.CanCommitToTransform);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for client-side to notify it is ready!");
yield return base.OnServerAndClientsConnected();
}
// TODO: rewrite after perms & authority changes
[UnityTest]
public IEnumerator TestAuthoritativeTransformChangeOneAtATime([Values] bool testLocalTransform)
public enum TransformSpace
{
// Get the client player's NetworkTransform for both instances
var authoritativeNetworkTransform = m_ServerSideClientPlayer.GetComponent<NetworkTransform>();
var otherSideNetworkTransform = m_ClientSideClientPlayer.GetComponent<NetworkTransform>();
Assert.That(!otherSideNetworkTransform.CanCommitToTransform);
Assert.That(authoritativeNetworkTransform.CanCommitToTransform);
if (authoritativeNetworkTransform.CanCommitToTransform)
{
authoritativeNetworkTransform.InLocalSpace = testLocalTransform;
}
if (otherSideNetworkTransform.CanCommitToTransform)
{
otherSideNetworkTransform.InLocalSpace = testLocalTransform;
}
float approximation = 0.05f;
// test position
var authPlayerTransform = authoritativeNetworkTransform.transform;
Assert.AreEqual(Vector3.zero, otherSideNetworkTransform.transform.position, "server side pos should be zero at first"); // sanity check
authPlayerTransform.position = new Vector3(10, 20, 30);
yield return WaitForConditionOrTimeOut(() => otherSideNetworkTransform.transform.position.x > approximation);
Assert.False(s_GlobalTimeoutHelper.TimedOut, $"timeout while waiting for position change! Otherside value {otherSideNetworkTransform.transform.position.x} vs. Approximation {approximation}");
Assert.True(new Vector3(10, 20, 30) == otherSideNetworkTransform.transform.position, $"wrong position on ghost, {otherSideNetworkTransform.transform.position}"); // Vector3 already does float approximation with ==
// test rotation
authPlayerTransform.rotation = Quaternion.Euler(45, 40, 35); // using euler angles instead of quaternions directly to really see issues users might encounter
Assert.AreEqual(Quaternion.identity, otherSideNetworkTransform.transform.rotation, "wrong initial value for rotation"); // sanity check
yield return WaitForConditionOrTimeOut(() => otherSideNetworkTransform.transform.rotation.eulerAngles.x > approximation);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "timeout while waiting for rotation change");
// approximation needed here since eulerAngles isn't super precise.
Assert.LessOrEqual(Math.Abs(45 - otherSideNetworkTransform.transform.rotation.eulerAngles.x), approximation, $"wrong rotation on ghost on x, got {otherSideNetworkTransform.transform.rotation.eulerAngles.x}");
Assert.LessOrEqual(Math.Abs(40 - otherSideNetworkTransform.transform.rotation.eulerAngles.y), approximation, $"wrong rotation on ghost on y, got {otherSideNetworkTransform.transform.rotation.eulerAngles.y}");
Assert.LessOrEqual(Math.Abs(35 - otherSideNetworkTransform.transform.rotation.eulerAngles.z), approximation, $"wrong rotation on ghost on z, got {otherSideNetworkTransform.transform.rotation.eulerAngles.z}");
// test scale
UnityEngine.Assertions.Assert.AreApproximatelyEqual(1f, otherSideNetworkTransform.transform.lossyScale.x, "wrong initial value for scale"); // sanity check
UnityEngine.Assertions.Assert.AreApproximatelyEqual(1f, otherSideNetworkTransform.transform.lossyScale.y, "wrong initial value for scale"); // sanity check
UnityEngine.Assertions.Assert.AreApproximatelyEqual(1f, otherSideNetworkTransform.transform.lossyScale.z, "wrong initial value for scale"); // sanity check
authPlayerTransform.localScale = new Vector3(2, 3, 4);
yield return WaitForConditionOrTimeOut(() => otherSideNetworkTransform.transform.lossyScale.x > 1f + approximation);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "timeout while waiting for scale change");
UnityEngine.Assertions.Assert.AreApproximatelyEqual(2f, otherSideNetworkTransform.transform.lossyScale.x, "wrong scale on ghost");
UnityEngine.Assertions.Assert.AreApproximatelyEqual(3f, otherSideNetworkTransform.transform.lossyScale.y, "wrong scale on ghost");
UnityEngine.Assertions.Assert.AreApproximatelyEqual(4f, otherSideNetworkTransform.transform.lossyScale.z, "wrong scale on ghost");
// todo reparent and test
// todo test all public API
World,
Local
}
[UnityTest]
public IEnumerator TestCantChangeTransformFromOtherSideAuthority([Values] bool testClientAuthority)
public enum OverrideState
{
// Get the client player's NetworkTransform for both instances
var authoritativeNetworkTransform = m_ServerSideClientPlayer.GetComponent<NetworkTransform>();
var otherSideNetworkTransform = m_ClientSideClientPlayer.GetComponent<NetworkTransform>();
Update,
CommitToTransform
}
Assert.AreEqual(Vector3.zero, otherSideNetworkTransform.transform.position, "other side pos should be zero at first"); // sanity check
/// <summary>
/// Tests changing all axial values one at a time.
/// These tests are performed:
/// - While in local space and world space
/// - While interpolation is enabled and disabled
/// - Using the TryCommitTransformToServer "override" that can be used
/// from a child derived or external class.
/// </summary>
[UnityTest]
public IEnumerator TestAuthoritativeTransformChangeOneAtATime([Values] TransformSpace testLocalTransform, [Values] Interpolation interpolation, [Values] OverrideState overideState)
{
var overrideUpdate = overideState == OverrideState.CommitToTransform;
m_AuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
m_NonAuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
otherSideNetworkTransform.transform.position = new Vector3(4, 5, 6);
m_AuthoritativeTransform.InLocalSpace = testLocalTransform == TransformSpace.Local;
// test position
var authPlayerTransform = overrideUpdate ? m_OwnerTransform.transform : m_AuthoritativeTransform.transform;
Assert.AreEqual(Vector3.zero, m_NonAuthoritativeTransform.transform.position, "server side pos should be zero at first"); // sanity check
authPlayerTransform.position = new Vector3(10, 20, 30);
if (overrideUpdate)
{
m_OwnerTransform.CommitToTransform();
}
yield return WaitForConditionOrTimeOut(PositionsMatch);
AssertOnTimeout($"Timed out waiting for positions to match");
// test rotation
Assert.AreEqual(Quaternion.identity, m_NonAuthoritativeTransform.transform.rotation, "wrong initial value for rotation"); // sanity check
authPlayerTransform.rotation = Quaternion.Euler(45, 40, 35); // using euler angles instead of quaternions directly to really see issues users might encounter
if (overrideUpdate)
{
m_OwnerTransform.CommitToTransform();
}
yield return WaitForConditionOrTimeOut(RotationsMatch);
AssertOnTimeout($"Timed out waiting for rotations to match");
authPlayerTransform.localScale = new Vector3(2, 3, 4);
if (overrideUpdate)
{
m_OwnerTransform.CommitToTransform();
}
yield return WaitForConditionOrTimeOut(ScaleValuesMatch);
AssertOnTimeout($"Timed out waiting for scale values to match");
}
/// <summary>
/// Test to verify nonAuthority cannot change the transform directly
/// </summary>
[UnityTest]
public IEnumerator VerifyNonAuthorityCantChangeTransform([Values] Interpolation interpolation)
{
m_AuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
m_NonAuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
Assert.AreEqual(Vector3.zero, m_NonAuthoritativeTransform.transform.position, "other side pos should be zero at first"); // sanity check
m_NonAuthoritativeTransform.transform.position = new Vector3(4, 5, 6);
yield return s_DefaultWaitForTick;
Assert.AreEqual(Vector3.zero, otherSideNetworkTransform.transform.position, "got authority error, but other side still moved!");
#if NGO_TRANSFORM_DEBUG
// We are no longer emitting this warning, and we are banishing tests that rely on console output, so
// needs re-implementation
// TODO: This should be a separate test - verify 1 behavior per test
LogAssert.Expect(LogType.Warning, new Regex(".*without authority detected.*"));
#endif
}
Assert.AreEqual(Vector3.zero, m_NonAuthoritativeTransform.transform.position, "[Position] NonAuthority was able to change the position!");
var nonAuthorityRotation = m_NonAuthoritativeTransform.transform.rotation;
var originalNonAuthorityEulerRotation = nonAuthorityRotation.eulerAngles;
var nonAuthorityEulerRotation = originalNonAuthorityEulerRotation;
// Verify rotation is not marked dirty when rotated by half of the threshold
nonAuthorityEulerRotation.y += 20.0f;
nonAuthorityRotation.eulerAngles = nonAuthorityEulerRotation;
m_NonAuthoritativeTransform.transform.rotation = nonAuthorityRotation;
yield return s_DefaultWaitForTick;
var nonAuthorityCurrentEuler = m_NonAuthoritativeTransform.transform.rotation.eulerAngles;
Assert.True(originalNonAuthorityEulerRotation.Equals(nonAuthorityCurrentEuler), "[Rotation] NonAuthority was able to change the rotation!");
var nonAuthorityScale = m_NonAuthoritativeTransform.transform.localScale;
m_NonAuthoritativeTransform.transform.localScale = nonAuthorityScale * 100;
yield return s_DefaultWaitForTick;
Assert.True(nonAuthorityScale.Equals(m_NonAuthoritativeTransform.transform.localScale), "[Scale] NonAuthority was able to change the scale!");
}
/// <summary>
/// Validates that rotation checks don't produce false positive
/// results when rolling over between 0 and 360 degrees
/// </summary>
[UnityTest]
public IEnumerator TestRotationThresholdDeltaCheck()
public IEnumerator TestRotationThresholdDeltaCheck([Values] Interpolation interpolation)
{
// Get the client player's NetworkTransform for both instances
var authoritativeNetworkTransform = m_ServerSideClientPlayer.GetComponent<NetworkTransformTestComponent>();
var otherSideNetworkTransform = m_ClientSideClientPlayer.GetComponent<NetworkTransformTestComponent>();
otherSideNetworkTransform.RotAngleThreshold = authoritativeNetworkTransform.RotAngleThreshold = 5.0f;
m_AuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
m_NonAuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
var halfThreshold = authoritativeNetworkTransform.RotAngleThreshold * 0.5001f;
var serverRotation = authoritativeNetworkTransform.transform.rotation;
var serverEulerRotation = serverRotation.eulerAngles;
m_NonAuthoritativeTransform.RotAngleThreshold = m_AuthoritativeTransform.RotAngleThreshold = 5.0f;
var halfThreshold = m_AuthoritativeTransform.RotAngleThreshold * 0.5001f;
var authorityRotation = m_AuthoritativeTransform.transform.rotation;
var authorityEulerRotation = authorityRotation.eulerAngles;
// Verify rotation is not marked dirty when rotated by half of the threshold
serverEulerRotation.y += halfThreshold;
serverRotation.eulerAngles = serverEulerRotation;
authoritativeNetworkTransform.transform.rotation = serverRotation;
var results = authoritativeNetworkTransform.ApplyState();
Assert.IsFalse(results.isRotationDirty, $"Rotation is dirty when rotation threshold is {authoritativeNetworkTransform.RotAngleThreshold} degrees and only adjusted by {halfThreshold} degrees!");
authorityEulerRotation.y += halfThreshold;
authorityRotation.eulerAngles = authorityEulerRotation;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
var results = m_AuthoritativeTransform.ApplyState();
Assert.IsFalse(results.isRotationDirty, $"Rotation is dirty when rotation threshold is {m_AuthoritativeTransform.RotAngleThreshold} degrees and only adjusted by {halfThreshold} degrees!");
yield return s_DefaultWaitForTick;
// Verify rotation is marked dirty when rotated by another half threshold value
serverEulerRotation.y += halfThreshold;
serverRotation.eulerAngles = serverEulerRotation;
authoritativeNetworkTransform.transform.rotation = serverRotation;
results = authoritativeNetworkTransform.ApplyState();
Assert.IsTrue(results.isRotationDirty, $"Rotation was not dirty when rotated by the threshold value: {authoritativeNetworkTransform.RotAngleThreshold} degrees!");
authorityEulerRotation.y += halfThreshold;
authorityRotation.eulerAngles = authorityEulerRotation;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
results = m_AuthoritativeTransform.ApplyState();
Assert.IsTrue(results.isRotationDirty, $"Rotation was not dirty when rotated by the threshold value: {m_AuthoritativeTransform.RotAngleThreshold} degrees!");
yield return s_DefaultWaitForTick;
//Reset rotation back to zero on all axis
serverRotation.eulerAngles = serverEulerRotation = Vector3.zero;
authoritativeNetworkTransform.transform.rotation = serverRotation;
authorityRotation.eulerAngles = authorityEulerRotation = Vector3.zero;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
yield return s_DefaultWaitForTick;
// Rotate by 360 minus halfThreshold (which is really just negative halfThreshold) and verify rotation is not marked dirty
serverEulerRotation.y = 360 - halfThreshold;
serverRotation.eulerAngles = serverEulerRotation;
authoritativeNetworkTransform.transform.rotation = serverRotation;
results = authoritativeNetworkTransform.ApplyState();
authorityEulerRotation.y = 360 - halfThreshold;
authorityRotation.eulerAngles = authorityEulerRotation;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
results = m_AuthoritativeTransform.ApplyState();
Assert.IsFalse(results.isRotationDirty, $"Rotation is dirty when rotation threshold is {authoritativeNetworkTransform.RotAngleThreshold} degrees and only adjusted by " +
$"{Mathf.DeltaAngle(0, serverEulerRotation.y)} degrees!");
Assert.IsFalse(results.isRotationDirty, $"Rotation is dirty when rotation threshold is {m_AuthoritativeTransform.RotAngleThreshold} degrees and only adjusted by " +
$"{Mathf.DeltaAngle(0, authorityEulerRotation.y)} degrees!");
serverEulerRotation.y -= halfThreshold;
serverRotation.eulerAngles = serverEulerRotation;
authoritativeNetworkTransform.transform.rotation = serverRotation;
results = authoritativeNetworkTransform.ApplyState();
authorityEulerRotation.y -= halfThreshold;
authorityRotation.eulerAngles = authorityEulerRotation;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
results = m_AuthoritativeTransform.ApplyState();
Assert.IsTrue(results.isRotationDirty, $"Rotation was not dirty when rotated by {Mathf.DeltaAngle(0, serverEulerRotation.y)} degrees!");
Assert.IsTrue(results.isRotationDirty, $"Rotation was not dirty when rotated by {Mathf.DeltaAngle(0, authorityEulerRotation.y)} degrees!");
//Reset rotation back to zero on all axis
serverRotation.eulerAngles = serverEulerRotation = Vector3.zero;
authoritativeNetworkTransform.transform.rotation = serverRotation;
authorityRotation.eulerAngles = authorityEulerRotation = Vector3.zero;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
yield return s_DefaultWaitForTick;
serverEulerRotation.y -= halfThreshold;
serverRotation.eulerAngles = serverEulerRotation;
authoritativeNetworkTransform.transform.rotation = serverRotation;
results = authoritativeNetworkTransform.ApplyState();
Assert.IsFalse(results.isRotationDirty, $"Rotation is dirty when rotation threshold is {authoritativeNetworkTransform.RotAngleThreshold} degrees and only adjusted by " +
$"{Mathf.DeltaAngle(0, serverEulerRotation.y)} degrees!");
authorityEulerRotation.y -= halfThreshold;
authorityRotation.eulerAngles = authorityEulerRotation;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
results = m_AuthoritativeTransform.ApplyState();
Assert.IsFalse(results.isRotationDirty, $"Rotation is dirty when rotation threshold is {m_AuthoritativeTransform.RotAngleThreshold} degrees and only adjusted by " +
$"{Mathf.DeltaAngle(0, authorityEulerRotation.y)} degrees!");
serverEulerRotation.y -= halfThreshold;
serverRotation.eulerAngles = serverEulerRotation;
authoritativeNetworkTransform.transform.rotation = serverRotation;
results = authoritativeNetworkTransform.ApplyState();
authorityEulerRotation.y -= halfThreshold;
authorityRotation.eulerAngles = authorityEulerRotation;
m_AuthoritativeTransform.transform.rotation = authorityRotation;
results = m_AuthoritativeTransform.ApplyState();
Assert.IsTrue(results.isRotationDirty, $"Rotation was not dirty when rotated by {Mathf.DeltaAngle(0, serverEulerRotation.y)} degrees!");
Assert.IsTrue(results.isRotationDirty, $"Rotation was not dirty when rotated by {Mathf.DeltaAngle(0, authorityEulerRotation.y)} degrees!");
}
private bool ValidateBitSetValues(NetworkTransform.NetworkTransformState serverState, NetworkTransform.NetworkTransformState clientState)
{
if (serverState.HasPositionX == clientState.HasPositionX && serverState.HasPositionY == clientState.HasPositionY && serverState.HasPositionZ == clientState.HasPositionZ &&
serverState.HasRotAngleX == clientState.HasRotAngleX && serverState.HasRotAngleY == clientState.HasRotAngleY && serverState.HasRotAngleZ == clientState.HasRotAngleZ &&
serverState.HasScaleX == clientState.HasScaleX && serverState.HasScaleY == clientState.HasScaleY && serverState.HasScaleZ == clientState.HasScaleZ)
{
return true;
}
return false;
}
/// <summary>
/// Test to make sure that the bitset value is updated properly
/// </summary>
[UnityTest]
public IEnumerator TestBitsetValue([Values] Interpolation interpolation)
{
m_AuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
m_NonAuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
m_NonAuthoritativeTransform.RotAngleThreshold = m_AuthoritativeTransform.RotAngleThreshold = 0.1f;
yield return s_DefaultWaitForTick;
m_AuthoritativeTransform.transform.rotation = Quaternion.Euler(1, 2, 3);
var serverLastSentState = m_AuthoritativeTransform.GetLastSentState();
var clientReplicatedState = m_NonAuthoritativeTransform.ReplicatedNetworkState.Value;
yield return WaitForConditionOrTimeOut(() => ValidateBitSetValues(serverLastSentState, clientReplicatedState));
AssertOnTimeout($"Timed out waiting for Authoritative Bitset state to equal NonAuthoritative replicated Bitset state!");
yield return WaitForConditionOrTimeOut(RotationsMatch);
AssertOnTimeout($"[Timed-Out] Authoritative rotation {m_AuthoritativeTransform.transform.rotation.eulerAngles} != Non-Authoritative rotation {m_NonAuthoritativeTransform.transform.rotation.eulerAngles}");
}
private float m_DetectedPotentialInterpolatedTeleport;
/// <summary>
/// The tests teleporting with and without interpolation
/// </summary>
[UnityTest]
public IEnumerator TeleportTest([Values] Interpolation interpolation)
{
m_AuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
m_NonAuthoritativeTransform.Interpolate = interpolation == Interpolation.EnableInterpolate;
var authTransform = m_AuthoritativeTransform.transform;
var nonAuthPosition = m_NonAuthoritativeTransform.transform.position;
var currentTick = m_AuthoritativeTransform.NetworkManager.ServerTime.Tick;
m_DetectedPotentialInterpolatedTeleport = 0.0f;
var teleportDestination = new Vector3(100.00f, 100.00f, 100.00f);
var targetDistance = Mathf.Abs(Vector3.Distance(nonAuthPosition, teleportDestination));
m_AuthoritativeTransform.Teleport(new Vector3(100.00f, 100.00f, 100.00f), authTransform.rotation, authTransform.localScale);
yield return WaitForConditionOrTimeOut(() => TeleportPositionMatches(nonAuthPosition));
AssertOnTimeout($"[Timed-Out][Teleport] Timed out waiting for NonAuthoritative position to !");
Assert.IsTrue(m_DetectedPotentialInterpolatedTeleport == 0.0f, $"Detected possible interpolation on non-authority side! NonAuthority distance: {m_DetectedPotentialInterpolatedTeleport} | Target distance: {targetDistance}");
}
/// <summary>
/// This test validates the <see cref="NetworkTransform.SetState(Vector3?, Quaternion?, Vector3?, bool)"/> method
/// usage for the non-authoritative side. It will either be the owner or the server making/requesting state changes.
/// This validates that:
/// - The owner authoritative mode can still be controlled by the server (i.e. owner authoritative with server authority override capabilities)
/// - The server authoritative mode can still be directed by the client owner.
/// </summary>
/// <remarks>
/// This also tests that the original server authoritative model with client-owner driven NetworkTransforms is preserved.
/// </remarks>
[UnityTest]
public IEnumerator NonAuthorityOwnerSettingStateTest([Values] Interpolation interpolation)
{
var interpolate = interpolation == Interpolation.EnableInterpolate;
m_AuthoritativeTransform.Interpolate = interpolate;
m_NonAuthoritativeTransform.Interpolate = interpolate;
m_NonAuthoritativeTransform.RotAngleThreshold = m_AuthoritativeTransform.RotAngleThreshold = 0.1f;
// Test one parameter at a time first
var newPosition = new Vector3(125f, 35f, 65f);
var newRotation = Quaternion.Euler(1, 2, 3);
var newScale = new Vector3(2.0f, 2.0f, 2.0f);
m_NonAuthoritativeTransform.SetState(newPosition, null, null, interpolate);
yield return WaitForConditionOrTimeOut(() => PositionsMatchesValue(newPosition));
AssertOnTimeout($"Timed out waiting for non-authoritative position state request to be applied!");
Assert.True(Aproximately(newPosition, m_AuthoritativeTransform.transform.position), "Authoritative position does not match!");
Assert.True(Aproximately(newPosition, m_NonAuthoritativeTransform.transform.position), "Non-Authoritative position does not match!");
m_NonAuthoritativeTransform.SetState(null, newRotation, null, interpolate);
yield return WaitForConditionOrTimeOut(() => RotationMatchesValue(newRotation.eulerAngles));
AssertOnTimeout($"Timed out waiting for non-authoritative rotation state request to be applied!");
Assert.True(Aproximately(newRotation.eulerAngles, m_AuthoritativeTransform.transform.rotation.eulerAngles), "Authoritative rotation does not match!");
Assert.True(Aproximately(newRotation.eulerAngles, m_NonAuthoritativeTransform.transform.rotation.eulerAngles), "Non-Authoritative rotation does not match!");
m_NonAuthoritativeTransform.SetState(null, null, newScale, interpolate);
yield return WaitForConditionOrTimeOut(() => ScaleMatchesValue(newScale));
AssertOnTimeout($"Timed out waiting for non-authoritative scale state request to be applied!");
Assert.True(Aproximately(newScale, m_AuthoritativeTransform.transform.localScale), "Authoritative scale does not match!");
Assert.True(Aproximately(newScale, m_NonAuthoritativeTransform.transform.localScale), "Non-Authoritative scale does not match!");
// Test all parameters at once
newPosition = new Vector3(55f, 95f, -25f);
newRotation = Quaternion.Euler(20, 5, 322);
newScale = new Vector3(0.5f, 0.5f, 0.5f);
m_NonAuthoritativeTransform.SetState(newPosition, newRotation, newScale, interpolate);
yield return WaitForConditionOrTimeOut(() => PositionRotationScaleMatches(newPosition, newRotation.eulerAngles, newScale));
AssertOnTimeout($"Timed out waiting for non-authoritative position, rotation, and scale state request to be applied!");
Assert.True(Aproximately(newPosition, m_AuthoritativeTransform.transform.position), "Authoritative position does not match!");
Assert.True(Aproximately(newPosition, m_NonAuthoritativeTransform.transform.position), "Non-Authoritative position does not match!");
Assert.True(Aproximately(newRotation.eulerAngles, m_AuthoritativeTransform.transform.rotation.eulerAngles), "Authoritative rotation does not match!");
Assert.True(Aproximately(newRotation.eulerAngles, m_NonAuthoritativeTransform.transform.rotation.eulerAngles), "Non-Authoritative rotation does not match!");
Assert.True(Aproximately(newScale, m_AuthoritativeTransform.transform.localScale), "Authoritative scale does not match!");
Assert.True(Aproximately(newScale, m_NonAuthoritativeTransform.transform.localScale), "Non-Authoritative scale does not match!");
}
private bool Aproximately(float x, float y)
{
return Mathf.Abs(x - y) <= k_AproximateDeltaVariance;
}
private bool Aproximately(Vector3 a, Vector3 b)
{
return Mathf.Abs(a.x - b.x) <= k_AproximateDeltaVariance &&
Mathf.Abs(a.y - b.y) <= k_AproximateDeltaVariance &&
Mathf.Abs(a.z - b.z) <= k_AproximateDeltaVariance;
}
private const float k_AproximateDeltaVariance = 0.01f;
private bool PositionsMatchesValue(Vector3 positionToMatch)
{
var authorityPosition = m_AuthoritativeTransform.transform.position;
var nonAuthorityPosition = m_NonAuthoritativeTransform.transform.position;
var auhtorityIsEqual = Aproximately(authorityPosition, positionToMatch);
var nonauthorityIsEqual = Aproximately(nonAuthorityPosition, positionToMatch);
if (!auhtorityIsEqual)
{
VerboseDebug($"Authority position {authorityPosition} != position to match: {positionToMatch}!");
}
if (!nonauthorityIsEqual)
{
VerboseDebug($"NonAuthority position {nonAuthorityPosition} != position to match: {positionToMatch}!");
}
return auhtorityIsEqual && nonauthorityIsEqual;
}
private bool RotationMatchesValue(Vector3 rotationEulerToMatch)
{
var authorityRotationEuler = m_AuthoritativeTransform.transform.rotation.eulerAngles;
var nonAuthorityRotationEuler = m_NonAuthoritativeTransform.transform.rotation.eulerAngles;
var auhtorityIsEqual = Aproximately(authorityRotationEuler, rotationEulerToMatch);
var nonauthorityIsEqual = Aproximately(nonAuthorityRotationEuler, rotationEulerToMatch);
if (!auhtorityIsEqual)
{
VerboseDebug($"Authority rotation {authorityRotationEuler} != rotation to match: {rotationEulerToMatch}!");
}
if (!nonauthorityIsEqual)
{
VerboseDebug($"NonAuthority position {nonAuthorityRotationEuler} != rotation to match: {rotationEulerToMatch}!");
}
return auhtorityIsEqual && nonauthorityIsEqual;
}
private bool ScaleMatchesValue(Vector3 scaleToMatch)
{
var authorityScale = m_AuthoritativeTransform.transform.localScale;
var nonAuthorityScale = m_NonAuthoritativeTransform.transform.localScale;
var auhtorityIsEqual = Aproximately(authorityScale, scaleToMatch);
var nonauthorityIsEqual = Aproximately(nonAuthorityScale, scaleToMatch);
if (!auhtorityIsEqual)
{
VerboseDebug($"Authority scale {authorityScale} != scale to match: {scaleToMatch}!");
}
if (!nonauthorityIsEqual)
{
VerboseDebug($"NonAuthority scale {nonAuthorityScale} != scale to match: {scaleToMatch}!");
}
return auhtorityIsEqual && nonauthorityIsEqual;
}
private bool TeleportPositionMatches(Vector3 nonAuthorityOriginalPosition)
{
var nonAuthorityPosition = m_NonAuthoritativeTransform.transform.position;
var authorityPosition = m_AuthoritativeTransform.transform.position;
var targetDistance = Mathf.Abs(Vector3.Distance(nonAuthorityOriginalPosition, authorityPosition));
var nonAuthorityCurrentDistance = Mathf.Abs(Vector3.Distance(nonAuthorityPosition, nonAuthorityOriginalPosition));
if (!Aproximately(targetDistance, nonAuthorityCurrentDistance))
{
if (nonAuthorityCurrentDistance >= 0.15f * targetDistance && nonAuthorityCurrentDistance <= 0.75f * targetDistance)
{
m_DetectedPotentialInterpolatedTeleport = nonAuthorityCurrentDistance;
}
return false;
}
var xIsEqual = Aproximately(authorityPosition.x, nonAuthorityPosition.x);
var yIsEqual = Aproximately(authorityPosition.y, nonAuthorityPosition.y);
var zIsEqual = Aproximately(authorityPosition.z, nonAuthorityPosition.z);
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"Authority position {authorityPosition} != NonAuthority position {nonAuthorityPosition}");
}
return xIsEqual && yIsEqual && zIsEqual; ;
}
private bool PositionRotationScaleMatches(Vector3 position, Vector3 eulerRotation, Vector3 scale)
{
return PositionsMatchesValue(position) && RotationMatchesValue(eulerRotation) && ScaleMatchesValue(scale);
}
private bool RotationsMatch()
{
var authorityEulerRotation = m_AuthoritativeTransform.transform.rotation.eulerAngles;
var nonAuthorityEulerRotation = m_NonAuthoritativeTransform.transform.rotation.eulerAngles;
var xIsEqual = Aproximately(authorityEulerRotation.x, nonAuthorityEulerRotation.x);
var yIsEqual = Aproximately(authorityEulerRotation.y, nonAuthorityEulerRotation.y);
var zIsEqual = Aproximately(authorityEulerRotation.z, nonAuthorityEulerRotation.z);
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"Authority rotation {authorityEulerRotation} != NonAuthority rotation {nonAuthorityEulerRotation}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
private bool PositionsMatch()
{
var authorityPosition = m_AuthoritativeTransform.transform.position;
var nonAuthorityPosition = m_NonAuthoritativeTransform.transform.position;
var xIsEqual = Aproximately(authorityPosition.x, nonAuthorityPosition.x);
var yIsEqual = Aproximately(authorityPosition.y, nonAuthorityPosition.y);
var zIsEqual = Aproximately(authorityPosition.z, nonAuthorityPosition.z);
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"Authority position {authorityPosition} != NonAuthority position {nonAuthorityPosition}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
private bool ScaleValuesMatch()
{
var authorityScale = m_AuthoritativeTransform.transform.localScale;
var nonAuthorityScale = m_NonAuthoritativeTransform.transform.localScale;
var xIsEqual = Aproximately(authorityScale.x, nonAuthorityScale.x);
var yIsEqual = Aproximately(authorityScale.y, nonAuthorityScale.y);
var zIsEqual = Aproximately(authorityScale.z, nonAuthorityScale.z);
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"Authority scale {authorityScale} != NonAuthority scale {nonAuthorityScale}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
/*
* ownership change
* test teleport with interpolation
* test teleport without interpolation
* test dynamic spawning
*/
protected override IEnumerator OnTearDown()
{
UnityEngine.Object.DestroyImmediate(m_PlayerPrefab);
m_EnableVerboseDebug = false;
Object.DestroyImmediate(m_PlayerPrefab);
yield return base.OnTearDown();
}
}