Forum rules - please read before posting.

Cinemachine workflow

I'll share here what's my workflow (workaround) for using cinemachine and timeline with AC, and see if it's the optimal way of integrating both.
Basically I use the AC camera as the brain, set my gameCamera (currently opsive's TPS) as as an external cinemachine camera, and disabling the cinemachine component on every other virtual camera, and use an AC action to set the priority of the virtual cameras and enabling them if I need them on the gameplay. My action

using UnityEngine;
using System.Collections;
using Cinemachine;

#if UNITY_EDITOR
using UnityEditor;
namespace AC
{

    [System.Serializable]
    public class ActionCinemachineCamera : Action
    {

        // Declare variables here
        public CinemachineVirtualCameraBase CM_cam;
        public int Priority;

        public ActionCinemachineCamera()
        {
            this.isDisplayed = true;
            category = ActionCategory.Camera;
            title = "CM Priority";
            description = "Changes CM priority";
        }


        override public float Run()
        {
            if (CM_cam)
            {
                if (CM_cam.GetComponent<CinemachineVirtualCameraBase>())
                {
                    CM_cam.GetComponent<CinemachineVirtualCameraBase>().enabled = true;
                    CM_cam.MoveToTopOfPrioritySubqueue();
                }
                CM_cam.Priority = Priority;
            }
            return 0f;
        }


#if UNITY_EDITOR

override public void ShowGUI ()
{
            CM_cam = (CinemachineVirtualCameraBase)EditorGUILayout.ObjectField("CM Camera:", CM_cam, typeof(CinemachineVirtualCameraBase), true);
            Priority = EditorGUILayout.IntField("Priority:", Priority);
            AfterRunningOption();
}

public override string SetLabel ()
{
string labelAdd = "Changes virtual camera priority and moves to top queue";
return labelAdd;
}


    }

}
This way I can use Cinemachine in timeline, custom cameras and also cinemachine cameras during gameplay. Any drawbacks ? Suggestions ? What is your way ?

Comments

  • Looks good to me!  It'd be very welcome on the community wiki.
  • Can you do it ? I have no idea how to post it there.
  • Hi Guys,
    Thanks for putting this together. I'm looking into extending this so that in integrates with AC's Default Camera Switch Action.

    I'd like to have it detect whether the cam has a cinemachine component on it, and if it does, raise the priority, while also switching the cam.

    Then I'd like to have it revert the priority back to the original number.

    I've been hunting around for the default Camera Switch Action code. I think taking it as a base, it should be fairly simple to put the rest together. Do you know wher eI can find it?

    Thanks!

  • Sounds intereseting, @juanelo_dev.

    You can edit the script of any Action via its cog menu in the ActionList Editor window. The Camera: Switch Action is stored in ActionCamera.cs - which you can subclass if you want to make an Action that derives from it.

    However, it's not just the Action that's used to switch camera - cameras are also set when entering scenes, and loading save games. From what you've described, it would be better to hook into the OnSwitchCamera custom event instead - which will allow you to run custom code whenever the camera is set. Something like:

    using UnityEngine;
    using AC;
    
    public class SetCMPriority : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnSwitchCamera += SwitchCamera; }
        private void OnDisable () { EventManager.OnSwitchCamera -= SwitchCamera; }
    
        private void SwitchCamera (_Camera old, _Camera newCamera, float tt)
        {
            // Check if newCamera has a Cinemachine component here
        }
    
    }
    
  • This is great, @ChrisIceBox. Thanks!

  • using UnityEngine;
    using AC;
    using Cinemachine;
    
    [RequireComponent(typeof(CinemachineBrain))]
    public class SetCMPriority : MonoBehaviour
    {
    
        public int offPriority = 10;
        public int onPriority = 20;
        public bool setLookat = true;
    
        private void OnEnable() { EventManager.OnSwitchCamera += SwitchCamera; }
        private void OnDisable() { EventManager.OnSwitchCamera -= SwitchCamera; }
    
        private void SwitchCamera(_Camera oldCamera, _Camera newCamera, float tt)
        {
            if (oldCamera != null)
            {
                CinemachineVirtualCameraBase oldCMCam = oldCamera.GetComponent<CinemachineVirtualCameraBase>();
                if (oldCMCam != null)
                {
                    oldCMCam.Priority = offPriority;
                    oldCMCam.LookAt = null;
                }
            }
    
            CinemachineVirtualCameraBase newCMCam = newCamera.GetComponent<CinemachineVirtualCameraBase>();
            if (newCMCam != null)
            {
                newCMCam.Priority = onPriority;
                if (setLookat) newCMCam.LookAt = AC.KickStarter.player.transform;
            }
        }
    
    }
    

    Create a new C# script with the above code and attach that to your AC MainCamera, it will attach a CinemachineBrain component if there isn't already one. Next add a CinemachineVirtualCamera component to your AC GameCameras and voila, full cinemachine control. I'm using it to do depth of field tracking, handheld noise, and I'm going to set it up to use composer to follow the player around the scene but still use the deadzones.

  • @rtwarner THANK YOU!! This is so much better, I came here hoping for this exact solution. Manually setting the priority gets really messy down the line, this is perfect :)

  • Awesome, @rtwarner ! Did you ever end up doing an update on this?

  • edited July 2022

    I didn't use the official Cinemachine bridge.

    I took @rtwarner's approach and extended it. And I created a new "CM Action: ActionCamera".

  • Looks good, @zfh2773!

    A rudimentary Cinemachine integration can now also be found on the AC Downloads page.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.