TryGetFeatureValue always 0 for Unity XR Input's CommonUsages.trigger - unity3d

I am developing a VR game in Unity (2020.3.15f2) using the XR Interaction Toolkit package (1.0.0-pre.5) for my Oculus Quest 2. At this stage in my development, I am trying to recognize presses to the trigger and grip buttons on the controllers respectively in order to animate some 3D hand models. Here's the script I've written to accomplish this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class HandPresence : MonoBehaviour {
public InputDeviceCharacteristics controllerCharacteristics;
public GameObject handModelPrefab;
private InputDevice targetDevice;
private GameObject spawnedHandModel;
private Animator handAnimator;
void Start() {
TryInitialize();
}
void TryInitialize() {
List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);
if (devices.Count > 0) {
targetDevice = devices[0];
spawnedHandModel = Instantiate(handModelPrefab, transform);
handAnimator = spawnedHandModel.GetComponent<Animator>();
}
}
void UpdateHandAnimation() {
if (targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue)) {
handAnimator.SetFloat("Trigger", triggerValue);
} else {
handAnimator.SetFloat("Trigger", 0);
}
if (targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue)) {
handAnimator.SetFloat("Grip", gripValue);
} else {
handAnimator.SetFloat("Grip", 0);
}
}
void Update()
{
if (!targetDevice.isValid) {
TryInitialize();
} else {
spawnedHandModel.SetActive(true);
UpdateHandAnimation();
}
}
}
The issue I'm experiencing is that the values of both triggerValue and gripValue are always 0. The value of targetDevice looks fine. I also tried using triggerButton, gripButton, primaryButton, etc. and they are always 0/false as well. The hand models show up just fine and their movement is in sync with the movement of the controllers, but they just don't seem to want to register any button presses.
I've been stuck on this one for hours and would very much appreciate any insight, thank you!

Is your project setup with the (new) Input System? I have no problem detecting there trigger and grip values.
Also make sure the targetDevice actually uses trigger and grip features, maybe it is another device such as the HMD.

Related

I'm using Unity and creating a text based game where your options directly affect the health and need assistance

I'm trying to add a healthbar which is affected by the choices made and link the two but am unable to do so. Any help would be appreciated.
I will recommend you that before you go any further with your game development process, you research a bit more about general OOP programming (no offense or anything, it will just make things waaay easier, trust me).
That being said, here's an example to steer you to the right direction:
You can create a script with a global variable called health and subtract it every time the player makes a decision that would "punish" them, here's an example of how that could work:
PlayerManager.cs (put this script on your player)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour {
public float health = 100f;
private void Update() {
if (health < 0)
Die();
}
private void Die () {
//Fancy animations and text can be added on this method
Destroy(gameObject);
}
}
And on your dialogue script, once they choose a wrong answer, then you can just decrease health on the proper player manager instance, like this:
DialogueSystem.cs
public PlayerManager playerManager;
public float damage = 10f;
private void Start() {
//You can initialize other stuff here as well
//This line is assuming you have the dialogue system script attach to your player as well
playerManager = GetComponent<PlayerManager>();
}
private void Update () {
//This is obviously going to change depending on how your system is built
if (wrongChoice) {
playerManager.health -= damage;
wrongChoice = false;
}
}

GVRReticlePointer not working properly with onClick Events

I followed this tutorial and done everything that was mentioned, everything looked and worked fine except the reticle point expands and contracts when I gaze upon the cube but the click event does not trigger. Any help would be greatly appreciated.
Code for random teleport is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ReticleTest : MonoBehaviour
{
public void RandomlyTeleport()
{
Debug.Log("reached here");
gameObject.transform.position = new Vector3(
GetRandomCoordinate(), Random.Range(0.5f, 2), GetRandomCoordinate());
}
private float GetRandomCoordinate()
{
var coordinate = Random.Range(-7,7);
while( coordinate > -1.5 && coordinate < 1.5)
{
coordinate = Random.Range(-5,5);
}
return coordinate;
}
}
These are screenshots
Update :
It turns out that the same thing is happening when I bring up the scene that comes premade with SDK HelloVR, although the hexagon(the only interactive thing in the scene) changes color when I gaze at it, nothing else happens when I click it. So, this is not a problem with what I made but the inherent problem of unity or the SDK

Unity SteamVR Actions not Working in Build

