Search Unity

Using Virtual Camera in If Statement

Discussion in 'Cinemachine' started by GordonArber, Feb 2, 2019.

  1. GordonArber

    GordonArber

    Joined:
    Jan 2, 2016
    Posts:
    22
    I am using 2 virtual cameras in my game and switching them with Clear Shot. I want to trigger a meter if a certain camera is live. I can't seem to figure it out. Here is my code, what am I doing wrong?

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

    public class SuspicionBarScript : MonoBehaviour
    {
    public CinemachineVirtualCamera storeFrontCam;
    public CinemachineVirtualCamera kitchenCam;

    Image suspicionBar;
    float maxSuspicion = 100f;
    public static float suspicion;
    private const float coef = 0.2f;
    private const float pos = 5f;
    // Start is called before the first frame update
    void Start()
    {
    suspicionBar = GetComponent<Image>();
    suspicion = 0f;
    }

    // Update is called once per frame
    private void Update()
    {
    suspicionBar.fillAmount = suspicion / maxSuspicion;

    if (storeFrontCam.m_StandbyUpdate.Equals("Live") && suspicion > 0)
    {
    suspicion -= pos * Time.deltaTime;
    }
    else
    {
    suspicion += coef * Time.deltaTime;
    }

    if (suspicion >= 100)
    {
    FindObjectOfType<GameHandler>().EndGame();
    }
    }
    }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Why not just use the OnCameraLive event issued by the vcam? It fires whenever the vcam goes live. Also, the Brain has an OnVcamActivated event, when fires whenever any vcam goes live.
     
  3. GordonArber

    GordonArber

    Joined:
    Jan 2, 2016
    Posts:
    22
    I played around with that and couldn't figure out how continue to get it updating. I tried a while loop in the methods that I was calling, but wouldn't I still need to say something like
    Code (CSharp):
    1. While(virtualCam1 is active)
    2. {
    3. Do This Code
    4. }
    I would still need to know how to call that camera because that isn't the correct way
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Oh, I see. You don't want to know when the vcam goes live, you want to do something while the vcam is live.
    You need to do: CinemachineCore.Instance.IsLive(vcam);
     
  5. GordonArber

    GordonArber

    Joined:
    Jan 2, 2016
    Posts:
    22
    Awesome! That worked for the if statement. Is there a way to get that to work with the Cinemachine built in On Camera Live? I really appreciate your help!
     
    Last edited: Feb 4, 2019