I am using the following script to trigger a cutscene in horror fps kit which I have converted into a video to spare self of the atrocity of adding prefabs again to the scene and make it more complicated. The above script works as intended as in when the health of enemy drops beneath 200 something is indeed triggered however it says no camera rendering Secondly apparently it pauses the game however in horror fps kit the game in the background doesnt pause event if you press esc and go to pause menu. Any work around for this? or if anyone understands the code try the code with any emerald ai enemy and share the results .
using UnityEngine;
using EmeraldAI;
using UnityEngine.Video;
using UnityEngine.SceneManagement;
using Cinemachine;
using UnityEngine.UI;
public class EnemyHealthCutsceneTrigger : MonoBehaviour
{
public EmeraldAISystem emeraldAI;
public int healthThreshold = 200;
public VideoPlayer cutsceneVideoPlayer; // Reference to the VideoPlayer
public RenderTexture renderTexture; // Reference to the Render Texture
public Camera cutsceneCamera; // Camera used to render to the Render Texture
public Camera horrorFPSMainCamera; // Reference to the Main Camera of Horror FPS Kit
public Camera horrorFPSArmsCamera; // Reference to the Arms Camera of Horror FPS Kit
public CinemachineBrain cinemachineBrain; // Reference to the Cinemachine Brain
public CinemachineVirtualCamera cutsceneVirtualCamera; // Reference to the Cinemachine Virtual Camera
public Canvas cutsceneCanvas; // Reference to the Canvas containing the RawImage
public RawImage cutsceneRawImage; // Reference to the RawImage displaying the video
private bool cutsceneTriggered = false;
private MonoBehaviour[] playerComponents;
void Start()
{
if (emeraldAI == null)
{
emeraldAI = GetComponent<EmeraldAISystem>();
}
if (cutsceneVideoPlayer == null || renderTexture == null)
{
Debug.LogError("VideoPlayer or Render Texture is not assigned.");
}
if (horrorFPSMainCamera == null || horrorFPSArmsCamera == null)
{
Debug.LogError("Main Camera and/or Arms Camera used by the Horror FPS Kit is not assigned.");
}
if (cinemachineBrain == null)
{
Debug.LogError("Cinemachine Brain is not assigned.");
}
if (cutsceneVirtualCamera == null)
{
Debug.LogError("Cinemachine Virtual Camera for the cutscene is not assigned.");
}
if (cutsceneCamera == null)
{
Debug.LogError("Cutscene Camera is not assigned.");
}
if (cutsceneCanvas == null || cutsceneRawImage == null)
{
Debug.LogError("Cutscene Canvas or RawImage is not assigned.");
}
// Ensure that the cutscene camera, virtual camera, Cinemachine Brain, Canvas, and RawImage are initially disabled
cutsceneCamera.gameObject.SetActive(false);
cutsceneVirtualCamera.gameObject.SetActive(false);
cinemachineBrain.enabled = false;
cutsceneCanvas.enabled = false;
cutsceneRawImage.enabled = false;
// Configure the Video Player
cutsceneVideoPlayer.renderMode = VideoRenderMode.RenderTexture;
cutsceneVideoPlayer.targetTexture = renderTexture;
// Get all the components that should be disabled during the cutscene
playerComponents = GetComponents<MonoBehaviour>();
// Subscribe to the VideoPlayer's loopPointReached event to detect when the video finishes
cutsceneVideoPlayer.loopPointReached += OnCutsceneEnd;
}
void Update()
{
if (!cutsceneTriggered && emeraldAI.CurrentHealth <= healthThreshold)
{
TriggerCutscene();
cutsceneTriggered = true;
}
}
void TriggerCutscene()
{
// Disable the Horror FPS Kit cameras
horrorFPSMainCamera.enabled = false;
horrorFPSArmsCamera.enabled = false;
// Enable the cutscene camera and set depth
cutsceneCamera.depth = 1;
cutsceneCamera.gameObject.SetActive(true);
// Ensure Cinemachine is active and cutscene virtual camera is enabled
cinemachineBrain.enabled = true;
cutsceneVirtualCamera.gameObject.SetActive(true);
// Enable the Canvas and RawImage for displaying the cutscene
cutsceneCanvas.enabled = true;
cutsceneRawImage.enabled = true;
// Pause the game
Time.timeScale = 0f;
// Play the video
cutsceneVideoPlayer.Play();
}
void OnCutsceneEnd(VideoPlayer vp)
{
// Stop the Video Player
cutsceneVideoPlayer.Stop();
Debug.Log("Cutscene ended. Disabling cutscene elements...");
// Disable the cutscene camera, Cinemachine Brain, virtual camera, Canvas, and RawImage
cutsceneCamera.gameObject.SetActive(false);
cutsceneVirtualCamera.gameObject.SetActive(false);
cinemachineBrain.enabled = false;
cutsceneRawImage.enabled = false;
Debug.Log("RawImage disabled: " + cutsceneRawImage.enabled);
cutsceneCanvas.enabled = false;
Debug.Log("Canvas disabled: " + cutsceneCanvas.enabled);
// Re-enable the Horror FPS Kit cameras
horrorFPSMainCamera.enabled = true;
horrorFPSArmsCamera.enabled = true;
Debug.Log("Horror FPS Kit cameras re-enabled.");
// Re-enable all player-related components to resume gameplay
foreach (var component in playerComponents)
{
component.enabled = true;
}
Debug.Log("Player components re-enabled.");
// Resume the game
Time.timeScale = 1f;
// Optionally, transition to the next scene after the cutscene
SceneManager.LoadScene("Scene 3(The showdown)");
}
}