Hello guys!
I came across a problem that I wasn’t able to solve it for 2 days till now.
What I’m trying to achieve:
What I’m trying to do is to make my player to play a video when he is inside the trigger and you press the spacebar, the video is displaying through UI RawImage inside a Canvas with a RenderTexture properly set.
My setup:
I have a collider set on Is Trigger (Stand0_Trigger on the image) with a script attached and a VideoPlayer component as well.
On my scene I have a Canvas with a button to indicate you can play the video, there’s also a Panel with the RawImage where I can display the video, the panel is hidden by default, but I set the visibility as soon as you press the spacebar.
On the other hand I have a Character Controller with my custom avatar and a camera as children.
The avatar has a script to control its movement (its a 3d world) and the camera has another to control the rotation with the mouse, so you can look everywhere.
The problem:
So my whole setup works pretty well, when I play my project and came close to the trigger and press the spacebar the panel turns on and the video starts.
Now, the problem occurs when the video ends and the user stop the video or close the panel, because If you press the spacebar again, the video is not showing neither the panel activates.
The only way to make it work, is if you click on the screen (any place) and then you press the spacebar.
The weird part is that I put a Debug Log when the user hit spacebar and the log indeed shows my message but the actions aren’t executed.
Images:
My code:
```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class StandVideo_Trigger : MonoBehaviour
{
public VideoPlayer screenVid;
public Button interactions;
public GameObject vidPanel;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//Debug.Log("Space Bar pressed!");
vidPanel.SetActive(true);
//screenVid.Play();
StartCoroutine(PlayVideo());
}
}
public void OnTriggerStay(Collider other)
{
interactions.interactable = true;
if (Input.GetKeyDown(KeyCode.Space))
{
//Debug.Log("Space Bar pressed!");
//vidPanel.SetActive(!vidPanel.activeSelf);
//screenVid.Play();
//StartCoroutine(PlayVideo());
}
}
public void OnTriggerExit(Collider other)
{
screenVid.Stop();
interactions.interactable = !interactions.interactable;
}
private IEnumerator PlayVideo()
{
Debug.Log("Trying to play Video");
while (!screenVid.isPlaying)
{
screenVid.Play();
yield return new WaitForEndOfFrame();
}
Debug.Log("Video is playing: " + screenVid.isPlaying);
}
public void ShowPanel()
{
vidPanel.SetActive(false);
}
}**
```
So after 2 days of trying several setups and a lot of trial and error, I decided to call my actions on the update method instead of the OnTriggerStay and surprisingly it worked as expected using the same code.
So that makes me wonder if this is a bug or just a normal behavior from the OnTriggerStay method.
Any insights or thoughts will be appreciated.
From the Unity documentation: