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

Question Can you make the Cinemachine Virtual Camera Aim along a path?

Discussion in 'Cinemachine' started by Sapien_, Sep 22, 2022.

  1. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    I'm trying to parent a gameobject to a cinema virtual camera to move along a path while following another gameobject (the player). However, I can't find an option to have the cinemachine camera aim at the path. The only thing I could find is changing the "Camera Up" to "Path" in the Body section, but that doesn't actually change the rotation of the virtual camera. So the child of the Virtual camera, isn't rotating.

    Is there a way to make so the virtual camera is aiming at the direction of the path its moving along?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can put both the vcam and the gameobject as children of a CinemachineDollyCart, which can follow the player while remaining on and rotating with the path.
     
  3. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    I'm not sure I follow. When I make the Vcam a child of the Dolly, it as well as the child does not rotate along the path when following the player.
     
    Last edited: Sep 22, 2022
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Can you show the inspectors of the path, DollyCart, and vcam please? Also please show the hierarchy.
     
  5. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    upload_2022-9-23_15-40-52.png

    I know camera up is set to default, but I did that because although on the camera window it seems to be rotating, its rotation actually isn't changing
     
    Last edited: Sep 23, 2022
  6. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    I made a child of the Vcam so that I could see where its pointing. In this image. You can see that it is following the "Follow Target" object (The Cube). But it is not pointing along the path.

    upload_2022-9-23_15-51-31.png
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Change the vcam's "Body" setting to "Do Nothing", so that it inherits its transform from the DollyCart parent. Then, you'll need a script to make the cart follow the target.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Another option is to make a simple CM extension to make the vcam's orientation come from the track. Let me know if you want to go that route, and I can help you out with it.
     
  9. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    It depends which 1 is easiest. The programming for these I was doing in playmaker. If the extension is better, I don't mind going that route.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Extension is easiest for sure. Here is one that pulls the rotation from the path. Add it to a vcam with TrackdDolly in the Body, and Do Nothing in the Aim. If you drop this script into your project, it will automatically appear in the vcam's "Add Extension" dropdown.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An custom extension for Cinemachine Virtual Camera that pulls rotation from the path
    6. /// </summary>
    7. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class RotationFromDollyTrack : CinemachineExtension
    9. {
    10.     protected override void PostPipelineStageCallback(
    11.         CinemachineVirtualCameraBase vcam,
    12.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    13.     {
    14.         if (stage == CinemachineCore.Stage.Body)
    15.         {
    16.             var dollyVcam = vcam as CinemachineVirtualCamera;
    17.             if (dollyVcam != null)
    18.             {
    19.                 var dolly = dollyVcam.GetCinemachineComponent<CinemachineTrackedDolly>();
    20.                 var path = dolly == null ? null : dolly.m_Path;
    21.                 if (path != null)
    22.                 {
    23.                     state.RawOrientation = path.EvaluateOrientationAtUnit(dolly.m_PathPosition, dolly.m_PositionUnits);
    24.                 }
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    upload_2022-9-23_13-50-24.png
     
    Sapien_ likes this.
  11. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    Right. Cool, I'll try this out
     
    Gregoryl likes this.
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Let me know how it works out for you
     
    Sapien_ likes this.
  13. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    Hey man. This worked out perfectly! May have been more practical to use the dolly cart and make a follow script, but I like the control I have over the VCam. Thanks a lot @Gregoryl
     
    Gregoryl likes this.