Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to use Cinemachine from C# code (Dolly path)?

Discussion in 'Cinemachine' started by YanaRedMachine, Sep 21, 2018.

  1. YanaRedMachine

    YanaRedMachine

    Joined:
    Sep 21, 2018
    Posts:
    3
    I need in runtime to set the path, and move the camera along it - with acceleration path the start and deceleration path the end. But I cannot find tutorial with examples how to work with Cinemachine from C# code.

    This code makes camera to follow the character and keep it into center of view.

    Code (CSharp):
    1. public class CameraScript : MonoBehaviour {
    2.     public CinemachineVirtualCamera virtualCameraPrefab;
    3.     public Transform tPlayer;
    4.     public Transform tFollowTarget;
    5.  
    6.     private Camera camera;
    7.     private CinemachineVirtualCamera virtualCamera;
    8.  
    9.     void Start () {
    10.         camera = Camera.main;
    11.         virtualCamera = UnityEngine.Object.Instantiate(virtualCameraPrefab);
    12.         var brain = camera.gameObject.AddComponent<Cinemachine.CinemachineBrain>();
    13.         virtualCamera.transform.position = camera.transform.position;
    14.  
    15.         virtualCamera.Follow = tFollowTarget;
    16.         virtualCamera.LookAt = tPlayer;
    17.     }
    18. }
    19.  
    But I cannot find out how to make the camera follow the Dolly Path. I have tried this, but it doesn't work (of course, I have no idea how to attach DollyCart to virtualCamera or brains...):

    Code (CSharp):
    1. public class CameraScript : MonoBehaviour {
    2.     public CinemachineVirtualCamera virtualCameraPrefab;
    3.     public Transform tPlayer;
    4.     public Transform tFollowTarget;
    5.     public Transform[] tWaypoints;
    6.  
    7.     private Camera camera;
    8.     private CinemachineVirtualCamera virtualCamera;
    9.  
    10.     void Start () {
    11.         camera = Camera.main;
    12.         virtualCamera = UnityEngine.Object.Instantiate(virtualCameraPrefab);
    13.         var brain = camera.gameObject.AddComponent<Cinemachine.CinemachineBrain>();
    14.         virtualCamera.transform.position = camera.transform.position;
    15.  
    16.         virtualCamera.LookAt = tPlayer;
    17.  
    18.         CinemachinePath path = new CinemachinePath();
    19.         CinemachinePath.Waypoint[] waypoints = new CinemachinePath.Waypoint[tWaypoints.Length];
    20.         for (int i = 0; i < tWaypoints.Length; i++) {
    21.             waypoints[i] = new CinemachinePath.Waypoint();
    22.             waypoints[i].position = tWaypoints[i].position;
    23.        }
    24.         path.m_Waypoints = waypoints;
    25.  
    26.         CinemachineDollyCart cart = new CinemachineDollyCart();
    27.         cart.m_Path = path;
    28.     }
    29. }
    30.  
    Could you please help me with moving virtualCamera along the Dolly path. Please, give me link to the tutorial with examples how to use Cinemachine from C# code.
     
  2. YanaRedMachine

    YanaRedMachine

    Joined:
    Sep 21, 2018
    Posts:
    3
    How to add waypoints to the CinemachineSmoothPath structure? This code does not works...
    Code (CSharp):
    1. public Transform[] tWaypoints;
    2. ...
    3. CinemachineSmoothPath path = new GameObject("DollyTrack").AddComponent<CinemachineSmoothPath>();
    4. path.m_Waypoints = new CinemachineSmoothPath.Waypoint[tWaypoints.Length];
    5. for (int i = 0; i < tWaypoints.Length; i++) {
    6.     path.m_Waypoints[i] = new CinemachineSmoothPath.Waypoint();
    7.     path.m_Waypoints[i].position = tWaypoints[i].position;
    8. }
     
    onercorr likes this.
  3. YanaRedMachine

    YanaRedMachine

    Joined:
    Sep 21, 2018
    Posts:
    3
    Solved:
    Code (CSharp):
    1.         camera = Camera.main;
    2.         virtualCamera = UnityEngine.Object.Instantiate(virtualCameraPrefab);
    3.         var brain = camera.gameObject.AddComponent<Cinemachine.CinemachineBrain>();
    4.         virtualCamera.transform.position = camera.transform.position;
    5.  
    6.         virtualCamera.LookAt = tPlayer;
    7.  
    8.         CinemachineTrackedDolly trackedDolly = virtualCamera.AddCinemachineComponent<CinemachineTrackedDolly>();
    9.  
    10.         CinemachineSmoothPath smoothPath = new GameObject("DollyTrack").AddComponent<CinemachineSmoothPath>();
    11.         smoothPath.m_Waypoints = new CinemachineSmoothPath.Waypoint[tWaypoints.Length];
    12.         for (int i = 0; i < tWaypoints.Length; i++) {
    13.             smoothPath.m_Waypoints[i] = new CinemachineSmoothPath.Waypoint();
    14.             smoothPath.m_Waypoints[i].position = tWaypoints[i].position;
    15.         }
    16.  
    17.         trackedDolly.m_Path = smoothPath;
    18.         trackedDolly.m_PathPosition = 0;