Search Unity

Problem with implementing Cinemachine into scene that has pre-existing zoom trigger function.

Discussion in 'Cinemachine' started by segasega89, Aug 18, 2019.

  1. segasega89

    segasega89

    Joined:
    Feb 10, 2019
    Posts:
    5
    Hi there,

    I'm a beginner to Unity and I'm trying to make a 2D platform game at the moment where I hope to implement both a "zoom on trigger" function and Cinemachine into the gameplay.
    I'm working on a scene where the player crosses a trigger(in this case a rectangular quad) and the orthographic camera zooms in on the scene. I've got this function working properly yet once I add Cinemachine into the scene the zoom function stops functioning completely. I'm assuming that the reason why this code stops working when I add Cinemachine to the project is that I need to reference the virtual camera rather than the main camera in the code of the "Zoom In" function?
    Here is the code I have for the the ZoomIn function shown below:


    void ZoomIn(){

    if(Camera.main.orthographicSize > 8f)
    {

    Camera.main.orthographicSize =
    Mathf.Lerp (
    Camera.main.orthographicSize,
    Camera.main.orthographicSize + -increment,
    timeLerp * Time.deltaTime);


    }else if(Camera.main.orthographicSize < 5f){
    {
    shouldZoomIn = false;
    }

    }




    Could anybody help me on how to alter the code on this function so that I can reference the vcam rather than the main camera? I'm really not sure how to go about it. I've found a thread on how to reference the virtual cam here:https://forum.unity.com/threads/how...change-virtual-cams-orthographic-size.499491/
    It suggests putting the current camera and the Cinemachine Brain into variables and set the orthographic size the virtual camera by querying the Cinemachine Brain variable:

    1. var camera = Camera.main;
    2. var brain = (camera == null) ? null : camera.GetComponent<CinemachineBrain>();
    3. var vcam = (brain == null) ? null : brain.AciveVirtualCamera as CinemachineVirtualCamera;
    4. if (vcam != null)
    5. vcam.blablabla

    However when I try to implement the code found in this thread I get errors. In particular the "camera.GetComponent<CinemachineBrain>" part.



    Any helpful information would be greatly appreciated!

    Kind regards
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,845
    I'm not sure what errors you get with the camera.GetComponent<CinemachineBrain> part. It should work.

    However, with Cinemachine it's much easier to get what you're looking for, and you don't need any code at all. Here is how to do it:

    In addition to your primary vcam, create a second vcam for use inside the trigger, with the zoom set the way you want it. Set that vcam to the same priority as the primary vcam, and deactivate its gameObject. Then, add a CinemachineTriggerAction script to your trigger, and in the OnObjectEnter section activate your special vcam, and in the OnObjectExit section deactivate it. The CM brain will automatically take care of making a smooth transition.
     
  3. segasega89

    segasega89

    Joined:
    Feb 10, 2019
    Posts:
    5
    Hi again,

    Thanks very much for the helpful reply.

    So I've created a second 2D Vcam that's called "CM vcam2" and I've set its priority to the same number as my CM vcam1( which has a priority of 10). I've also set the orthographic size of the CM vcam2 to a much smaller number compared to my CM vcam1 in order to create the zoom effect. I've also disabled the CM vcam2 as you've instructed.

    I'm just trying to figure out what code I need to write to activate "CM vcam2" when the player touches the trigger(in this case a rectangular quad). I've created a "CinemachineZoom" script which I've attached to the player and in the script I have written an "OnTriggerEnter2D" function which I assume will allow me to activate the CM vcam2(which will create the smooth blending effect between the two cameras when the player touches the quad).

    However, I don't really understand how to refer to the "CM vcam2" in the "OnTriggerEnter2D" function.
    Here's the code I've written in the "CinemachineZoom" script down below:

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col){
    2.            
    3.                vcam2.gameObject.SetActive();
    4.                  
    5.         }//end of OnTriggerEnter2D
    An error occurs because the "vcam2.gameObject.SetActive();" line is incorrect. Must I put the CM vcam2 into a variable beforehand?

    Any help would be very much appreciated.

    Kind regards,
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,845
    You don't need to write a custom script. Add the handy CinemachineTriggerAction script to your trigger collider, and drag the vcam in question into the "Target" fields, as indicated:

    upload_2019-8-27_13-38-4.png

    Then, when Player enters the trigger vcam2 will be activated, and it will be deactivated when Player leaves the trigger.
     
  5. segasega89

    segasega89

    Joined:
    Feb 10, 2019
    Posts:
    5
    I can't seem to find the "CinemachineTriggerAction" script that you've mentioned when I try to add it via the add component function to my quad(which is acting as a trigger collider).

    Cinemachine Screenshot.png
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,845
    What version of CM are you using?
     
  7. segasega89

    segasega89

    Joined:
    Feb 10, 2019
    Posts:
    5
    I've attached a screenshot of the pop up box that appears when I click on "about" underneath the Cinemachine tab in Unity. I believe the version I have downloaded is 2.1.10 if I'm not mistaken.
    Also the version of Unity that I have is "Unity 2019.2.1f1 Personal".

    Whenever I download Cinemachine for Unity I always click on the Asset Store tab and search for Cinemachine through the "Search for assets" input box. I just download Cinemachine through this method.

    My PC does not have a graphics card, it just uses the Intel HD graphics that's built into my Intel i5 2500 CPU. Could it be possible that the reason why the Cinemachine Trigger Action script doesn't appear is because I don't have a GPU that will be sufficiently powerful for certain Cinemachine functions?

    Cinemachine Version.png
     
    Last edited: Aug 28, 2019
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,845
    You are using a very old version of Cinemachine. Newer versions are delivered using the Unity Package Manager.

    Here is what you need to do:

    1. Make a backup of your project.
    2. Open an empty scene
    3. Delete Cinemachine from your assets
    4. Open the package manager and install the latest version of Cinemachine

    upload_2019-8-28_8-43-39.png

    You need to do it in that order (deleting the old CM is important) in order for the upgrade to happen correctly.
     
  9. segasega89

    segasega89

    Joined:
    Feb 10, 2019
    Posts:
    5
    Hi again,

    Thanks very much for your help. I was actually able to get the thing to work perfectly thanks to your helpful answers when I got around to tackling the project again a couple of days ago.

    However when I opened the project today for some reason when I run the game instead of the CM vcam1 focusing initially on the player game object(the character that can be controlled on the screen similar to Super Mario Bros) it focuses on the CM vcam2(the camera that zooms in when the player passes the trigger object) which is wrong.
    I obviously want the CM vcam1 to focus on the player game object when the game runs initially until it passes the trigger object and then have the CM vcam1 blend to CM vcam2.
    Also the the player game object has been dragged and dropped into the "follow" section of the CM vcam1.The priority of the cameras have both been set to 10.

    I'm not sure why this has happened as I saved the project when it was working perfectly a couple of days ago but when I reopened it the cameras aren't working properly at all.

    Kind regards
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,845
    CM Brain will pick the vcam with the highest priority. If there are multiple vcams with the same highest priority then the brain will choose the most-recently-activated among those. Try lowering the priority of vcam2.