Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Setting Cinemachine Vcam to fixed Y postion

Discussion in 'Getting Started' started by unity_Axmmp6YxjVaB1A, Mar 16, 2021.

  1. unity_Axmmp6YxjVaB1A

    unity_Axmmp6YxjVaB1A

    Joined:
    Mar 16, 2021
    Posts:
    2
    I was following a tutorial on youtube to remake angry birds and it used Cinemachine Camera to follow the player and enemies. However when it zooms out, the camera pans super low on the Y axis and looks like this.
    upload_2021-3-15_19-43-8.png

    I would much rather have it pan upwards. than to go beneath the ground like this. I tried writing a script to keep the position locked at 0 but it doesn't work. I used Debug.Log() to check that my code is running and it is.
    upload_2021-3-15_19-44-58.png

    Basically how can I keep the camera from going below a Y of 0.
    Thank you
     

    Attached Files:

  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,238
    You can write a custom CM extension for the vcam. Here is one that locks the camera Y to a fixed position. You can modify it to implement a minimum value instead.

    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. unity_Axmmp6YxjVaB1A

    unity_Axmmp6YxjVaB1A

    Joined:
    Mar 16, 2021
    Posts:
    2
    Thanks for the help I'll try it out!