Skip to content

Commit 0db1753

Browse files
authored
CMCL-1706: Added InputAxisControllerBase.GetController() and InputAxisControllerBase.TriggerRecentering() (#1072)
* Added InputAxisControllerBase.GetController() and InputAxisControllerBase.TriggerRecentering * add missing XML doc
1 parent bd74da5 commit 0db1753

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

com.unity.cinemachine/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414

1515
### Added
1616
- Added `CinemachineConfiner2D.CameraWasDisplaced()` and `CinemachineConfiner2D.GetCameraDisplacementDistance()` methods.
17+
- Added `InputAxisControllerBase.GetController()` method, to conveniently fetch an Input Controller having a specific name.
18+
- Added `InputAxisControllerBase.TriggerRecentering()` to trigger recentering of an axis having a specific name.
1719

1820

1921
## [3.1.4] - 2025-06-10

com.unity.cinemachine/Runtime/Core/InputAxis.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ public void UpdateRecentering(float deltaTime, bool forceCancel, float center)
303303
}
304304
}
305305

306-
/// <summary>Trigger re-centering immediately, regardless of whether re-centering
307-
/// is enabled or the wait time has elapsed.</summary>
306+
/// <summary>Trigger recentering immediately, regardless of whether recentering
307+
/// is enabled or the wait time has elapsed. Note that Recentering will be automatically canceled
308+
/// if any change to the input value is detected.</summary>
308309
public void TriggerRecentering() => m_RecenteringState.m_ForceRecenter = true;
309310

310311
/// <summary>Cancel any current re-centering in progress, and reset the wait time</summary>
@@ -398,5 +399,16 @@ public void Reset(ref InputAxis axis)
398399
m_CurrentSpeed = 0;
399400
axis.Reset();
400401
}
402+
403+
/// <summary>
404+
/// Cancel any current input, resetting the speed to zero. This will eliminate any
405+
/// residual acceleration or deceleration of the input value.
406+
/// </summary>
407+
/// <param name="axis">The axis in question</param>
408+
public void CancelCurrentInput(ref InputAxis axis)
409+
{
410+
m_CurrentSpeed = 0;
411+
axis.SetValueAndLastValue(axis.Value);
412+
}
401413
}
402414
}

com.unity.cinemachine/Runtime/Core/InputAxisControllerBase.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,47 @@ public void UpdateControllers(UnityEngine.Object context, float deltaTime)
195195
c.Driver.ProcessInput(ref m_Axes[i].DrivenAxis(), c.InputValue, deltaTime);
196196
}
197197
}
198-
}
199198

199+
int GetControllerIndex(string axisName)
200+
{
201+
for (int i = 0; i < Controllers.Count; ++i)
202+
{
203+
var c = Controllers[i];
204+
if (c.Name == axisName)
205+
return i;
206+
}
207+
return -1;
208+
}
209+
210+
/// <summary>
211+
/// Get the controller for a given axis name. The axis name is the name displayed
212+
/// for the axis foldout on the inspector.
213+
/// </summary>
214+
/// <param name="axisName">The name of the axis, as it appears in the inspector.</param>
215+
/// <returns>The first Controller object with the matching axis name, or null if not found.</returns>
216+
public InputAxisControllerBase<T>.Controller GetController(string axisName)
217+
{
218+
int i = GetControllerIndex(axisName);
219+
return i < 0 ? null : Controllers[i];
220+
}
221+
222+
/// <summary>
223+
/// Triggers recentering for a given axis, and also cancels any input currently in progrress for that axis.
224+
/// </summary>
225+
/// <param name="axisName">The name of the axis, as it appears in the inspector.</param>
226+
/// <returns>True if the axis was found and recentering triggered, false otherwise</returns>
227+
public bool TriggerRecentering(string axisName)
228+
{
229+
int i = GetControllerIndex(axisName);
230+
if (i >= 0)
231+
{
232+
var c = Controllers[i];
233+
c.Driver.CancelCurrentInput(ref m_Axes[i].DrivenAxis());
234+
m_Axes[i].DrivenAxis().TriggerRecentering();
235+
}
236+
return i >= 0;
237+
}
238+
}
200239

201240
/// <summary>
202241
/// This is a base class for a behaviour that is used to drive IInputAxisOwner behaviours,
@@ -331,6 +370,22 @@ protected void UpdateControllers(float deltaTime)
331370

332371
m_ControllerManager.UpdateControllers(this, deltaTime);
333372
}
373+
374+
/// <summary>
375+
/// Get the controller for a given axis name. The axis name is the name displayed
376+
/// for the axis foldout on the inspector.
377+
/// </summary>
378+
/// <param name="axisName">The name of the axis, as it appears in the inspector.</param>
379+
/// <returns>The first Controller object with the matching axis name, or null if not found.</returns>
380+
public Controller GetController(string axisName) => m_ControllerManager.GetController(axisName);
381+
382+
/// <summary>
383+
/// Triggers recentering for a given axis, and also cancels any input currently in progress for that axis.
384+
/// This ensures that the recentering begins immediately.
385+
/// </summary>
386+
/// <param name="axisName">The name of the axis, as it appears in the inspector.</param>
387+
/// <returns>True if the axis was found and recentering triggered, false otherwise</returns>
388+
public bool TriggerRecentering(string axisName) => m_ControllerManager.TriggerRecentering(axisName);
334389
}
335390
}
336391

0 commit comments

Comments
 (0)