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 158f26b913 com.unity.netcode.gameobjects@1.9.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.9.1] - 2024-04-18

### Added
- Added AnticipatedNetworkVariable<T>, which adds support for client anticipation of NetworkVariable values, allowing for more responsive gameplay (#2820)
- Added AnticipatedNetworkTransform, which adds support for client anticipation of NetworkTransforms (#2820)
- Added NetworkVariableBase.ExceedsDirtinessThreshold to allow network variables to throttle updates by only sending updates when the difference between the current and previous values exceeds a threshold. (This is exposed in NetworkVariable<T> with the callback NetworkVariable<T>.CheckExceedsDirtinessThreshold) (#2820)
- Added NetworkVariableUpdateTraits, which add additional throttling support: MinSecondsBetweenUpdates will prevent the NetworkVariable from sending updates more often than the specified time period (even if it exceeds the dirtiness threshold), while MaxSecondsBetweenUpdates will force a dirty NetworkVariable to send an update after the specified time period even if it has not yet exceeded the dirtiness threshold. (#2820)
- Added virtual method NetworkVariableBase.OnInitialize() which can be used by NetworkVariable subclasses to add initialization code (#2820)
- Added virtual method NetworkVariableBase.Update(), which is called once per frame to support behaviors such as interpolation between an anticipated value and an authoritative one. (#2820)
- Added NetworkTime.TickWithPartial, which represents the current tick as a double that includes the fractional/partial tick value. (#2820)
- Added NetworkTickSystem.AnticipationTick, which can be helpful with implementation of client anticipation. This value represents the tick the current local client was at at the beginning of the most recent network round trip, which enables it to correlate server update ticks with the client tick that may have triggered them. (#2820)
- `NetworkVariable` now includes built-in support for `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, and `Dictionary` (#2813)
- `NetworkVariable` now includes delta compression for collection values (`NativeList`, `NativeArray`, `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, `Dictionary`, and `FixedString` types) to save bandwidth by only sending the values that changed. (Note: For `NativeList`, `NativeArray`, and `List`, this algorithm works differently than that used in `NetworkList`. This algorithm will use less bandwidth for "set" and "add" operations, but `NetworkList` is more bandwidth-efficient if you are performing frequent "insert" operations.) (#2813)
- `UserNetworkVariableSerialization` now has optional callbacks for `WriteDelta` and `ReadDelta`. If both are provided, they will be used for all serialization operations on NetworkVariables of that type except for the first one for each client. If either is missing, the existing `Write` and `Read` will always be used. (#2813)
- Network variables wrapping `INetworkSerializable` types can perform delta serialization by setting `UserNetworkVariableSerialization<T>.WriteDelta` and `UserNetworkVariableSerialization<T>.ReadDelta` for those types. The built-in `INetworkSerializable` serializer will continue to be used for all other serialization operations, but if those callbacks are set, it will call into them on all but the initial serialization to perform delta serialization. (This could be useful if you have a large struct where most values do not change regularly and you want to send only the fields that did change.) (#2813)

### Fixed

- Fixed issue where NetworkTransformEditor would throw and exception if you excluded the physics package. (#2871)
- Fixed issue where `NetworkTransform` could not properly synchronize its base position when using half float precision. (#2845)
- Fixed issue where the host was not invoking `OnClientDisconnectCallback` for its own local client when internally shutting down. (#2822)
- Fixed issue where NetworkTransform could potentially attempt to "unregister" a named message prior to it being registered. (#2807)
- Fixed issue where in-scene placed `NetworkObject`s with complex nested children `NetworkObject`s (more than one child in depth) would not synchronize properly if WorldPositionStays was set to true. (#2796)

### Changed

- Changed `NetworkObjectReference` and `NetworkBehaviourReference` to allow null references when constructing and serializing. (#2874)
- Changed `NetworkAnimator` no longer requires the `Animator` component to exist on the same `GameObject`. (#2872)
- Changed `NetworkTransform` to now use `NetworkTransformMessage` as opposed to named messages for NetworkTransformState updates. (#2810)
- Changed `CustomMessageManager` so it no longer attempts to register or "unregister" a null or empty string and will log an error if this condition occurs. (#2807)
2024-04-18 00:00:00 +00:00

522 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
{
public 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));
}
}
}
}
public 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.Slerp(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.Slerp(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.Slerp(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.Slerp(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.Slerp(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.Slerp(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.Slerp(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.Slerp(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);
}
}
}