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. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Follow only along a certain axis

Discussion in 'Cinemachine' started by Devilly, Aug 9, 2018.

  1. Devilly

    Devilly

    Joined:
    Dec 5, 2017
    Posts:
    14
    I have a Cinemachine Virtual Camera which follows the player nicely according to the Follow property and the settings in the Body. Now what I'd like is to make the cinemachine follow only along a certain axis. I'm assuming there is a setting for this?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,637
    There is no specific setting for that, but it's very easy to write an extension that constrains the camera's position. Here is an example that constrains it to a plane at constant Z. You can modify it to constrain the camera position to a line. Just drop the file into your assets, and it will be available in the vcam's "Extensions" dropdown.
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that locks the camera's Z co-ordinate
    6. /// </summary>
    7. [ExecuteInEditMode] [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class LockCameraZ : CinemachineExtension
    9. {
    10.     [Tooltip("Lock the camera's Z position to this value")]
    11.     public float m_ZPosition = 10;
    12.  
    13.     protected override void PostPipelineStageCallback(
    14.         CinemachineVirtualCameraBase vcam,
    15.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    16.     {
    17.         if (stage == CinemachineCore.Stage.Body)
    18.         {
    19.             var pos = state.RawPosition;
    20.             pos.z = m_ZPosition;
    21.             state.RawPosition = pos;
    22.         }
    23.     }
    24. }
    25.  
     
    Devilly likes this.
  3. Devilly

    Devilly

    Joined:
    Dec 5, 2017
    Posts:
    14
    That's working nicely. Thank you!
     
  4. Devilly

    Devilly

    Joined:
    Dec 5, 2017
    Posts:
    14
    There is one thing, though. Implementing it this way causes blending between two camera's to be somewhat hard, doesn't it? What I want is several y values on which the player is tracked. If the player moves from being the closest to value A to being the closest to value B the camera should gradually move to the other y value. Is this doable with Cinemachine or do I have to write such code myself?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,637
    I don't see why the blending should be hard. The camera will blend from one track to the other when you activate the new vcam. You can set the blend style in the Brain.
     
  6. Devilly

    Devilly

    Joined:
    Dec 5, 2017
    Posts:
    14
    I had this blending set up and it worked fine. Now, when setting the y value of the cameras in the extension class it jumps straight to the y value of the activated camera.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,637
    Blending should still work. Can you show me inspectors and code?
     
  8. Devilly

    Devilly

    Joined:
    Dec 5, 2017
    Posts:
    14
    I was investigating this stuff based on your comment. I applied quite some changes in a small time frame to our project. It seems the problem lies in one of the other changes. Once I get to the bottom of this I'll let you know if everything works as you're suggesting. Thanks. :)
     
    Gregoryl likes this.
  9. Devilly

    Devilly

    Joined:
    Dec 5, 2017
    Posts:
    14
    After some fiddling around I found out the cause for the missing blend was I destroyed the virtual camera a blend started with too early.

    So you've helped me perfectly. Thank you!
     
    Gregoryl likes this.