Search Unity

How to Trigger State-Driven vcam?

Discussion in 'Cinemachine' started by rrabassa, Oct 31, 2017.

  1. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170
    Hello!

    I have a state driven camera that switches camera setups according to the player's animation state. Is there a way to switch vcams based on a collider trigger? The player's animation might not change, but I'd like to change the camera setup for certain parts of the game.

    Thanks,
    rich
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Within the SDC, no, but you could have another vcam and activate that from your trigger. The SDC would just blend to that, and it would blend back when your override vcam deactivates.
     
  3. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170
    So onTriggerEnter I can activate another vcam outside of the SDC? Do you have an example how to do that? Camera.Enable doesn't seem to see the vcams.

    Thanks,
    rich
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Use vcam.gameObject.SetActive() to toggle its active status.
    This activates/deactivates the vcam. The Unity camera will become aware of it automatically because of its CinemachineBrain component.
     
    tasticad likes this.
  5. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170
    Excellent! I will try that. Also I'm guessing priority on the vcam will sort everything out?

    -rich
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Yes, the higher-priority vcam will win. If multiple active vcams have the same (highest) priority, then the most-recently-activated one will win.
     
  7. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170
    Hi again,

    Kinda grasping at straws here---

    using Cinemachine.Utility;
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Serialization;

    public class vCamSwitch : MonoBehaviour {

    public CinemachineVirtualCamera vcam;


    void OnTriggerEnter(Collider other) {
    switchToThisVcam();
    }

    public void switchToThisVcam()
    {

    vcam.gameObject.SetActive();
    }


    }


    What am I missing :) ?
    -rich
     
  8. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170
    Okay I got it working but It's not blending, it's a direct cut to the camera. However, it blends on exit. Any suggestions how I blend it in?



    thanks,

    rich


    here's the code:

    using Cinemachine.Utility;
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Serialization;

    public class vCamSwitch : MonoBehaviour {
    public GameObject vcam;

    void OnTriggerEnter(Collider other) {
    switchToThisVcam();
    }


    void OnTriggerExit(Collider other) {
    switchVcamOff();
    }


    public void switchToThisVcam()
    {

    vcam.gameObject.SetActive(true);
    }

    public void switchVcamOff()
    {

    vcam.gameObject.SetActive(false);
    }


    }
     
    Hawk0077 likes this.
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Can you show me a picture of the inspector for your CinemachineBrain?
    In particular, I want to see whether you have custom blend rules that would produce the result you're seeing.
     
  10. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170
    I think i figured it out -- When I created the new Virtual Camera, at some point I wound up with a Camera component and a Cinemachine Brain. When I disabled these two components it seems to work. Not sure how the extra camera and brain got in there tho - here's a screenshot:

    Screen Shot 2017-11-01 at 12.49.59 PM.png

    I have noticed something else. If I put the newly created Virtual Camera into the CM StateDrivenCamera object (along with the other cameras), I can't seem to reference it from my trigger script. If it's outside of the group, then I can. See image (vcam4 specifically).

    Screen Shot 2017-11-01 at 12.52.01 PM.png
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Are you creating these vcams using the Cinemachine menu in the editor, or are you doing it by script? I ask because it's very peculiar to be getting these extra brains and Camera components. It's a good idea to clean them up, or general confusion will result.

    Your scene should have a single camera with a brain component. Delete all the others (remove them, don't just deactivate).

    The way the State-Driven-Camera works (also ClearShot, MixingCamera, and other "meta-camera" manager vcams) is that they manage their vcam children and hide them from the outside world. Their children become a kind of private vcam army, that they manage. Parenting vcam4 under the SDC effectively enrolls vcam4 in the SDC's private army - which you don't want to do. Keep it outside, where it belongs.

    That said, nothing stops you from organizing your vcams in container objects for neatness, but be aware that vcams parented to other vcams has a special meaning in Cinemachine. Don't do it without just cause.
     
  12. rrabassa

    rrabassa

    Joined:
    Jul 17, 2013
    Posts:
    170

    I created it using the Cinemachine Menu but I'm unable to recreate it - it's possible I duplicated an earlier camera at some point.
     
  13. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks, this worked for me too. Im not a coder so much appreciated.
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Cinemachine now comes with a handy CinemachineTriggerAction helper script. Attach it to your collider and then you can use it to activate/deactivate vcams, play timelines, trigger events, and other things, all without having to code.

    upload_2020-3-12_10-59-7.png
     
    Hawk0077 likes this.
  15. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi @Gregoryl, this method is not working for me.

    On my Trigger Zone, I have a Box Collider with Trigger bool checked. Regarding to this post https://forum.unity.com/threads/cinemachine-trigger-action-not-working-anymore.794331/, I also added a Rigidbody with Gravity unchecked.

    On my SDC, I made an EaseIn/Out transition when switching between the TriggerCam and the GameplayCam. But there are no transition happeing on entering/exiting the Trigger zone.

    Questions :
    • Should I deactivate the TriggerCam first?
    • Should I support this method with any other code?
    • or is this simply a bug?

    On the trigger zone
    upload_2020-3-26_13-26-0.png

    SDC
    upload_2020-3-26_13-24-49.png
     
    Last edited: Mar 26, 2020
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    It will work if you take VCAM_HOUSE_CloseUp out of the StateDrivenCamera. Vcams parented to SDC cannot be activated except by the SDC itself.

    Vcam parenting in CM has a special meaning: it makes the child vcam wholly owned by the parent, so that it can only be activated by the parent.
     
    Hawk0077 likes this.
  17. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Thanks for the quick reply. Unfortunately, it does not. I removed VCAM_HOUSE_CloseUp from the SDC hierarchy and expected to switch from CM_ThirdPerson_FreeLook to it when entering the trigger zone, but nothing happens.

    Also, let's say it did work. How then will it be possible to blend between those two cameras without having them under the same SDC? (Sorry if it's a newbie question, I'm freshly starting with SDC).
     
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Maybe it's a priority thing. Put VCAM_HOUSE_CloseUp at a higher priority that the SDC, then when it's activated it will become dominant.

    As for blends, it's the job of the CM Brain (on the main camera) to take care of blending between the vcams. When a new vcam is activated with sufficient priority, the brain will apply its default blend to the transition (unless you have a custom blend defined for this one).

    I get the sense that in your project the SDC is unnecessary. If your default vcam is the FreeLook, and you want to activate other vcams in response to game events, then you should not be using the SDC. Just leave the FreeLook loose, like VCAM_HOUSE_CloseUp. The SDC is for mapping vcams to animation states.

    For example, if your default FreeLook had different settings depending on what the player is doing (walking, running, etc) then you could have multiple FreeLooks parented to the SDC, each with slightly different settings, with instructions to map them to specific animation states of the player. The SDC would blend between them based on the animation state. The whole thing would look like one vcam to the brain, which would then blend into or out of this complex thing very simply.
     
  19. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    I took your advice and tried a different approach. I stopped using SDC beacause as you said, as it is only for animation states mapping, I woulf have been stucked at a certain point with this limit. I can't find the post, but someone suggested to have a simple list containing all my Vcams on a parent node under which they are and call them individually and when needed by index. This indeed works perfectly for me.


    I'm really loving Cinemachine right know due to this :)

    Again, many thanks for your help!
     
    Gregoryl likes this.