This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransformAnticipationTests.cs
Unity Technologies 48c6a6121c com.unity.netcode.gameobjects@2.0.0
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] - 2024-09-12

### Added

- Added tooltips for all of the `NetworkObject` component's properties. (#3052)
- Added message size validation to named and unnamed message sending functions for better error messages. (#3049)
- Added "Check for NetworkObject Component" property to the Multiplayer->Netcode for GameObjects project settings. When disabled, this will bypass the in-editor `NetworkObject` check on `NetworkBehaviour` components. (#3031)
- Added `NetworkTransform.SwitchTransformSpaceWhenParented` property that, when enabled, will handle the world to local, local to world, and local to local transform space transitions when interpolation is enabled. (#3013)
- Added `NetworkTransform.TickSyncChildren` that, when enabled, will tick synchronize nested and/or child `NetworkTransform` components to eliminate any potential visual jittering that could occur if the `NetworkTransform` instances get into a state where their state updates are landing on different network ticks. (#3013)
- Added `NetworkObject.AllowOwnerToParent` property to provide the ability to allow clients to parent owned objects when running in a client-server network topology. (#3013)
- Added `NetworkObject.SyncOwnerTransformWhenParented` property to provide a way to disable applying the server's transform information in the parenting message on the client owner instance which can be useful for owner authoritative motion models. (#3013)
- Added `NetcodeEditorBase` editor helper class to provide easier modification and extension of the SDK's components. (#3013)

### Fixed

- Fixed issue where `NetworkAnimator` would send updates to non-observer clients. (#3057)
- Fixed issue where an exception could occur when receiving a universal RPC for a `NetworkObject` that has been despawned. (#3052)
- Fixed issue where a NetworkObject hidden from a client that is then promoted to be session owner was not being synchronized with newly joining clients.(#3051)
- Fixed issue where clients could have a wrong time delta on `NetworkVariableBase` which could prevent from sending delta state updates. (#3045)
- Fixed issue where setting a prefab hash value during connection approval but not having a player prefab assigned could cause an exception when spawning a player. (#3042)
- Fixed issue where the `NetworkSpawnManager.HandleNetworkObjectShow` could throw an exception if one of the `NetworkObject` components to show was destroyed during the same frame. (#3030)
- Fixed issue where the `NetworkManagerHelper` was continuing to check for hierarchy changes when in play mode. (#3026)
- Fixed issue with newly/late joined clients and `NetworkTransform` synchronization of parented `NetworkObject` instances. (#3013)
- Fixed issue with smooth transitions between transform spaces when interpolation is enabled (requires `NetworkTransform.SwitchTransformSpaceWhenParented` to be enabled). (#3013)

### Changed

- Changed `NetworkTransformEditor` now uses `NetworkTransform` as the base type class to assure it doesn't display a foldout group when using the base `NetworkTransform` component class. (#3052)
- Changed `NetworkAnimator.Awake` is now a protected virtual method. (#3052)
- Changed  when invoking `NetworkManager.ConnectionManager.DisconnectClient` during a distributed authority session a more appropriate message is logged. (#3052)
- Changed `NetworkTransformEditor` so it now derives from `NetcodeEditorBase`. (#3013)
- Changed `NetworkRigidbodyBaseEditor` so it now derives from `NetcodeEditorBase`. (#3013)
- Changed `NetworkManagerEditor` so it now derives from `NetcodeEditorBase`. (#3013)
2024-09-12 00:00:00 +00:00

521 lines
28 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Unity.Netcode.Components;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Unity.Netcode.RuntimeTests
{
internal class NetworkTransformAnticipationComponent : NetworkBehaviour
{
[Rpc(SendTo.Server)]
public void MoveRpc(Vector3 newPosition)
{
transform.position = newPosition;
}
[Rpc(SendTo.Server)]
public void ScaleRpc(Vector3 newScale)
{
transform.localScale = newScale;
}
[Rpc(SendTo.Server)]
public void RotateRpc(Quaternion newRotation)
{
transform.rotation = newRotation;
}
public bool ShouldSmooth = false;
public bool ShouldMove = false;
public override void OnReanticipate(double lastRoundTripTime)
{
var transform_ = GetComponent<AnticipatedNetworkTransform>();
if (transform_.ShouldReanticipate)
{
if (ShouldSmooth)
{
transform_.Smooth(transform_.PreviousAnticipatedState, transform_.AuthoritativeState, 1);
}
if (ShouldMove)
{
transform_.AnticipateMove(transform_.AuthoritativeState.Position + new Vector3(0, 5, 0));
}
}
}
}
internal class NetworkTransformAnticipationTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;
protected override bool m_EnableTimeTravel => true;
protected override bool m_SetupIsACoroutine => false;
protected override bool m_TearDownIsACoroutine => false;
protected override void OnPlayerPrefabGameObjectCreated()
{
m_PlayerPrefab.AddComponent<AnticipatedNetworkTransform>();
m_PlayerPrefab.AddComponent<NetworkTransformAnticipationComponent>();
}
protected override void OnTimeTravelServerAndClientsConnected()
{
var serverComponent = GetServerComponent();
var testComponent = GetTestComponent();
var otherClientComponent = GetOtherClientComponent();
serverComponent.transform.position = Vector3.zero;
serverComponent.transform.localScale = Vector3.one;
serverComponent.transform.rotation = Quaternion.LookRotation(Vector3.forward);
testComponent.transform.position = Vector3.zero;
testComponent.transform.localScale = Vector3.one;
testComponent.transform.rotation = Quaternion.LookRotation(Vector3.forward);
otherClientComponent.transform.position = Vector3.zero;
otherClientComponent.transform.localScale = Vector3.one;
otherClientComponent.transform.rotation = Quaternion.LookRotation(Vector3.forward);
}
public AnticipatedNetworkTransform GetTestComponent()
{
return m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<AnticipatedNetworkTransform>();
}
public AnticipatedNetworkTransform GetServerComponent()
{
foreach (var obj in Object.FindObjectsByType<AnticipatedNetworkTransform>(FindObjectsSortMode.None))
{
if (obj.NetworkManager == m_ServerNetworkManager && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
{
return obj;
}
}
return null;
}
public AnticipatedNetworkTransform GetOtherClientComponent()
{
foreach (var obj in Object.FindObjectsByType<AnticipatedNetworkTransform>(FindObjectsSortMode.None))
{
if (obj.NetworkManager == m_ClientNetworkManagers[1] && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
{
return obj;
}
}
return null;
}
[Test]
public void WhenAnticipating_ValueChangesImmediately()
{
var testComponent = GetTestComponent();
testComponent.AnticipateMove(new Vector3(0, 1, 2));
testComponent.AnticipateScale(new Vector3(1, 2, 3));
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
Assert.AreEqual(new Vector3(0, 1, 2), testComponent.transform.position);
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.transform.localScale);
Assert.AreEqual(Quaternion.LookRotation(new Vector3(2, 3, 4)), testComponent.transform.rotation);
Assert.AreEqual(new Vector3(0, 1, 2), testComponent.AnticipatedState.Position);
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AnticipatedState.Scale);
Assert.AreEqual(Quaternion.LookRotation(new Vector3(2, 3, 4)), testComponent.AnticipatedState.Rotation);
}
[Test]
public void WhenAnticipating_AuthoritativeValueDoesNotChange()
{
var testComponent = GetTestComponent();
var startPosition = testComponent.transform.position;
var startScale = testComponent.transform.localScale;
var startRotation = testComponent.transform.rotation;
testComponent.AnticipateMove(new Vector3(0, 1, 2));
testComponent.AnticipateScale(new Vector3(1, 2, 3));
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
Assert.AreEqual(startPosition, testComponent.AuthoritativeState.Position);
Assert.AreEqual(startScale, testComponent.AuthoritativeState.Scale);
Assert.AreEqual(startRotation, testComponent.AuthoritativeState.Rotation);
}
[Test]
public void WhenAnticipating_ServerDoesNotChange()
{
var testComponent = GetTestComponent();
var startPosition = testComponent.transform.position;
var startScale = testComponent.transform.localScale;
var startRotation = testComponent.transform.rotation;
testComponent.AnticipateMove(new Vector3(0, 1, 2));
testComponent.AnticipateScale(new Vector3(1, 2, 3));
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
var serverComponent = GetServerComponent();
Assert.AreEqual(startPosition, serverComponent.AuthoritativeState.Position);
Assert.AreEqual(startScale, serverComponent.AuthoritativeState.Scale);
Assert.AreEqual(startRotation, serverComponent.AuthoritativeState.Rotation);
Assert.AreEqual(startPosition, serverComponent.AnticipatedState.Position);
Assert.AreEqual(startScale, serverComponent.AnticipatedState.Scale);
Assert.AreEqual(startRotation, serverComponent.AnticipatedState.Rotation);
TimeTravel(2, 120);
Assert.AreEqual(startPosition, serverComponent.AuthoritativeState.Position);
Assert.AreEqual(startScale, serverComponent.AuthoritativeState.Scale);
Assert.AreEqual(startRotation, serverComponent.AuthoritativeState.Rotation);
Assert.AreEqual(startPosition, serverComponent.AnticipatedState.Position);
Assert.AreEqual(startScale, serverComponent.AnticipatedState.Scale);
Assert.AreEqual(startRotation, serverComponent.AnticipatedState.Rotation);
}
[Test]
public void WhenAnticipating_OtherClientDoesNotChange()
{
var testComponent = GetTestComponent();
var startPosition = testComponent.transform.position;
var startScale = testComponent.transform.localScale;
var startRotation = testComponent.transform.rotation;
testComponent.AnticipateMove(new Vector3(0, 1, 2));
testComponent.AnticipateScale(new Vector3(1, 2, 3));
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
var otherClientComponent = GetOtherClientComponent();
Assert.AreEqual(startPosition, otherClientComponent.AuthoritativeState.Position);
Assert.AreEqual(startScale, otherClientComponent.AuthoritativeState.Scale);
Assert.AreEqual(startRotation, otherClientComponent.AuthoritativeState.Rotation);
Assert.AreEqual(startPosition, otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(startScale, otherClientComponent.AnticipatedState.Scale);
Assert.AreEqual(startRotation, otherClientComponent.AnticipatedState.Rotation);
TimeTravel(2, 120);
Assert.AreEqual(startPosition, otherClientComponent.AuthoritativeState.Position);
Assert.AreEqual(startScale, otherClientComponent.AuthoritativeState.Scale);
Assert.AreEqual(startRotation, otherClientComponent.AuthoritativeState.Rotation);
Assert.AreEqual(startPosition, otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(startScale, otherClientComponent.AnticipatedState.Scale);
Assert.AreEqual(startRotation, otherClientComponent.AnticipatedState.Rotation);
}
[Test]
public void WhenServerChangesSnapValue_ValuesAreUpdated()
{
var testComponent = GetTestComponent();
var serverComponent = GetServerComponent();
serverComponent.Interpolate = false;
testComponent.AnticipateMove(new Vector3(0, 1, 2));
testComponent.AnticipateScale(new Vector3(1, 2, 3));
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
rpcComponent.MoveRpc(new Vector3(2, 3, 4));
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
var otherClientComponent = GetOtherClientComponent();
WaitForConditionOrTimeOutWithTimeTravel(() => testComponent.AuthoritativeState.Position == serverComponent.transform.position && otherClientComponent.AuthoritativeState.Position == serverComponent.transform.position);
Assert.AreEqual(serverComponent.transform.position, testComponent.transform.position);
Assert.AreEqual(serverComponent.transform.position, testComponent.AnticipatedState.Position);
Assert.AreEqual(serverComponent.transform.position, testComponent.AuthoritativeState.Position);
Assert.AreEqual(serverComponent.transform.position, otherClientComponent.transform.position);
Assert.AreEqual(serverComponent.transform.position, otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(serverComponent.transform.position, otherClientComponent.AuthoritativeState.Position);
}
public void AssertQuaternionsAreEquivalent(Quaternion a, Quaternion b)
{
var aAngles = a.eulerAngles;
var bAngles = b.eulerAngles;
Assert.AreEqual(aAngles.x, bAngles.x, 0.001, $"Quaternions were not equal. Expected: {a}, but was {b}");
Assert.AreEqual(aAngles.y, bAngles.y, 0.001, $"Quaternions were not equal. Expected: {a}, but was {b}");
Assert.AreEqual(aAngles.z, bAngles.z, 0.001, $"Quaternions were not equal. Expected: {a}, but was {b}");
}
public void AssertVectorsAreEquivalent(Vector3 a, Vector3 b)
{
Assert.AreEqual(a.x, b.x, 0.001, $"Vectors were not equal. Expected: {a}, but was {b}");
Assert.AreEqual(a.y, b.y, 0.001, $"Vectors were not equal. Expected: {a}, but was {b}");
Assert.AreEqual(a.z, b.z, 0.001, $"Vectors were not equal. Expected: {a}, but was {b}");
}
[Test]
public void WhenServerChangesSmoothValue_ValuesAreLerped()
{
var testComponent = GetTestComponent();
var otherClientComponent = GetOtherClientComponent();
testComponent.StaleDataHandling = StaleDataHandling.Ignore;
otherClientComponent.StaleDataHandling = StaleDataHandling.Ignore;
var serverComponent = GetServerComponent();
serverComponent.Interpolate = false;
testComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldSmooth = true;
otherClientComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldSmooth = true;
var startPosition = testComponent.transform.position;
var startScale = testComponent.transform.localScale;
var startRotation = testComponent.transform.rotation;
var anticipePosition = new Vector3(0, 1, 2);
var anticipeScale = new Vector3(1, 2, 3);
var anticipeRotation = Quaternion.LookRotation(new Vector3(2, 3, 4));
var serverSetPosition = new Vector3(3, 4, 5);
var serverSetScale = new Vector3(4, 5, 6);
var serverSetRotation = Quaternion.LookRotation(new Vector3(5, 6, 7));
testComponent.AnticipateMove(anticipePosition);
testComponent.AnticipateScale(anticipeScale);
testComponent.AnticipateRotate(anticipeRotation);
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
rpcComponent.MoveRpc(serverSetPosition);
rpcComponent.RotateRpc(serverSetRotation);
rpcComponent.ScaleRpc(serverSetScale);
WaitForMessagesReceivedWithTimeTravel(new List<Type>
{
typeof(RpcMessage),
typeof(RpcMessage),
typeof(RpcMessage),
}, new List<NetworkManager> { m_ServerNetworkManager });
WaitForMessageReceivedWithTimeTravel<NetworkTransformMessage>(m_ClientNetworkManagers.ToList());
var percentChanged = 1f / 60f;
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.transform.position);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.transform.localScale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.transform.rotation);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.AnticipatedState.Position);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.AnticipatedState.Scale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.AnticipatedState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AuthoritativeState.Position);
AssertVectorsAreEquivalent(serverSetScale, testComponent.AuthoritativeState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AuthoritativeState.Rotation);
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.transform.position);
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.transform.localScale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.transform.rotation);
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.AnticipatedState.Position);
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.AnticipatedState.Scale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.AnticipatedState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AuthoritativeState.Position);
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AuthoritativeState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AuthoritativeState.Rotation);
for (var i = 1; i < 60; ++i)
{
TimeTravel(1f / 60f, 1);
percentChanged = 1f / 60f * (i + 1);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.transform.position);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.transform.localScale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.transform.rotation);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.AnticipatedState.Position);
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.AnticipatedState.Scale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.AnticipatedState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AuthoritativeState.Position);
AssertVectorsAreEquivalent(serverSetScale, testComponent.AuthoritativeState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AuthoritativeState.Rotation);
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.transform.position);
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.transform.localScale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.transform.rotation);
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.AnticipatedState.Position);
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.AnticipatedState.Scale);
AssertQuaternionsAreEquivalent(Quaternion.Lerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.AnticipatedState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AuthoritativeState.Position);
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AuthoritativeState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AuthoritativeState.Rotation);
}
TimeTravel(1f / 60f, 1);
AssertVectorsAreEquivalent(serverSetPosition, testComponent.transform.position);
AssertVectorsAreEquivalent(serverSetScale, testComponent.transform.localScale);
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.transform.rotation);
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AnticipatedState.Position);
AssertVectorsAreEquivalent(serverSetScale, testComponent.AnticipatedState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AnticipatedState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AuthoritativeState.Position);
AssertVectorsAreEquivalent(serverSetScale, testComponent.AuthoritativeState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AuthoritativeState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.transform.position);
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.transform.localScale);
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.transform.rotation);
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AnticipatedState.Position);
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AnticipatedState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AnticipatedState.Rotation);
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AuthoritativeState.Position);
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AuthoritativeState.Scale);
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AuthoritativeState.Rotation);
}
[Test]
public void WhenServerChangesReanticipeValue_ValuesAreReanticiped()
{
var testComponent = GetTestComponent();
var otherClientComponent = GetOtherClientComponent();
testComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldMove = true;
otherClientComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldMove = true;
var serverComponent = GetServerComponent();
serverComponent.Interpolate = false;
serverComponent.transform.position = new Vector3(0, 1, 2);
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
rpcComponent.MoveRpc(new Vector3(0, 1, 2));
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
WaitForMessageReceivedWithTimeTravel<NetworkTransformMessage>(m_ClientNetworkManagers.ToList());
Assert.AreEqual(new Vector3(0, 6, 2), testComponent.transform.position);
Assert.AreEqual(new Vector3(0, 6, 2), testComponent.AnticipatedState.Position);
Assert.AreEqual(new Vector3(0, 1, 2), testComponent.AuthoritativeState.Position);
Assert.AreEqual(new Vector3(0, 6, 2), otherClientComponent.transform.position);
Assert.AreEqual(new Vector3(0, 6, 2), otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(new Vector3(0, 1, 2), otherClientComponent.AuthoritativeState.Position);
}
[Test]
public void WhenStaleDataArrivesToIgnoreVariable_ItIsIgnored([Values(10u, 30u, 60u)] uint tickRate, [Values(0u, 1u, 2u)] uint skipFrames)
{
m_ServerNetworkManager.NetworkConfig.TickRate = tickRate;
m_ServerNetworkManager.NetworkTickSystem.TickRate = tickRate;
for (var i = 0; i < skipFrames; ++i)
{
TimeTravel(1 / 60f, 1);
}
var serverComponent = GetServerComponent();
serverComponent.Interpolate = false;
var testComponent = GetTestComponent();
testComponent.StaleDataHandling = StaleDataHandling.Ignore;
testComponent.Interpolate = false;
var otherClientComponent = GetOtherClientComponent();
otherClientComponent.StaleDataHandling = StaleDataHandling.Ignore;
otherClientComponent.Interpolate = false;
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
rpcComponent.MoveRpc(new Vector3(1, 2, 3));
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
testComponent.AnticipateMove(new Vector3(0, 5, 0));
rpcComponent.MoveRpc(new Vector3(4, 5, 6));
// Depending on tick rate, one of these two things will happen.
// The assertions are different based on this... either the tick rate is slow enough that the second RPC is received
// before the next update and we move to 4, 5, 6, or the tick rate is fast enough that the next update is sent out
// before the RPC is received and we get the update for the move to 1, 2, 3. Both are valid, what we want to assert
// here is that the anticipated state never becomes 1, 2, 3.
WaitForConditionOrTimeOutWithTimeTravel(() => testComponent.AuthoritativeState.Position == new Vector3(1, 2, 3) || testComponent.AuthoritativeState.Position == new Vector3(4, 5, 6));
if (testComponent.AnticipatedState.Position == new Vector3(4, 5, 6))
{
// Anticiped client received this data for a time earlier than its anticipation, and should have prioritized the anticiped value
Assert.AreEqual(new Vector3(4, 5, 6), testComponent.transform.position);
Assert.AreEqual(new Vector3(4, 5, 6), testComponent.AnticipatedState.Position);
// However, the authoritative value still gets updated
Assert.AreEqual(new Vector3(4, 5, 6), testComponent.AuthoritativeState.Position);
// Other client got the server value and had made no anticipation, so it applies it to the anticiped value as well.
Assert.AreEqual(new Vector3(4, 5, 6), otherClientComponent.transform.position);
Assert.AreEqual(new Vector3(4, 5, 6), otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(new Vector3(4, 5, 6), otherClientComponent.AuthoritativeState.Position);
}
else
{
// Anticiped client received this data for a time earlier than its anticipation, and should have prioritized the anticiped value
Assert.AreEqual(new Vector3(0, 5, 0), testComponent.transform.position);
Assert.AreEqual(new Vector3(0, 5, 0), testComponent.AnticipatedState.Position);
// However, the authoritative value still gets updated
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AuthoritativeState.Position);
// Other client got the server value and had made no anticipation, so it applies it to the anticiped value as well.
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.transform.position);
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AuthoritativeState.Position);
}
}
[Test]
public void WhenNonStaleDataArrivesToIgnoreVariable_ItIsNotIgnored([Values(10u, 30u, 60u)] uint tickRate, [Values(0u, 1u, 2u)] uint skipFrames)
{
m_ServerNetworkManager.NetworkConfig.TickRate = tickRate;
m_ServerNetworkManager.NetworkTickSystem.TickRate = tickRate;
for (var i = 0; i < skipFrames; ++i)
{
TimeTravel(1 / 60f, 1);
}
var serverComponent = GetServerComponent();
serverComponent.Interpolate = false;
var testComponent = GetTestComponent();
testComponent.StaleDataHandling = StaleDataHandling.Ignore;
testComponent.Interpolate = false;
var otherClientComponent = GetOtherClientComponent();
otherClientComponent.StaleDataHandling = StaleDataHandling.Ignore;
otherClientComponent.Interpolate = false;
testComponent.AnticipateMove(new Vector3(0, 5, 0));
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
rpcComponent.MoveRpc(new Vector3(1, 2, 3));
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
WaitForConditionOrTimeOutWithTimeTravel(() => testComponent.AuthoritativeState.Position == serverComponent.transform.position && otherClientComponent.AuthoritativeState.Position == serverComponent.transform.position);
// Anticiped client received this data for a time earlier than its anticipation, and should have prioritized the anticiped value
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.transform.position);
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AnticipatedState.Position);
// However, the authoritative value still gets updated
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AuthoritativeState.Position);
// Other client got the server value and had made no anticipation, so it applies it to the anticiped value as well.
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.transform.position);
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AnticipatedState.Position);
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AuthoritativeState.Position);
}
}
}