Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Cinemachine camera which only moves when target goes into soft bounds

Discussion in 'Cinemachine' started by CoBraLorD, Jul 26, 2020.

  1. CoBraLorD


    Feb 3, 2020
    Hi, I'd like to have a virtual camera which only follows the target whenever the target reaches the softzone. Within the deadzone the target can move however it wants without the camera transform changing.

    It's a 3d setup (my plane is in the xz space).

    I'd tried setting this up with a framing transposer, but that changes Y coordinate to maintain the distance to the target and the z depth delta also moves the coordinates within the dead zone.

    I've been looking into creating a custom extension for this, but I was wondering if there is an easier way to achieve this kind of behaviour?
  2. Gregoryl


    Unity Technologies

    Dec 22, 2016
    There is nothing out of the box to make that work perfectly, but you can come pretty close using a vcam with FramingTransposer in the Body, and DoNothing in the Aim. You need to set the FramingTransposer's Dead Zone Depth to an appropriate size.

    You have to play around with the settings, depending on the desired camera-target relationship. Don't expect perfection (due in part to perspective), but you can get it to be pretty nice.

    I set it up with my vcam looking down at the target from a 20 degree angle, and these settings in the Framing Transposer:


    You need to add a custom extension to lock the Y position. Here is what I used:

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    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;
    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. }
  3. CoBraLorD


    Feb 3, 2020
    Thank you for the feedback!