Setting Cinemachine Vcam to fixed Y postion

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.

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.

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

6938671--815407--upload_2021-3-15_19-41-54.png

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.

using UnityEngine;
using Cinemachine;

/// <summary>
/// An add-on module for Cinemachine Virtual Camera that locks the camera's Y co-ordinate
/// </summary>
[SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
public class LockCameraY : CinemachineExtension
{
    [Tooltip("Lock the camera's Y position to this value")]
    public float m_YPosition = 10;

    protected override void PostPipelineStageCallback(
        CinemachineVirtualCameraBase vcam,
        CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    {
        if (stage == CinemachineCore.Stage.Finalize)
        {
            var pos = state.RawPosition;
            pos.y = m_YPosition;
            state.RawPosition = pos;
        }
    }
}
2 Likes

Thanks for the help I’ll try it out!