How to Trigger State-Driven vcam?

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

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.

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

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.

1 Like

Excellent! I will try that. Also I’m guessing priority on the vcam will sort everything out?

-rich

Yes, the higher-priority vcam will win. If multiple active vcams have the same (highest) priority, then the most-recently-activated one will win.

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 :slight_smile: ?
-rich

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);
}

}

1 Like

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.

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:

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).

3273361--253009--Screen Shot 2017-11-01 at 12.52.01 PM.png

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.

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.

Thanks, this worked for me too. Im not a coder so much appreciated.

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.

5581456--576367--upload_2020-3-12_10-59-7.png

1 Like

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://discussions.unity.com/t/769291 , 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
5631844--584908--upload_2020-3-26_13-26-0.png

SDC
5631844--584905--upload_2020-3-26_13-24-49.png

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.

1 Like

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).

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.

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 :slight_smile:

Again, many thanks for your help!

1 Like