Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. 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

Using 3D confiners for a 2D game

Discussion in 'Cinemachine' started by CheMBurN, Mar 1, 2021.

  1. CheMBurN

    CheMBurN

    Joined:
    Dec 13, 2020
    Posts:
    4
    I'm working on a 2D platformer project and instead of parallaxing scripts I was just using the perspective camera.

    For confining I implemented 3D box collider confiners on a specific plane on the Z-axis.

    Is this an acceptable use? Would I run across any problems later on due to this implementation?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,828
    That will work. It might be a little more efficient to do it with a custom extension on the vcam, such as this one:
    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.  
     
  3. CheMBurN

    CheMBurN

    Joined:
    Dec 13, 2020
    Posts:
    4
    Excellent! I'll implement this right away and have a look. Thanks lots!
     
    Gregoryl likes this.
unityunity