Search Unity

Question OnTriggerStay method is preventing certain actions

Discussion in 'Physics' started by Tangerunity3d, Jul 28, 2020.

  1. Tangerunity3d

    Tangerunity3d

    Joined:
    Jun 9, 2020
    Posts:
    13
    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:
    https://ibb.co/SxZj9JC
    https://ibb.co/TgrRwrc

    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Video;
    6.  
    7. public class StandVideo_Trigger : MonoBehaviour
    8. {
    9.     public VideoPlayer screenVid;
    10.    
    11.     public Button interactions;
    12.     public GameObject vidPanel;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (Input.GetKeyDown(KeyCode.Space))
    23.         {
    24.             //Debug.Log("Space Bar pressed!");
    25.             vidPanel.SetActive(true);
    26.             //screenVid.Play();
    27.             StartCoroutine(PlayVideo());  
    28.         }
    29.     }
    30.  
    31.     public void OnTriggerStay(Collider other)
    32.     {
    33.         interactions.interactable = true;
    34.  
    35.         if (Input.GetKeyDown(KeyCode.Space))
    36.         {
    37.             //Debug.Log("Space Bar pressed!");
    38.             //vidPanel.SetActive(!vidPanel.activeSelf);
    39.             //screenVid.Play();
    40.             //StartCoroutine(PlayVideo());  
    41.         }
    42.     }
    43.  
    44.     public void OnTriggerExit(Collider other)
    45.     {
    46.         screenVid.Stop();
    47.         interactions.interactable = !interactions.interactable;
    48.        
    49.     }
    50.  
    51.     private IEnumerator PlayVideo()
    52.     {
    53.         Debug.Log("Trying to play Video");
    54.         while (!screenVid.isPlaying)
    55.         {
    56.             screenVid.Play();
    57.             yield return new WaitForEndOfFrame();
    58.         }
    59.         Debug.Log("Video is playing: " + screenVid.isPlaying);
    60.     }
    61.  
    62.     public void ShowPanel()
    63.     {
    64.         vidPanel.SetActive(false);
    65.     }
    66. }
    67.  


    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:
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Physics callbacks happen at the end of the simulation step which unless you run physics yourself per render frame will be per fixed-update frame.

    The Stay callbacks stop happening when the physics bodies involved sleep as well.
     
    Tangerunity3d likes this.
  3. Tangerunity3d

    Tangerunity3d

    Joined:
    Jun 9, 2020
    Posts:
    13
    Thanks for the clarification.

    So, what can I do overcome my problem?

    I have some UI elements that allows me to play, pause or close my video but if I want to play the video again without moving my player out of the collider, nothing works unless I click the screen (which probably takes the physics out of sleep hence detecting my inputs again)