-
Notifications
You must be signed in to change notification settings - Fork 461
fix: Use Time.unscaledDeltaTime instead of Time.deltaTime as time step #2187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5846a3d
fix: Use Time.unscaledDeltaTime instead of Time.deltaTime as time ste…
PitouGames db9f862
Merge remote-tracking branch 'pitou/fix/time-scale' into fix/unscaled…
jeffreyrainy c6ddc8e
style: removing unrelated stylistic differences. Fixing brackets plac…
jeffreyrainy 1d5b644
test: test adjustments
jeffreyrainy 1de0b39
test: test adjustments
jeffreyrainy 1ccaebd
Merge branch 'fix/unscaled-deltatime-2183' of github.com:Unity-Techno…
jeffreyrainy 7c7bb53
test: Applying PR review suggestion of saving the time scale
jeffreyrainy 45b1627
Merge branch 'develop' into fix/unscaled-deltatime-2183
jeffreyrainy 5c0cc93
test: Applying PR review suggestion of saving the time scale, second …
jeffreyrainy 8f872fd
Merge branch 'fix/unscaled-deltatime-2183' of github.com:Unity-Techno…
jeffreyrainy 02a222e
Reverted changes to AddNetworkPrefabTests and fixed instabilities cau…
ShadauxCat 106df9a
reverting unwanted whitespace change
jeffreyrainy f8dfa62
reducing the test cases to 0.0f, 1.0f and 2.0f timescales
jeffreyrainy fc02d04
Revert "Reverted changes to AddNetworkPrefabTests and fixed instabili…
jeffreyrainy 0c68342
Merge branch 'develop' into fix/unscaled-deltatime-2183
jeffreyrainy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| using System.Collections; | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
| using UnityEngine.Assertions.Comparers; | ||
| using UnityEngine.TestTools; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
|
|
||
|
|
@@ -11,25 +12,45 @@ namespace Unity.Netcode.RuntimeTests | |
| /// </summary> | ||
| public class NetworkTimeSystemTests | ||
| { | ||
| private MonoBehaviourTest<PlayerLoopTimeTestComponent> m_MonoBehaviourTest; // cache for teardown | ||
| private MonoBehaviourTest<PlayerLoopFixedTimeTestComponent> m_PlayerLoopFixedTimeTestComponent; // cache for teardown | ||
| private MonoBehaviourTest<PlayerLoopTimeTestComponent> m_PlayerLoopTimeTestComponent; // cache for teardown | ||
|
|
||
| private float m_OriginalTimeScale = 1.0f; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| m_OriginalTimeScale = Time.timeScale; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
|
|
||
| // Create, instantiate, and host | ||
| Assert.IsTrue(NetworkManagerHelper.StartNetworkManager(out _)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests whether time is accessible and has correct values inside Update/FixedUpdate. | ||
| /// This test applies only when <see cref="Time.timeScale"> is 1. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [UnityTest] | ||
| public IEnumerator PlayerLoopFixedTimeTest() | ||
| { | ||
| m_PlayerLoopFixedTimeTestComponent = new MonoBehaviourTest<PlayerLoopFixedTimeTestComponent>(); | ||
|
|
||
| yield return m_PlayerLoopFixedTimeTestComponent; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests whether time is accessible and has correct values inside Update, for multiples <see cref="Time.timeScale"/> values. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [UnityTest] | ||
| public IEnumerator PlayerLoopTimeTest() | ||
| public IEnumerator PlayerLoopTimeTest_WithDifferentTimeScale([Values(0.0f, 0.1f, 0.5f, 1.0f, 2.0f, 5.0f)] float timeScale) | ||
| { | ||
| m_MonoBehaviourTest = new MonoBehaviourTest<PlayerLoopTimeTestComponent>(); | ||
| Time.timeScale = timeScale; | ||
|
|
||
| m_PlayerLoopTimeTestComponent = new MonoBehaviourTest<PlayerLoopTimeTestComponent>(); | ||
|
|
||
| yield return m_MonoBehaviourTest; | ||
| yield return m_PlayerLoopTimeTestComponent; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -40,10 +61,10 @@ public IEnumerator PlayerLoopTimeTest() | |
| [UnityTest] | ||
| public IEnumerator CorrectAmountTicksTest() | ||
| { | ||
| var tickSystem = NetworkManager.Singleton.NetworkTickSystem; | ||
| var delta = tickSystem.LocalTime.FixedDeltaTime; | ||
| var previous_localTickCalculated = 0; | ||
| var previous_serverTickCalculated = 0; | ||
| NetworkTickSystem tickSystem = NetworkManager.Singleton.NetworkTickSystem; | ||
| float delta = tickSystem.LocalTime.FixedDeltaTime; | ||
| int previous_localTickCalculated = 0; | ||
| int previous_serverTickCalculated = 0; | ||
|
|
||
| while (tickSystem.LocalTime.Time < 3f) | ||
| { | ||
|
|
@@ -70,7 +91,7 @@ public IEnumerator CorrectAmountTicksTest() | |
|
|
||
| Assert.AreEqual(previous_localTickCalculated, NetworkManager.Singleton.LocalTime.Tick, $"Calculated local tick {previous_localTickCalculated} does not match local tick {NetworkManager.Singleton.LocalTime.Tick}!"); | ||
| Assert.AreEqual(previous_serverTickCalculated, NetworkManager.Singleton.ServerTime.Tick, $"Calculated server tick {previous_serverTickCalculated} does not match server tick {NetworkManager.Singleton.ServerTime.Tick}!"); | ||
| Assert.True(Mathf.Approximately((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time), $"Local time {(float)NetworkManager.Singleton.LocalTime.Time} is not approximately server time {(float)NetworkManager.Singleton.ServerTime.Time}!"); | ||
| Assert.AreEqual((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time, $"Local time {(float)NetworkManager.Singleton.LocalTime.Time} is not approximately server time {(float)NetworkManager.Singleton.ServerTime.Time}!", FloatComparer.s_ComparerWithDefaultTolerance); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -80,15 +101,23 @@ public void TearDown() | |
| // Stop, shutdown, and destroy | ||
| NetworkManagerHelper.ShutdownNetworkManager(); | ||
|
|
||
| if (m_MonoBehaviourTest != null) | ||
| Time.timeScale = m_OriginalTimeScale; | ||
|
|
||
| if (m_PlayerLoopFixedTimeTestComponent != null) | ||
| { | ||
| Object.DestroyImmediate(m_MonoBehaviourTest.gameObject); | ||
| Object.DestroyImmediate(m_PlayerLoopFixedTimeTestComponent.gameObject); | ||
| m_PlayerLoopFixedTimeTestComponent = null; | ||
| } | ||
| } | ||
|
|
||
| if (m_PlayerLoopTimeTestComponent != null) | ||
| { | ||
| Object.DestroyImmediate(m_PlayerLoopTimeTestComponent.gameObject); | ||
| m_PlayerLoopTimeTestComponent = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class PlayerLoopTimeTestComponent : MonoBehaviour, IMonoBehaviourTest | ||
| public class PlayerLoopFixedTimeTestComponent : MonoBehaviour, IMonoBehaviourTest | ||
| { | ||
| public const int Passes = 100; | ||
|
|
||
|
|
@@ -101,7 +130,7 @@ public class PlayerLoopTimeTestComponent : MonoBehaviour, IMonoBehaviourTest | |
| private NetworkTime m_ServerTimePreviousUpdate; | ||
| private NetworkTime m_LocalTimePreviousFixedUpdate; | ||
|
|
||
| public void Start() | ||
| private void Start() | ||
| { | ||
| // Run fixed update at same rate as network tick | ||
| Time.fixedDeltaTime = NetworkManager.Singleton.LocalTime.FixedDeltaTime; | ||
|
|
@@ -110,23 +139,23 @@ public void Start() | |
| Time.maximumDeltaTime = float.MaxValue; | ||
| } | ||
|
|
||
| public void Update() | ||
| private void Update() | ||
| { | ||
| // This must run first else it wont run if there is an exception | ||
| m_UpdatePasses++; | ||
|
|
||
| var localTime = NetworkManager.Singleton.LocalTime; | ||
| var serverTime = NetworkManager.Singleton.ServerTime; | ||
| NetworkTime localTime = NetworkManager.Singleton.LocalTime; | ||
| NetworkTime serverTime = NetworkManager.Singleton.ServerTime; | ||
|
|
||
| // time should have advanced on the host/server | ||
| Assert.True(m_LocalTimePreviousUpdate.Time < localTime.Time); | ||
| Assert.True(m_ServerTimePreviousUpdate.Time < serverTime.Time); | ||
| Assert.Less(m_LocalTimePreviousUpdate.Time, localTime.Time); | ||
| Assert.Less(m_ServerTimePreviousUpdate.Time, serverTime.Time); | ||
|
|
||
| // time should be further then last fixed step in update | ||
| Assert.True(m_LocalTimePreviousFixedUpdate.FixedTime < localTime.Time); | ||
| Assert.Less(m_LocalTimePreviousFixedUpdate.FixedTime, localTime.Time); | ||
|
|
||
| // we should be in same or further tick then fixed update | ||
| Assert.True(m_LocalTimePreviousFixedUpdate.Tick <= localTime.Tick); | ||
| Assert.LessOrEqual(m_LocalTimePreviousFixedUpdate.Tick, localTime.Tick); | ||
|
|
||
| // fixed update should result in same amounts of tick as network time | ||
| if (m_TickOffset == -1) | ||
|
|
@@ -135,23 +164,61 @@ public void Update() | |
| } | ||
| else | ||
| { | ||
| // offset of 1 is ok, this happens due to different tick duration offsets | ||
| Assert.True(Mathf.Abs(serverTime.Tick - m_TickOffset - m_LastFixedUpdateTick) <= 1); | ||
| // offset of 1 is ok, this happens due to different tick duration offsets | ||
| Assert.LessOrEqual(Mathf.Abs(serverTime.Tick - m_TickOffset - m_LastFixedUpdateTick), 1); | ||
| } | ||
|
|
||
| m_LocalTimePreviousUpdate = localTime; | ||
| m_ServerTimePreviousUpdate = serverTime; | ||
| } | ||
|
|
||
| public void FixedUpdate() | ||
| private void FixedUpdate() | ||
| { | ||
| var time = NetworkManager.Singleton.LocalTime; | ||
| m_LocalTimePreviousFixedUpdate = NetworkManager.Singleton.LocalTime; | ||
|
|
||
| m_LocalTimePreviousFixedUpdate = time; | ||
| Assert.AreEqual(Time.fixedDeltaTime, m_LocalTimePreviousFixedUpdate.FixedDeltaTime); | ||
| Assert.AreEqual((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time, null, FloatComparer.s_ComparerWithDefaultTolerance); | ||
| m_LastFixedUpdateTick++; | ||
| } | ||
|
|
||
| Assert.AreEqual(Time.fixedDeltaTime, time.FixedDeltaTime); | ||
| Assert.True(Mathf.Approximately((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time)); | ||
| public bool IsTestFinished => m_UpdatePasses >= Passes; | ||
| } | ||
|
|
||
| m_LastFixedUpdateTick++; | ||
| public class PlayerLoopTimeTestComponent : MonoBehaviour, IMonoBehaviourTest | ||
| { | ||
| public const int Passes = 100; | ||
|
|
||
| private int m_UpdatePasses = 0; | ||
|
|
||
| private NetworkTime m_LocalTimePreviousUpdate; | ||
| private NetworkTime m_ServerTimePreviousUpdate; | ||
| private NetworkTime m_LocalTimePreviousFixedUpdate; | ||
|
|
||
| private void Update() | ||
| { | ||
| // This must run first else it wont run if there is an exception | ||
| m_UpdatePasses++; | ||
|
|
||
| NetworkTime localTime = NetworkManager.Singleton.LocalTime; | ||
| NetworkTime serverTime = NetworkManager.Singleton.ServerTime; | ||
|
|
||
| // time should have advanced on the host/server | ||
| Assert.Less(m_LocalTimePreviousUpdate.Time, localTime.Time); | ||
| Assert.Less(m_ServerTimePreviousUpdate.Time, serverTime.Time); | ||
|
|
||
| // time should be further then last fixed step in update | ||
| Assert.Less(m_LocalTimePreviousFixedUpdate.FixedTime, localTime.Time); | ||
|
|
||
| // we should be in same or further tick then fixed update | ||
| Assert.LessOrEqual(m_LocalTimePreviousFixedUpdate.Tick, localTime.Tick); | ||
|
|
||
| m_LocalTimePreviousUpdate = localTime; | ||
| m_ServerTimePreviousUpdate = serverTime; | ||
| } | ||
|
|
||
| private void FixedUpdate() | ||
| { | ||
| m_LocalTimePreviousFixedUpdate = NetworkManager.Singleton.LocalTime; | ||
| } | ||
|
|
||
| public bool IsTestFinished => m_UpdatePasses >= Passes; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.