Search Unity

Cinemachine 2D Cameras won't transition

Discussion in 'Cinemachine' started by bloodwolftico, Aug 10, 2019.

  1. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    10
    Hi. So I was researching Cinemachine and wanted to create multiple rooms with their own Confiner colliders, and have multiple 2D Cameras that would transition between themselves every time the player leaves 1 room and goes into the next.

    I initially tried doing this with a script but that was not working, and I wasn't sure what to attach the script to.

    This is the code I was using:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Cinemachine;
    6.  
    7. public class CinemachineCameraController : MonoBehaviour
    8. {
    9.     public GameObject startcam;
    10.     public GameObject transitionCam;
    11.  
    12.     void Start()
    13.     {
    14.         startcam = GameObject.Find("CM Camera 1");
    15.         transitionCam = GameObject.Find("CM Camera 2");    
    16.     }
    17.  
    18.     private void OnTriggerEnter2D(Collider2D other)
    19.     {
    20.         if (other.CompareTag("Player"))
    21.         {        
    22.             startcam.SetActive(false);
    23.             transitionCam.SetActive(true);
    24.         }
    25.  
    26.     }
    27. }
    28.  
    I attached this to the Main Camera at first, but then tried attaching it to the Confiners. The 1st 2D Cam would disable on its own or the order of the cameras would always change.

    Then I did more research and was able to make the first camera transition into the 2nd camera, by just adjusting the priority. When I tried adding a 3rd camera and confiner, transitions stopped working. Now Cinemachine won't deactivate the first camera when I transition from the first room into the 2nd room.

    I don't know what I am doing wrong and would appreciate some help.
     

    Attached Files:

  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,725
    Set it up like this:
    Make your room confining boxes overlap a little. Attach a CinemachineTriggerAction script to each one. The TriggerAction script allows you activate and deactivate vcams when objects enter and leave the bounding shape. Set each one up to activate its vcam when the player enters, and to deactivate it when the player leaves.
     
    bloodwolftico likes this.
  3. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    10
    Hey Gregoryl! Thanks for the help!

    The good news is that camera transition is working again!

    The bad news is that the transition is still a little wonky, cameras swapping between themselves when they shouldn't, getting caught outside their confiners, and all cameras following me all the time.

    I've been trying to build the script and attach it to the confiners, but I am a little bit new to coding so I am not sure where exactly to add the CinemachineTriggerAction and whether I should use m_onObjectEnter / m_onObjectExit. I've been trying to find examples on the imported CM sample scenes or just in general but not a lot of luck.

    This is what I am currently using as code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CMCamController : MonoBehaviour
    7. {
    8.     //public GameObject startcam;
    9.     //public GameObject transitionCam;
    10.     public CinemachineVirtualCamera vcam;
    11.  
    12.     void Start()
    13.     {
    14.         //startcam = GameObject.Find("CM Camera 1");
    15.         //transitionCam = GameObject.Find("CM Camera 2");      
    16.         vcam = FindObjectOfType<CinemachineVirtualCamera>();
    17.     }
    18.  
    19.     private void OnTriggerEnter2D(Collider2D other)
    20.     {
    21.         if (other.CompareTag("Player"))
    22.         {
    23.             vcam.gameObject.SetActive(true);
    24.         }
    25.         else
    26.         {
    27.             vcam.gameObject.SetActive(false);
    28.         }
    29.      
    30.     }
    31. }
    This is a video where I show what is going on with the camera transition:

    Any help is appreciated.
     
  4. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    10
  5. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    10
    So I am running into similar problems that I ran into when applying the other solution I found on reddit.

    1- Camera transitions fine, but the game starts with the camer on Cam2 (cam1 gets disabled right off the bat).
    2- Is the camera transitions out of order (cam2 to cam4) the game forces the camera onto the next one (cam3).
    3- This keeps happening until I go into cam3 then onto cam4.
    4- After running the game, for some reason the Lens on some of the vcams change from Orthographic size to Field of view.

    https://imgur.com/PJrBVvI
     
  6. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    10
    UPDATE: the solution found on reddit works, so that's what I'll be using. Thanks!
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,725
  8. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    10
    Oh.. I didn't know that hehe, thanks! I'll take that in consideration next time.