Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Solved] Tracked dolly camera does not move if there is no aim target

Discussion in 'Cinemachine' started by legione0, Jul 9, 2018.

  1. legione0

    legione0

    Joined:
    Jul 9, 2018
    Posts:
    4
    Hello everyone,

    I want a virtual camera that simply follows the player along a path. Unfortunately, if I don't set an aim target the camera doesn't move, even if a follow target is set and auto dolly is enabled.

    I don't want the camera to rotate, just to immediately follow the player according to the direction he's going (therefore I'm not using a timeline). Did I miss some option?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,532
    It works for me without an aim target. Can you post a picture of your vcam inspector, and of your project hierarchy, showing both the vcam and the target?
     
  3. legione0

    legione0

    Joined:
    Jul 9, 2018
    Posts:
    4
    Here is the inspector hierarchy, it's pretty simple.
    project_hierarchy.png

    And here is the inspector of the dolly tracked camera.
    vcam_settings.png

    I also a made a short video about the issue:
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,532
    Thanks for the pics and the video. The video makes it clear. When you set the lookAt, it's not following, it's rotating.

    The problem is the relationship between the dolly track and the motion path of the player. The algorithm for auto-dolly is very simple: it just finds the point on the path that's closest to the target. That's what's happening. The closest point in your situation is the end of the path, until the target gets far enough away, then the camera starts to move.

    The solution is to make the dolly track more parallel to the player's trajectory.
     
  5. legione0

    legione0

    Joined:
    Jul 9, 2018
    Posts:
    4
    If the path is more parallel it does indeed work, but unfortunately I need to keep the dolly track as it is for aesthetic reasons, so I wrote a CinemachineExtension.

    For the ones who wanted to achieve the same effect below you can find the script.
    Keep in mind:
    • it works only with smooth paths;
    • you have to keep Auto-Dolly disabled;
    • you have to implement the logic for the other y and z axis (see the comments and sorry for my laziness);
    • if you're switching camera you have to copy paste eventual offsets of the previous camera for a seamless transition.
    @Gregoryl thank you, if you think the effect I want to achieve can be obtained with a simpler solution please let me (us) know!

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that follows a target despite how far the path is and in which direction the player is going. Auto-Dolly must be disabled in order to use this extension and it must be used a CinemachineSmoothPath.
    6. /// </summary>
    7. [ExecuteInEditMode]
    8. [SaveDuringPlay]
    9. [AddComponentMenu("")] // Hide in menu
    10. public class FollowTargetDollyXaxis : CinemachineExtension
    11. {
    12.    public enum ThresholdAxis { X, Y, Z }
    13.  
    14.    [Tooltip("It determines which axis to use for the threshold beyond which is activated the dolly. The threshold is calculated according to the position of the path first and last waypoint.")]
    15.    public ThresholdAxis thresholdAxis = ThresholdAxis.X;
    16.    [Tooltip("If the previous live camera has an offset put it here, consider only the thresholdAxis value")]
    17.    public float previousCameraOffset = 0;
    18.    [Tooltip("Boost the priority of the camera so to make it active.")]
    19.    public int priorittBoost = 1;
    20.  
    21.  
    22.    CinemachineTrackedDolly dolly;
    23.    float sequenceStart;
    24.    float sequenceEnd;
    25.    bool following = false;
    26.  
    27.    void Start()
    28.    {
    29.       CinemachineVirtualCamera vcam = GetComponent<CinemachineVirtualCamera>();
    30.       dolly = vcam.GetCinemachineComponent<CinemachineTrackedDolly>();
    31.  
    32.       CinemachineSmoothPath path = (CinemachineSmoothPath)dolly.m_Path;
    33.  
    34.       SetSequenceExtremes(path);
    35.    }
    36.  
    37.    protected override void PostPipelineStageCallback(
    38.        CinemachineVirtualCameraBase vcam,
    39.        CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    40.    {
    41.  
    42.       float targetPos = GetTargetPos(vcam);
    43.  
    44.       if (!following && targetPos >= sequenceStart)
    45.       {
    46.          following = true;
    47.          vcam.Priority += priorittBoost;
    48.          vcam.MoveToTopOfPrioritySubqueue();
    49.       }
    50.       else if (following && targetPos < sequenceStart)
    51.       {
    52.          following = false;
    53.          vcam.Priority -= priorittBoost;
    54.  
    55.       }
    56.  
    57.       if (following)
    58.       {
    59.  
    60.          dolly.m_PathPosition = Mathf.InverseLerp(sequenceStart, sequenceEnd, targetPos);
    61.       }
    62.    }
    63.  
    64.    private void SetSequenceExtremes(CinemachineSmoothPath p)
    65.    {
    66.       switch (thresholdAxis)
    67.       {
    68.          // #TODO IMPLEMENT OTHER AXIS HERE
    69.          case ThresholdAxis.X:
    70.             sequenceStart = p.m_Waypoints[0].position.x + gameObject.transform.position.x - previousCameraOffset;
    71.             sequenceEnd = p.m_Waypoints[p.m_Waypoints.Length - 1].position.x + gameObject.transform.position.x - previousCameraOffset;
    72.             break;
    73.       }
    74.    }
    75.  
    76.    private float GetTargetPos(CinemachineVirtualCameraBase vcam)
    77.    {
    78.       switch (thresholdAxis)
    79.       {
    80.          // #TODO IMPLEMENT OTHER AXIS ALSO HERE
    81.          case ThresholdAxis.X:
    82.             return vcam.Follow.position.x;
    83.          default:
    84.             Debug.LogError("No thresholdAxis selected! Returning 0");
    85.             return 0;
    86.       }
    87.    }
    88. }
    89.  
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,532
    I love it when people implement their own extensions :D
     
    legione0 likes this.
  7. legione0

    legione0

    Joined:
    Jul 9, 2018
    Posts:
    4
    I put a solved tag then ;)