Skip to content

Commit 2aa2741

Browse files
committed
FreeLook: delegate orbital's full X-axis, nut just value
1 parent 1bd3061 commit 2aa2741

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

Base/Runtime/Behaviours/CinemachineFreeLook.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,6 @@ override public void UpdateCameraState(Vector3 worldUp, float deltaTime)
274274
if (activeCam)
275275
m_YAxis.Update(deltaTime);
276276

277-
// Top rig is the master axis controller
278-
if (mOrbitals[0] != null)
279-
m_XAxis.Value = mOrbitals[0].m_XAxis.Value;
280-
if (m_BindingMode == CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp)
281-
m_XAxis.Value = 0;
282-
283277
PushSettingsToRigs();
284278

285279
//UnityEngine.Profiling.Profiler.EndSample();
@@ -495,7 +489,10 @@ private int LocateExistingRigs(string[] rigNames, bool forceOrbital)
495489
if (mOrbitals[i] != null)
496490
{
497491
mOrbitals[i].m_HeadingIsSlave = true;
498-
mOrbitals[i].PreUpdateXAxisValueOverride = () => { return m_XAxis.Value; };
492+
if (i == 0)
493+
mOrbitals[i].HeadingUpdater
494+
= (CinemachineOrbitalTransposer orbital, float deltaTime, Vector3 up)
495+
=> { return orbital.UpdateHeading(deltaTime, up, ref m_XAxis); };
499496
m_Rigs[i] = vcam;
500497
++rigsFound;
501498
}

Base/Runtime/Components/CinemachineOrbitalTransposer.cs

+35-26
Original file line numberDiff line numberDiff line change
@@ -191,41 +191,49 @@ protected override void OnValidate()
191191
[HideInInspector, NoSaveDuringPlay]
192192
public bool m_HeadingIsSlave = false;
193193

194-
internal delegate float GetXAxisValue();
195-
internal GetXAxisValue PreUpdateXAxisValueOverride;
194+
/// <summary>
195+
/// Delegate that allows the the m_XAxis object to be replaced with another one.
196+
/// </summary>
197+
internal delegate float UpdateHeadingDelegate(
198+
CinemachineOrbitalTransposer orbital, float deltaTime, Vector3 up);
199+
200+
/// <summary>
201+
/// Delegate that allows the the XAxis object to be replaced with another one.
202+
/// To use it, just call orbital.UpdateHeading() with a reference to a
203+
/// private AxisState object, and that AxisState object will be updated and
204+
/// used to calculate the heading.
205+
/// </summary>
206+
internal UpdateHeadingDelegate HeadingUpdater
207+
= (CinemachineOrbitalTransposer orbital, float deltaTime, Vector3 up)
208+
=> { return orbital.UpdateHeading(deltaTime, up, ref orbital.m_XAxis); };
196209

197210
/// <summary>
198-
/// When in slave mode, this should be called once and only
199-
/// once every hrame to update the heading. When not in slave mode, this is called automatically.
211+
/// Update the X axis and calculate the heading. This can be called by a delegate
212+
/// with a custom axis.
213+
/// <param name="deltaTime">Used for damping. If less than 0, no damping is done.</param>
214+
/// <param name="up">World Up, set by the CinemachineBrain</param>
215+
/// <param name="axis"></param>
216+
/// <returns>Axis value</returns>
200217
/// </summary>
201-
public void UpdateHeading(float deltaTime, Vector3 up)
218+
public float UpdateHeading(float deltaTime, Vector3 up, ref AxisState axis)
202219
{
203220
// Only read joystick when game is playing
204221
if (deltaTime >= 0 || CinemachineCore.Instance.IsLive(VirtualCamera))
205222
{
206223
bool xAxisInput = false;
207-
if (PreUpdateXAxisValueOverride != null)
208-
{
209-
float value = PreUpdateXAxisValueOverride();
210-
if (value != m_XAxis.Value)
211-
{
212-
xAxisInput = true;
213-
m_XAxis.Value = value;
214-
}
215-
}
216-
xAxisInput |= m_XAxis.Update(deltaTime);
224+
xAxisInput |= axis.Update(deltaTime);
217225
if (xAxisInput)
218226
{
219227
mLastHeadingAxisInputTime = Time.time;
220228
mHeadingRecenteringVelocity = 0;
221229
}
222230
}
223-
float targetHeading = GetTargetHeading(m_XAxis.Value, GetReferenceOrientation(up), deltaTime);
231+
float targetHeading = GetTargetHeading(axis.Value, GetReferenceOrientation(up), deltaTime);
224232
if (deltaTime < 0)
225233
{
226234
mHeadingRecenteringVelocity = 0;
227235
if (m_RecenterToTargetHeading.m_enabled)
228-
m_XAxis.Value = targetHeading;
236+
axis.Value = targetHeading;
229237
}
230238
else
231239
{
@@ -237,14 +245,14 @@ public void UpdateHeading(float deltaTime, Vector3 up)
237245
// Scale value determined heuristically, to account for accel/decel
238246
float recenterTime = m_RecenterToTargetHeading.m_RecenteringTime / 3f;
239247
if (recenterTime <= deltaTime)
240-
m_XAxis.Value = targetHeading;
248+
axis.Value = targetHeading;
241249
else
242250
{
243-
float headingError = Mathf.DeltaAngle(m_XAxis.Value, targetHeading);
251+
float headingError = Mathf.DeltaAngle(axis.Value, targetHeading);
244252
float absHeadingError = Mathf.Abs(headingError);
245253
if (absHeadingError < UnityVectorExtensions.Epsilon)
246254
{
247-
m_XAxis.Value = targetHeading;
255+
axis.Value = targetHeading;
248256
mHeadingRecenteringVelocity = 0;
249257
}
250258
else
@@ -256,12 +264,16 @@ public void UpdateHeading(float deltaTime, Vector3 up)
256264
float accel = desiredVelocity - mHeadingRecenteringVelocity;
257265
if ((desiredVelocity < 0 && accel < 0) || (desiredVelocity > 0 && accel > 0))
258266
desiredVelocity = mHeadingRecenteringVelocity + desiredVelocity * scale;
259-
m_XAxis.Value += desiredVelocity;
267+
axis.Value += desiredVelocity;
260268
mHeadingRecenteringVelocity = desiredVelocity;
261269
}
262270
}
263271
}
264272
}
273+
float finalHeading = axis.Value;
274+
if (m_BindingMode == BindingMode.SimpleFollowWithWorldUp)
275+
axis.Value = 0;
276+
return finalHeading;
265277
}
266278

267279
private void OnEnable()
@@ -296,17 +308,14 @@ public override void MutateCameraState(ref CameraState curState, float deltaTime
296308
mLastTargetPosition = (PreviousTarget == null) ? Vector3.zero : PreviousTarget.position;
297309
mHeadingTracker = null;
298310
}
299-
UpdateHeading(deltaTime, curState.ReferenceUp);
311+
float heading = HeadingUpdater(this, deltaTime, curState.ReferenceUp);
300312

301313
if (IsValid)
302314
{
303315
mLastTargetPosition = FollowTarget.position;
304316

305317
// Calculate the heading
306-
float heading = m_XAxis.Value;
307-
if (m_BindingMode == BindingMode.SimpleFollowWithWorldUp)
308-
m_XAxis.Value = 0;
309-
else
318+
if (m_BindingMode != BindingMode.SimpleFollowWithWorldUp)
310319
heading += m_Heading.m_HeadingBias;
311320
Quaternion headingRot = Quaternion.AngleAxis(heading, curState.ReferenceUp);
312321

0 commit comments

Comments
 (0)