Search code examples
c#unity-game-enginegame-developmentvideo-player

I am struggling to - smoothly - skip forward in a playing video


Unity Game Engine

I have a video of a character jumping forward in multiple steps.

Until now, I have a semi-functioning C# script with a long paragraph of IF/ELSE statemens, skipping through a specific set of frame-numbers everytime I hit 'Spacebar' ( Which is what I want ) – The problem is when I run the scene in Unity: It's laggy and skips half of the button-presses or sometimes counts double.

Unity Game Engine

Here I start the Scene/Game by pausing the video. Hitting the 'spacebar' plays the video until we hit frame number: 24, and pauses. Pressing play again continues the video until we hit frame number 48, and pauses. And so on...

This technique does work, but it is very laggy and unreliable. Sometimes I have to hit 'spacebar' multiple times, sometimes the character does only half a jump and the video just skips to the next set frame.

I found the class-property of VideoPlayer: isPrepared which sounds like something I need. But I can't figure out how to implement this correctly.

If you have any suggention for improvements, it would greatly appreciated.

Here is my clunky-but-semi-functioning code.

 // Here I start by pausing the game
    void Start()
    {
      _MiniGame.Pause();
    }

    // Hitting the 'Spacebar' initiates the jump
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            Debug.Log("Spacebar pressed");
            _MiniGame.Play();
        }
        else if (_MiniGame.frame == 24)
        {
            _MiniGame.Pause();
        }
        else if (_MiniGame.frame == 48)
        {
            _MiniGame.Pause();
        }
        else if (_MiniGame.frame == 72)
        {
            _MiniGame.Pause();
        }
        else if (_MiniGame.frame == 96)
        {
            _MiniGame.Pause();
        }
        else if (_MiniGame.frame == 120)
        {
            _MiniGame.Pause();
        }
        else if (_MiniGame.frame == 144)
        {
            _MiniGame.Pause();
        }
    }

Solution

  • So the most efficient way I could find was to actually cut the video into 7 smaller pieces, with a tiny pause inbetween. Making Unity able to pre-load just enough so it wouldn't feel sluggish. Thank you all for your input, it has been very much appreciated 💛

    public VideoPlayer _01;
    public VideoPlayer _02;
    public VideoPlayer _03;
    public VideoPlayer _04;
    public VideoPlayer _05;
    public VideoPlayer _06;
    public VideoPlayer _end;
    
    public void jump()
    {
      if (isCoolDown == false)
      {
        if (_01.enabled == true)
        {
          _01.enabled = false;
          _02.enabled = true;
          StartCoroutine(CoolDown( ));
        }
        else if (_02.enabled == true)
        {
          _02.enabled = false;
          _03.enabled = true;
          StartCoroutine(CoolDown( ));
        }
        else if (_03.enabled == true)
        {
          _03.enabled = false;
          _04.enabled = true;
          StartCoroutine(CoolDown( ));
        }
        else if (_04.enabled == true)
        {
          _04.enabled = false;
          _05.enabled = true;
          StartCoroutine(CoolDown( ));
        }
        else if (_05.enabled == true)
        {
          _05.enabled = false;
          _06.enabled = true;
          StartCoroutine(CoolDown( ));
        }
        else if (_06.enabled == true)
        {
          _BottomCanvas_01.SetActive(false);
          _BottomCanvas_02.SetActive(true);
          _06.enabled = false;
          _end.enabled = true;
          Debug.Log("Last jump");
        }
      }
    }
    
    IEnumerator CoolDown( )
    {
    isCoolDown = true;
    yield return new WaitForSeconds(coolDown);
    isCoolDown = false;
    }