[HOWTO] How To: Use Cinemachine with the Dialogue System

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Tony Li
Posts: 22137
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Use Cinemachine with the Dialogue System

Post by Tony Li »

Hi,

Would you please click on the NullReferenceException in the Console window, press Ctrl+C (or Cmd+C on Mac) to copy the details to the clipboard, and then paste them into a reply?
Strig
Posts: 40
Joined: Fri Aug 21, 2020 12:33 pm

Re: How To: Use Cinemachine with the Dialogue System

Post by Strig »

Here's two older prints I had of the error. Sorry if the first one is hard to read.
Captura de Tela (356).png
Captura de Tela (356).png (28.14 KiB) Viewed 3095 times
Captura de Tela (260).png
Captura de Tela (260).png (21.91 KiB) Viewed 3095 times
User avatar
Tony Li
Posts: 22137
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Use Cinemachine with the Dialogue System

Post by Tony Li »

Please post the text from the clipboard, not a screenshot. Thanks!
Strig
Posts: 40
Joined: Fri Aug 21, 2020 12:33 pm

Re: How To: Use Cinemachine with the Dialogue System

Post by Strig »

Sorry about that. Here's the full Console Log. Do tell if I need to post something else. To reiterate: the dialogues are the only thing triggering this error so far.

NullReferenceException: Object reference not set to an instance of an object
CinemachineShake.ShakeCamera (System.Single intensity, System.Single time) (at Assets/Scripts/CinemachineShake.cs:30)
HutongGames.PlayMaker.Actions.ShakeCam_PM.CallShake (System.Single intensity, System.Single time) (at Assets/Scripts/PlayMaker Actions/ShakeCam_PM.cs:21)
HutongGames.PlayMaker.Actions.ShakeCam_PM.OnEnter () (at Assets/Scripts/PlayMaker Actions/ShakeCam_PM.cs:15)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
HutongGames.PlayMaker.FsmState.OnEnter () (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
HutongGames.PlayMaker.Fsm.FixedUpdateState (HutongGames.PlayMaker.FsmState state) (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
HutongGames.PlayMaker.Fsm.FixedUpdate () (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
PlayMakerFixedUpdate.FixedUpdate () (at <7e9e696ff1a04620968849cd9bbf2ab4>:0)
User avatar
Tony Li
Posts: 22137
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Use Cinemachine with the Dialogue System

Post by Tony Li »

The error is here:

CinemachineShake.ShakeCamera (System.Single intensity, System.Single time) (at Assets/Scripts/CinemachineShake.cs:30)

What is CinemachineShake? It looks like a custom script. Can you share the code?
Strig
Posts: 40
Joined: Fri Aug 21, 2020 12:33 pm

Re: [HOWTO] How To: Use Cinemachine with the Dialogue System

Post by Strig »

Sorry for the late reply. Here is the code for the Cinemachine Shake script.

Code: Select all

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

public class CinemachineShake : MonoBehaviour
{
    public static CinemachineShake Instance { get; private set; }

    private CinemachineVirtualCamera virtualCamera;

    [SerializeField]
    private CinemachineBrain cinemachineBrain;
    private float shakeTimer;
    private float shakeTimerTotal;
    private float startingIntensity;

    private void Awake()
    {
        Instance = this;
        cinemachineBrain = GetComponent<CinemachineBrain>();
    }

    public void ShakeCamera(float intensity, float time)
    {
        virtualCamera = (CinemachineVirtualCamera)cinemachineBrain.ActiveVirtualCamera;
        CinemachineBasicMultiChannelPerlin cameraShake = virtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();


        cameraShake.m_AmplitudeGain = intensity;

        startingIntensity = intensity;
        shakeTimerTotal = time;
        shakeTimer = time;
    }

    public void ShakeCamera(CameraShakeParams settings)
    {
        ShakeCamera(settings.intensity,settings.duration);
    }

    private void Update()
    {
        if (shakeTimer > 0)
        {
            shakeTimer -= Time.deltaTime;
            if (shakeTimer <= 0)
            {
                CinemachineBasicMultiChannelPerlin cameraShake = virtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
                cameraShake.m_AmplitudeGain = Mathf.Lerp(startingIntensity, 0f, 1- (shakeTimer / shakeTimerTotal));
            }
        }
    }

    public void SetInstance()
    {
        Instance = this;
    }

    [System.Serializable]
    public struct CameraShakeParams
    {
        public float intensity;
        public float duration;
    }
}
The problem is when it tries to change the amplitude gain, apparently, but what's got me stumped is that I don't think the dialogues should be interfering in Cinemachine's Basic Perlin. Maybe there's something I'm not seeing?
User avatar
Tony Li
Posts: 22137
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Use Cinemachine with the Dialogue System

Post by Tony Li »

What's the reason for calling SetInstance in the Dialogue System Trigger?

Can you remove it? I suspect that SetInstance is setting CinemachineShake to a value that later becomes invalid.
User avatar
PayasoPrince
Posts: 104
Joined: Thu Jan 27, 2022 6:47 pm

Re: [HOWTO] How To: Use Cinemachine with the Dialogue System

Post by PayasoPrince »

This thread was useful for me and helped me use Cinemachine with The Dialogue System.
Is there a way to pin this? :idea:
User avatar
Tony Li
Posts: 22137
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Use Cinemachine with the Dialogue System

Post by Tony Li »

There are a lot of HOWTO threads, especially in the Dialogue System for Unity forum section. Instead of pinning them all (which would fill up the first several pages of the forum), you can enter "HOWTO" in the search bar to see all How To articles.
User avatar
HawkX
Posts: 147
Joined: Mon Feb 27, 2017 1:50 pm
Location: Quebec
Contact:

Re: [HOWTO] How To: Use Cinemachine with the Dialogue System

Post by HawkX »

Sorry for reviving a 2year old thread (which was already revived this past january) ;)

I am trying to find how to "freeze" cinemachine virtual camera movement during cutscene (dialogues)

Our cinemachine vcam has a variable horizontal axis on Mouse X from -45 to 45 angle, and it keeps moving even during dialogues... any suggestion to freeze it would be greatly appreciated! :)
Post Reply