Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Set limits for the position of the Cinemachine camera that follows the player

Discussion in 'Cinemachine' started by MarkMaa, Mar 6, 2021.

  1. MarkMaa

    MarkMaa

    Joined:
    Jan 20, 2020
    Posts:
    36
    I have a player object than can fall into the abyss. Player can move in any direction. Camera settings :



    Camera follows player along all axes. But I need that when the player falls into the abyss, the camera remains in its Y position. That is, I need to set the minimum Y position for the cinemachine camera.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    You can do that with a simple custom Cinemachine extension. Here is one that locks the vcam's Y position to a fixed value. You can modify it to implement a minimum Y 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.  
     
    flashframe, BrokenAardvark and Adham9 like this.
  3. punkrooks

    punkrooks

    Joined:
    Apr 15, 2016
    Posts:
    35

    Hi Gregoryl,
    How can we set the minimum and maximum y value of cinemachine.
    So that it only moves within a range say y1 - y2.

    I am facing an issue when player falls as we cannot see what is visible below, this might solve the issue
    https://stackoverflow.com/questions/69279505/unity2d-player-off-camera-on-falling-with-cinemachine
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    punkrooks likes this.