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

How do I limit vcam depth movement (z) without using Confiner?

Discussion in 'Cinemachine' started by rockyboar, Mar 9, 2020.

  1. rockyboar

    rockyboar

    Joined:
    Feb 10, 2019
    Posts:
    9
    I have a 3d game project with a fixed camera perspective; (imagine 2.5d game or a beatem up game where you can move in the z-axis). When I move in the z-axis, using Cinemachine vcam with framing transposer, the camera follows. I want the camera to stop moving at a certain point when I move to close in on the screen (negative z).

    The reason I don't want to use a Confiner for this is because from what I understand, the 3D colliders are limited in shape. What I mean by this is I am actually using the Confiner now but only for x and y axis confinement because I need ithe confiner to be an irregular shape and the Polygon Collider 2D does this but I think there's nothing like this in 3D.

    So I was thinking maybe there are other ways to limit or confine the z-axis movement of vcam. Can someone guide me on how do I go about to achieve this? I'm thinking to still use my current Confiner but only x and y confinement and then some custom code to confine the z-axis but I don't know how I can achieve this.
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You could try something like this.
    When the Z value of your vcam is getting close to 0 (or any value), then you start increasing the Camera Distance property of your FramingTransposer.
     
  3. rockyboar

    rockyboar

    Joined:
    Feb 10, 2019
    Posts:
    9
    How do I do that? I tried something like this:


    Code (CSharp):
    1. void Start()
    2.     {
    3.         _vcam = GetComponent<CinemachineVirtualCamera>();
    4.         _framingTransposer = GetComponent<CinemachineFramingTransposer>();
    5.     }
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         if (_vcam.transform.position.z < -10f)
    11.         {
    12.             //_vcam.transform.position = new Vector3(_vcam.transform.position.x, _vcam.transform.position.y, -10f);
    13.             _framingTransposer.m_CameraDistance = 10;
    14.         }
    15.     }
    But it throws a null reference exception. Also, is it possible to just stop the vcam from moving?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,268
    The best way would be to write a custom extension. Here is one that locks the camera on the Z axis. Easy to modify it so that it sets a boundary instead of locking to a fixed value.

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