I am trying to switch to another scene when the headset is removed. It is already working in the Unity editor, however, not in the build.
Setup:
Unity 2018.3.6f1
SteamVR Unity Plugin v.2.2.0
Vive Pro
This code is working in the editor:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using Valve.VR;
public class EndGame : MonoBehaviour
{
[Tooltip("This action lets you know when the player has placed the headset on their head")]
public SteamVR_Action_Boolean headsetOnHead = SteamVR_Input.GetBooleanAction("HeadsetOnHead");
void Update()
{
if (SteamVR.initializedState != SteamVR.InitializedStates.InitializeSuccess)
{
return;
}
if (headsetOnHead != null)
{
if (headsetOnHead.GetStateDown(SteamVR_Input_Sources.Head))
{
StopCoroutine(RestartGame());
}
else if (headsetOnHead.GetStateUp(SteamVR_Input_Sources.Head))
{
StartCoroutine(RestartGame());
}
}
}
IEnumerator RestartGame()
{
yield return new WaitForSecondsRealtime(3);
SceneManager.LoadScene("Startscene", LoadSceneMode.Single);
yield return null;
}
}
In the Editor \actions\default\in\HeadsetOnHead is referenced to public SteamVR_Action_Boolean headsetOnHead.
The actions.json (including /actions/default/in/HeadsetOnHead) is in the build folder and the logs don't show any errors.
Switching to SteamVR_LoadLevel instead of scene management solved the issue for the current Unity Version.
Also, the script was attached to the SteamVR Player Prefab in the scene. Now it's placed on a seperate gameobject in the scene, to avoid issues with dont distroy on load with the Player prefab.

Unity | webCamScript not displaying

I have the code exact from a tutorial I copied, and the webcam is inserted and works. But when I load the Unity game (In Unity Editor) there is no "No Device connected" error or incorrect scripts. I'm confused as to why it isn't working.
Why isn't it being displayed?
My webCamScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class webCamScript : MonoBehaviour {
public GameObject webCameraPlane;
// Use this for initialization
void Start () {
if (Application.isMobilePlatform) {
GameObject cameraParent = new GameObject ("camParent");
cameraParent.transform.position = this.transform.position;
this.transform.parent = cameraParent.transform;
cameraParent.transform.Rotate (Vector3.right, 90);
}
Input.gyro.enabled = true;
WebCamTexture webCameraTexture = new WebCamTexture ();
webCameraPlane.GetComponent<MeshRenderer> ().material.mainTexture = webCameraTexture;
webCameraTexture.Play ();
}
// Update is called once per frame
void Update () {
Quaternion cameraRotation = new Quaternion (Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.x, -Input.gyro.attitude.y);
this.transform.localRotation = cameraRotation;
}
}
Solved
I found the problem, I had a custom texture on the plane which was stopping the camera texture from being inserted.
I presume it has something to do with the fact that you have your code wrapped in an if statement that is checking to see if you are running on a mobile platform. The editor will not be classed as a mobile platform and hence that code will be ignored

Use bluetooth game controller to play/stop video in unity vr

I have created a vr app in unity using google vr sdk. Currently i could click on the screen to start the video. When i use the vr headset, i need to use the bluetooth controller to play/stop the video. Can someone help me do this?
You need to identify how unity map your controller buttons, once you identify what button do you want to press and how unity mapped in his Input Manager (Edit->Project Settings->Input) you just need to call you function from the Update like this:
void Update()
{
if(Input.GetButtonUp("Fire1"))
{
playVideoFuncion();
}
}
Where playVideoFunction() is your own fuction.
In this example I used "Fire1" but maybe in your case is different.
For example for the Xbox Controller you have this configuration explained in Xbox 360 Controller Input on Unity
If you can't find anything related on your controller you can do something like:
void Update()
{
if(Input.GetButtonUp("Fire1"))
{
Debug.Log("Fire 1 Pressed");
}
if(Input.GetButtonUp("Fire2"))
{
Debug.Log("Fire 1 Pressed");
}
if(Input.GetButtonUp("0"))
{
Debug.Log("Button 0 pressed");
}
// Add more buttons and logs
}
There are maybe other ways to identify the input from random controllers but I don't know how. I needed the mapping for the Xbox controller and that page was useful.
You can create a script and attach to the video player so that when a user clicks on the player the commands are run. I assume you already have the unity package for Google VR(GVR). Add the following sample script and modify to suit your need.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VideoPlayer : MonoBehaviour {
private bool _isPlaying;
// Use this for initialization
void Start () {
_isPlaying = false
}
// Update is called once per frame
void Update () {
if (GvrController.ClickButtonUp && _isPlaying) {
PlayVideo();
}
else if (GvrController.ClickButtonUp && !_isPlaying){
StopVideo ();
}
}
public void PlayVideo{
//Logic to Play Video
}
public void StopVideo{
//Logic to Stop Video
}
}