Skip to content

Commit c6e4018

Browse files
committed
Fixed a numerical error in FeedbackController and simplified associated code.
1 parent e78eae1 commit c6e4018

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Assets/Samples/RebindingUI/Game/FeedbackController.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ namespace UnityEngine.InputSystem.Samples.RebindUI
99
/// </summary>
1010
public class FeedbackController : MonoBehaviour
1111
{
12+
private const float kDefaultOutputFrequency = 10.0f;
13+
private const float kDefaultOutputThrottleDelay = 1.0f / kDefaultOutputFrequency;
14+
1215
[Header("Color Output")]
1316
[Tooltip("The device color output frequency (Hz)")]
14-
public float colorOutputFrequency = 10.0f;
17+
public float colorOutputFrequency = kDefaultOutputFrequency;
1518

1619
[Header("Force Feedback Output")]
1720
[Tooltip("The device rumble output frequency (Hz)")]
18-
public float rumbleOutputFrequency = 10.0f;
21+
public float rumbleOutputFrequency = kDefaultOutputFrequency;
1922

2023
/// <summary>
2124
/// Gets or sets the target light color.
@@ -25,7 +28,11 @@ public class FeedbackController : MonoBehaviour
2528
/// <summary>
2629
/// Gets or sets the strength of the rumble effect [0, 1].
2730
/// </summary>
28-
public float rumble { get; set; }
31+
public float rumble
32+
{
33+
get => m_Rumble;
34+
set => m_Rumble = Mathf.Clamp01(value);
35+
}
2936

3037
/// <summary>
3138
/// Records the device used to trigger an action.
@@ -74,6 +81,7 @@ public void RecordRecentDeviceFromAction(InputAction action)
7481
private Color m_DeviceColor = NoLight;
7582
private double m_NextRumbleUpdateTime;
7683
private float m_DeviceRumble;
84+
private float m_Rumble;
7785

7886
private void Awake()
7987
{
@@ -110,7 +118,7 @@ private void Update()
110118
if (now >= m_NextLightUpdateTime && (m_InvalidateLight || m_DeviceColor != color))
111119
{
112120
m_InvalidateLight = false;
113-
m_NextLightUpdateTime = NextMultipleOf(now, 1.0f / colorOutputFrequency);
121+
m_NextLightUpdateTime = ComputeNextUpdateTime(now, colorOutputFrequency);
114122
ApplyLight(color);
115123
}
116124

@@ -121,7 +129,7 @@ private void Update()
121129
if (now >= m_NextRumbleUpdateTime && (m_InvalidateRumble || !Mathf.Approximately(m_DeviceRumble, rumble)))
122130
{
123131
m_InvalidateRumble = false;
124-
m_NextRumbleUpdateTime = NextMultipleOf(now, 1.0f / rumbleOutputFrequency);
132+
m_NextRumbleUpdateTime = ComputeNextUpdateTime(now, rumbleOutputFrequency);
125133
ApplyRumble(rumble);
126134
}
127135
}
@@ -186,7 +194,7 @@ private static bool IsRecentlyUsed(InputDevice device, double realtimeSinceStart
186194

187195
private static bool DetectAbandonedDevices(double realTimeSinceStartup)
188196
{
189-
var removed = false;
197+
bool removed;
190198
var foundAtLeastOnePassiveDevice = false;
191199
do
192200
{
@@ -210,9 +218,10 @@ private static bool DetectAbandonedDevices(double realTimeSinceStartup)
210218
return foundAtLeastOnePassiveDevice;
211219
}
212220

213-
private static double NextMultipleOf(double value, double factor)
221+
private static double ComputeNextUpdateTime(double now, float frequency)
214222
{
215-
return Math.Round((value / factor), MidpointRounding.AwayFromZero) * factor;
223+
var factor = frequency > 0.0 ? 1.0f / frequency : kDefaultOutputThrottleDelay;
224+
return Math.Ceiling(now / factor) * factor;
216225
}
217226
}
218227
}

0 commit comments

Comments
 (0)