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

Lock an Axis in 2d VCam Follow Mode

Discussion in 'Cinemachine' started by Voodoochief, Feb 8, 2018.

  1. Voodoochief

    Voodoochief

    Joined:
    Dec 6, 2014
    Posts:
    8
    Hi,

    I can not find an answer to this in these forums or in the documentation so here goes.

    I have a 2D scene and at times I really want to Lock the Y axis component so as my character moves around he is only followed in the X axis. I tried to enlarge the Y axis Dead Zone to more than 1.0 so my character could walk off screen, but that did not work.

    I have a confiner, but I would need to turn that on and off, which I do not want to do.

    So How do I make the Follow camera follow my character only in the X, so my character can walk off screen?

    Thanks
    Rob
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You could make a vcam extension that locks the camera's Y position. Enable and disable it as needed. Here is an example of one that locks the camera's Z position. Just make the appropriate mods.
    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 (enabled && stage == CinemachineCore.Stage.Body)
    18.         {
    19.             var pos = state.RawPosition;
    20.             pos.z = m_ZPosition;
    21.             state.RawPosition = pos;
    22.         }
    23.     }
    24. }
    25.  
     
  3. Voodoochief

    Voodoochief

    Joined:
    Dec 6, 2014
    Posts:
    8
    Thanks that is a good idea, I think I will change what I came up with last night to this.

    I created an extra Object and each frame I copy the characters position into it with or without the Y as needed. I then follow this object to solve my issue.

    Fast answer, thanks again.
    Rob
     
  4. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    Um, using Unity 2020.1.1f1 and Cinemachine 2.6.0 ... with the Transposer Body this works, doesn't seem to work with the Framing Transposer though ... any way to make that happen?

    edit - using a perspective camera, 2.5d game
     
    Last edited: Aug 11, 2020
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Yes, it's a due to a bug in CM 2.6.0. We have fixed it for 2.6.1 (soon to be in preview). In the meantime, as a temporary fix, you can change line 17 of the script above to read
    if (enabled && stage == CinemachineCore.Stage.Finalize)
     
  6. nathanbmnt

    nathanbmnt

    Joined:
    Mar 9, 2015
    Posts:
    2
    I need to lock the x axis of my camera. The script Gregoryl gave works until I reload my scene with
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    and then the x axis of the camera follows my player but is reversed. Very odd.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Can you submit a bug report for this?
     
  8. nathanbmnt

    nathanbmnt

    Joined:
    Mar 9, 2015
    Posts:
    2
    Ok I've submitted the report. Now the issue is happening even when the scene has not been restarted.

    I had a CinemachineConfiner script extension on my virtual camera too, if I disable that your custom extension works fine. It seems they don't play nicely.
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Oh, that's new information. Did you try changing line 17 of the script to
    if (enabled && stage == CinemachineCore.Stage.Finalize)
    as mentioned in the other post?
     
  10. chrissmits

    chrissmits

    Joined:
    Jan 30, 2020
    Posts:
    1
    Sorry for the necropost. Thanks for the example cinemachine extension code, here is my take on it, someone might be interested in a script that is suitable for every axis:

    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 Axis co-ordinate
    6. /// </summary>
    7. [ExecuteInEditMode]
    8. [SaveDuringPlay]
    9. [AddComponentMenu("")] // Hide in menu
    10. public class LockCameraAxis : CinemachineExtension
    11. {
    12.     enum AxisToLock
    13.     {
    14.         X,
    15.         Y,
    16.         Z
    17.     };
    18.  
    19.     [SerializeField]
    20.     private AxisToLock _lockedAxis;
    21.  
    22.     [SerializeField, Tooltip("Lock the camera's Axis position to this value")]
    23.     private float _lockedAxisPosition = 10;
    24.  
    25.     protected override void PostPipelineStageCallback(
    26.         CinemachineVirtualCameraBase vcam,
    27.         CinemachineCore.Stage stage,
    28.         ref CameraState state,
    29.         float deltaTime
    30.     )
    31.     {
    32.         if (enabled && stage == CinemachineCore.Stage.Body)
    33.         {
    34.             var pos = state.RawPosition;
    35.             switch (_lockedAxis)
    36.             {
    37.                 case AxisToLock.X:
    38.                     pos.x = _lockedAxisPosition;
    39.                     break;
    40.                 case AxisToLock.Y:
    41.                     pos.y = _lockedAxisPosition;
    42.                     break;
    43.                 case AxisToLock.Z:
    44.                     pos.z = _lockedAxisPosition;
    45.                     break;
    46.             }
    47.             state.RawPosition = pos;
    48.         }
    49.     }
    50. }
    51.  
     
    naumovgrigorij62 and Christor_8 like this.