Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need Help Using Cinemachine in my 2D project!

Discussion in 'Cinemachine' started by namanchanduka01, May 22, 2020.

  1. namanchanduka01

    namanchanduka01

    Joined:
    Apr 7, 2020
    Posts:
    6
    So I saw a couple of videos on youtube which suggest that Cinemachine is the best way to control one or multiple cameras in your game. So I started my 2D sidescroller project. Camera movement involved with this project is as follows:-
    1) Cameras are stationary, as in there is no requirement of following the player.
    2) I want to setup multiple cameras(think of them like photo frames, and switch between them as and when the player moves into the range of new camera)
    3) *Optional* Since I care about the performance, it would be great if all the other gameobjects in the view of a camera are disabled if that camera isnt the currently active one.

    Imagine the player moving right and exits the camera, enters the space of the 2nd camera and the 1st cam gets disabled and 2nd gets enabled.

    I am having trouble with cinemachine as Im new to gamedev and unity. Please if anyone could help me set this up, I would be really thankful!
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hi,

    Create a Virtual Camera. Set your Look At target to your player (this will be needed if you are going to use Clear Shot Camera, because it will decide if the Look At target is visible or not). Set the Body and Aim to Do Nothing. See the DoNothingVcam image.
    After that's done, you need to manually position your virtual cameras. You may use the Solo button to make the current camera active in the game view. This will help a lot to find the best position for each camera.;)

    There are several ways to achieve this. A simple solution would be with ClearShot. Have a look at our examples scenes (when you import cinemachine from the package manager, there is an option to import our examples package - see the Samples image).
    With ClearShot, you could just set up your cameras one after the other. ClearShot will choose the camera that has the best shot of your player. Set your priorities to be the same, so that ClearShot won't change the currently active camera, if the player is not visible to any camera. See ClearShotSetup image.


    Depending on your scene, you could do a few things.
    You could group your gameObjects that belong to a camera. For example, create an empty gameObject groupView1, and put your objects visible to camera1 into this group. See the ObjectGroups image.
    If you are using ClearShot Camera, then check which camera is active, then enable the corresponding group and disable the rest. To check for the currently active camera, you can ask CinemachineBrain for the active virtual camera. CinemachineBrain is created on the main camera for you.
    Code (CSharp):
    1. CinemachineBrain CmBrain = Camera.main.GetComponent<CinemachineBrain>();
    2. var currentVcam = CmBrain.ActiveVirtualCamera;
    3. var currentVcamName = currentVcam.Name;
    I would go with the Clear Shot Camera setup, because this way you don't need to set up states and trigger volumes. Much more simple and clean.

    Note, you may want to enable neighbouring groups of objects, because there might be overlap in the camera views.
    Note2, have a look at Renderer.IsVisible feature (https://docs.unity3d.com/ScriptRefe...5.2021848930.1589894939-1207821455.1572890629). With this, you can check if an object is visible or not.

    Let us know, if you something's not clear. ;)
     

    Attached Files:

    Last edited: May 22, 2020
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    That's a good suggestion. Here's another idea, if you don't want to incur the cost of a ClearShot:

    Create a trigger shape and a deactivated virtual camera for each room. Add the CinemachineTriggerAction behaviour to the trigger object, and set it up to activate the virtual camera when the player enters the room deactivate it when the player leaves. If the trigger zones overlap just a little, then there will always be an active virtual camera.
     
    RemDust and gaborkb like this.