Search Unity

Stop Cinemachine from Following Player on the Y-Axis

Discussion in 'Cinemachine' started by QuindecimX, Mar 8, 2022.

  1. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    I'm using Cinemachine to create a top-down perspective game where the camera moves based on player position instead of the traditional method of manual camera control.

    I'm using the "Framing Transposer" setting in the Body tab (to allow me to set Dead Zone size, Soft Zone size, and so on), and set the Aim tab to "Do nothing" because the camera moves most satisfactory to me that way. Problem is when my character moves, the camera will move up and down on the Y-axis upon moving towards the Dead Zone.

    I just want the camera to move only on the X and Z axes. I couldn't find any setting to lock movement to certain axes. The only workaround I could find is setting Dead Zone height to zero. But doing that compromises the camera set-up I want for my game. So I'm guessing I may have to resort to scripting at this point.

    Any idea how to go about this?

    Screenshot 2022-03-07 202224.png
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Try this custom extension:

    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 Y co-ordinate
    6. /// </summary>
    7. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class LockCameraY : CinemachineExtension
    9. {
    10.     [Tooltip("Lock the camera's Y position to this value")]
    11.     public float m_YPosition = 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.Finalize)
    18.         {
    19.             var pos = state.RawPosition;
    20.             pos.y = m_YPosition;
    21.             state.RawPosition = pos;
    22.         }
    23.     }
    24. }
    25.  
     
  3. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    Hi Gregoryl. I created a C# file trying out the code you wrote. Unfortunately, I'm running into errors with it. I think the main problem causing all the errors is that Unity or Cinemachine does not want me inheriting from Cinemachine's Extension class in custom made script files for whatever reason.
    unity c# error list 1.png unity c# error list 2.png
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Looks like the Cinemachine namespace is not visible to your project. Do you have a local asmdef file? If so, you need to add Cinemachine to it.
     
  5. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    Apologies, but I'm not familiar with that file name. Where would I find it?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    upload_2022-3-11_14-39-51.png

    If you have one of those somewhere in your assets, it groups all the scripts in its folder and descendants into an assembly definition, a kind of box that isolates it from the rest of the c# world. You'll need to add entries there to any assemblies (outside the Unity built-in) referenced by those scripts.

    upload_2022-3-11_14-43-11.png

    In this case you'll have to add com.unity.cinemachine
     
  7. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    Ok first, I tried creating a new Assembly Definition and adding in the Reference. Unfortunately it ended up creating more errors not just for the one script, but for all my other ones too. So I deleted the asmdef file I created.

    Next, I went looking in my own file explorer for these asmdef files (since Unity's file search isn't the best), and found a good number of them within my project. I cross-referenced the specific Cinemachine name you mentioned to find the exact name in my Unity project. And here's what I found:

    upload_2022-3-11_15-49-52.png upload_2022-3-11_15-50-41.png

    Is this the correct asmdef file I should be looking for? When I try clicking on the "Open" button (in the top right of the second picture) so I can add the assembly reference, it opens up Visual Studios instead.

    upload_2022-3-11_15-57-41.png
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    No! You need to look for asmdef files in your Assets folder, not in your Packages folder. It's possible that there are none. If that's the case, don't add any because it's not the problem.

    What happens if you create, as a test, a new project then add Cinemachine to it, then add my script?
     
  9. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    After making a new project, I don't see any asmdef files in the Assets folder. However, there are suddenly no errors now. And in Visual Studio, everything is fine. I can even open the Cinemachine Extension class in the my new Project file. The new project is also using the same Unity version as my project too. I can't figure out how it's working here, but doesn't in my project.
    Here's the comparison:

    upload_2022-3-11_17-18-10.png upload_2022-3-11_17-19-4.png
     
  10. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    You know what, I figured it out. I checked the Cinemachine version in my package manager, and saw that there were higher versions available to update to (Unity didn't let me know for some reason). So I updated it, and in doing so it seemed to re-install/re-download whatever was missing because the script now works correctly!
    No more errors, and the script works perfectly within Unity when testing out the camera! Thank you!
     
    Gregoryl likes this.