Search Unity

Moving Camera via script along Dolly Track

Discussion in 'Cinemachine' started by AntLewis, Apr 12, 2020.

  1. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Hi there, I'm using the dolly track cam and (unsurprisingly as the track is a loop) having issues with the auto dolly functionality. Ideally, I'd like to build additional functionality into the autodolly code (for example never moving backward the waypoints list, either staying static or moving forward through the waypoints).

    However, I'm unsure how the current autodolly code calculates where it should be on the track, based on the target's position. Could you point me in the right direction of this code, or show me examples or other scripts that manually move the camera along the dolly track?

    Thanks for any help!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,723
    The AutoDolly feature just finds the closest point to the target on the path, and tries to put the camera there. The code is in CinemachineTrackedDolly.cs, and it looks like this:
    Code (CSharp):
    1.             // Get the new ideal path base position
    2.             if (m_AutoDolly.m_Enabled && FollowTarget != null)
    3.             {
    4.                 float prevPos = m_Path.ToNativePathUnits(m_PreviousPathPosition, m_PositionUnits);
    5.                 // This works in path units
    6.                 m_PathPosition = m_Path.FindClosestPoint(
    7.                     FollowTargetPosition,
    8.                     Mathf.FloorToInt(prevPos),
    9.                     (deltaTime < 0 || m_AutoDolly.m_SearchRadius <= 0)
    10.                         ? -1 : m_AutoDolly.m_SearchRadius,
    11.                     m_AutoDolly.m_SearchResolution);
    12.                 m_PathPosition = m_Path.FromPathNativeUnits(m_PathPosition, m_PositionUnits);
    13.  
    14.                 // Apply the path position offset
    15.                 m_PathPosition += m_AutoDolly.m_PositionOffset;
    16.             }
    You could write a custom script to follow a path (see CinemachineDollyCart.cs for an example). You can attach that script to an invisible object and have the vcam track that, or you could put DoNothing in the vcam's body and attach your script directly to the vcam to control its position based on your custom path-following algorithm.
     
  3. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Perfect, thankyou !