Wartoc
Wartoc

Reputation: 17

Unity, Vr, Vive - Controllers To Play / Pause a Video?

I am new and currently trying to get my Vive Controllers to Pause / Play Unity. So far i Can see my "hands" and it does Recognise my triggers, which is all it needs to.

Does Anyone know how to make it Pause when I press the Trigger and then Start when I press it again ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class Viveinput : MonoBehaviour
{
[SteamVR_DefaultAction("Squeeze")]
public SteamVR_Action_Single squeezeAction;
public bool paused;

void Update () {
    if (SteamVR_Input._default.inActions.GrabPinch.GetLastStateUp(SteamVR_Input_Sources.Any))
    {
        print(" Grab Pinch Up");
    }
    float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);

    if (triggerValue > 00f)
    {
        print(triggerValue);
    }

}
}

This is What I am using atm for connection between controller and Unity.

Upvotes: 1

Views: 335

Answers (1)

Antoine Thiry
Antoine Thiry

Reputation: 2442

I assume that your video is playing on a VideoPlayer MonoBehaviour :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class Viveinput : MonoBehaviour
{
public VideoPlayer video;

[SteamVR_DefaultAction("Squeeze")]
public SteamVR_Action_Single squeezeAction;
private bool _triggered = false;

void Update () {
    if (SteamVR_Input._default.inActions.GrabPinch.GetLastStateUp(SteamVR_Input_Sources.Any))
    {
        print(" Grab Pinch Up");
    }
    float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);

    if (triggerValue > 0f && !_triggered)
    {
        _triggered = true; // This will prevent the following code to be executed each frames when pressing the trigger.
        if(!video.isPlaying) { // You dont need a paused boolean as the videoplayer has a property for that.
            video.Play();
        } else {
            video.Pause();
        }
    } else {
         _triggered = false;
    }    
}
}

You need to drag and drop the VideoPlayer in the editor and it should be it.

Upvotes: 1

Related Questions