Forum rules - please read before posting.

Cinemachine Camera

Hello everyone,

I ran into two issues when using Cinemachine camera with AC,

At first I created a freelook Cinemachine Camera and attached AC Gamecamera to it and it didn't work.
However, when I attached the basic Camera component to Cinemachine camera everything worked smoothly, until I faced this issue.. the Cinemachine Camera requires the object to follow and look at to be available in the scene, and my player is not in the scene as I'm relaying on AC to Spawn the player prefab.. If I used the player which is in the project folder and not in the hierarchy and played the scene the camera will not follow the player anymore.

The second issue is that when I do a camera switch using AC interaction it doesn't switch from Cinemachine camera to AC Camera and vice versa, even with the basic camera component attached...

Is there a solution for these two issues?

Regards

Comments

  • edited February 2021

    the Cinemachine Camera requires the object to follow and look at to be available in the scene

    Unless you rely on Player-switching, or need the state of your Player to be saved between scenes (i.e. animation state), then you're not restricted to spawning the Player in at runtime - you can place them in the scene file.

    Otherwise, you'd have to assign the Cinemachine camera's target through script once the Player has been spawned:

    using UnityEngine;
    using Cinemachine;
    using AC;
    
    public class SetPlayerTarget : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnSetPlayer += SetNewPlayer; }
        private void OnDisable () { EventManager.OnSetPlayer -= SetNewPlayer; }
    
        private void SetNewPlayer (Player player)
        {
            GetComponent <CinemachineFreeLook>().m_Follow = player.transform;
            GetComponent <CinemachineFreeLook>().m_LookAt = player.transform;
        }
    
    }
    

    when I do a camera switch using AC interaction it doesn't switch from Cinemachine camera to AC Camera and vice versa

    Both AC and Cinemachine work similarly in that they both rely on a "master" camera (AC's MainCamera and Cinemachine's CMBrain) that perform the actual rendering, while relying on "reference" cameras (AC's GameCameras and Cinemachine's VCams) for position referencing.

    If you're using the CMBrain for rendering, then it won't be aware of any AC components / camera Actions.

    See the Manual's "Working with Cinemachine" chapter. The recommended way of working is to instead attach AC's Basic Camera component to the CMBrain object, and disable rendering on the CMBrain's Camera. That way, AC's MainCamera is the only one actually rendering, and you can switch to Cinemachine by switching to the CMBrain as a regular GameCamera.

  • @ChrisIceBox Impressive as always.

    It worked perfectly.
    Thank you so much!

  • edited February 2021

    Otherwise, you'd have to assign the Cinemachine camera's target through script once the Player has been spawned:

        using UnityEngine;
        using Cinemachine;
        using AC;
    
        public class SetPlayerTarget : MonoBehaviour
        {
    
            private void OnEnable () { EventManager.OnSetPlayer += SetNewPlayer; }
            private void OnDisable () { EventManager.OnSetPlayer -= SetNewPlayer; }
    
            private void SetNewPlayer (Player player)
            {
                GetComponent <CinemachineFreeLook>().m_Follow = player.transform;
                GetComponent <CinemachineFreeLook>().m_LookAt = player.transform;
            }
    
    }
    

    Hello again, I've attached this script to the camera with the CM FreeLook component, everything seemed to be working fine, until I close the project and reopen it or when I build the project, the camera's target becomes empty.
    To recreate the issue simply attach the script then try building. It will no longer work until you delete the script and add it again, but that won't fix it :(

  • Update:
    I noticed that the script works fine only if I don't follow this method:

    The recommended way of working is to instead attach AC's Basic Camera component to the CMBrain object, and disable rendering on the CMBrain's Camera. That way, AC's MainCamera is the only one actually rendering, and you can switch to Cinemachine by switching to the CMBrain as a regular GameCamera.

    Otherwise the target would be empty.

    Here is a screenshot of my camera component:

    https://imgur.com/4cZSBg7

    and the Main camera:

    https://imgur.com/18Soo21

  • Though the CMBrain camera component is disabled, you still need to set up your Cinemachine system in the usual way. Don't attach everything onto a single object - keep your CMBrain and VCams on separate objects.

  • edited February 2021

    I separated both the CMBrain and CMfreelook Cam away from each other. Yet still whenever I change scenes and come back the camera target is empty..

  • Try this:

    using UnityEngine;
    using Cinemachine;
    using AC;
    
    public class SetPlayerTarget : MonoBehaviour
    {
    
        private void OnEnable ()
        {
            EventManager.OnSetPlayer += SetNewPlayer;
            EventManager.OnAfterChangeScene += OnAfterChangeScene;
        }
    
        private void OnDisable ()
        {
            EventManager.OnSetPlayer -= SetNewPlayer;
            EventManager.OnAfterChangeScene -= OnAfterChangeScene;
        }
    
        private void OnAfterChangeScene (LoadingGame loadingGame)
        {
            SetNewPlayer (KickStarter.player);
        }
    
        private void SetNewPlayer (Player player)
        {
            if (player)
            {
                GetComponent <CinemachineFreeLook>().m_Follow = player.transform;
                GetComponent <CinemachineFreeLook>().m_LookAt = player.transform;
            }
        }
    
    }   
    
  • Hi @ChrisIceBox I attached the above script to my Cinemachine Virtual Camera, but it throws up a NullReferenceException error. Am I using it correctly? The "Look at" in the second image is where I want the Player prefab to be entered upon runtime.

    image
    image
    image

  • The script above requires a CinemachineFreeLook component attached - but you should be able to replace this with CinemachineVirtualCamera in the code.

  • Got it, thanks!

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.