- Home /
Trouble getting input from Vive controller touchpad
So I'm trying to get the position that a player is pressing down on on the Vive controller but I am getting back 0s. My code is
if(ControllerDevices[activeIdx].GetPress(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad))
{
Vector2 touch = ControllerDevices[activeIdx].GetAxis();
Debug.Log(Time.time + " touch " + touch);
}
When I press down the edge of the touchpad, I get (0,0) as output, which doesn't make any sense. Any other Vive devs have a better way of getting the position that's being touched on the touchpad?
Answer by Mmmpies · Jun 19, 2016 at 08:12 AM
I guess you'd use have to tell it to get the axis of the touchpad
This works with SteamVR for a simple movement script...
using UnityEngine;
using System.Collections;
using Valve.VR;
public class myTouchpad : MonoBehaviour
{
public GameObject player;
SteamVR_Controller.Device device;
SteamVR_TrackedObject controller;
Vector2 touchpad;
private float sensitivityX = 1.5F;
private Vector3 playerPos;
void Start()
{
controller = gameObject.GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void Update()
{
device = SteamVR_Controller.Input((int)controller.index);
//If finger is on touchpad
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
//Read the touchpad values
touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
// Handle movement via touchpad
if (touchpad.y > 0.2f || touchpad.y < -0.2f) {
// Move Forward
player.transform.position -= player.transform.forward * Time.deltaTime * (touchpad.y * 5f);
// Adjust height to terrain height at player positin
playerPos = player.transform.position;
playerPos.y = Terrain.activeTerrain.SampleHeight (player.transform.position);
player.transform.position = playerPos;
}
// handle rotation via touchpad
if (touchpad.x > 0.3f || touchpad.x < -0.3f) {
player.transform.Rotate (0, touchpad.x * sensitivityX, 0);
}
//Debug.Log ("Touchpad X = " + touchpad.x + " : Touchpad Y = " + touchpad.y);
}
}
}
The bit you need to concentrate on is this line...
touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
The default parameter to GetAxis is EVRButtonId.k_EButton_S$$anonymous$$mVR_Touchpad. The GetAxis is sometimes returning a value when my finger is on the touchpad, but most of the time it's not.
Odd I am getting a few 0.0, 0.0 results but far outweighed by actual results.
using UnityEngine;
using System.Collections;
using Valve.VR;
public class TouchpadInput : $$anonymous$$onoBehaviour {
private S$$anonymous$$mVR_Controller.Device device;
public S$$anonymous$$mVR_TrackedObject controller;
private int activeIdx;
// Use this for initialization
void Start () {
device = S$$anonymous$$mVR_Controller.Input((int)controller.index);
}
// Update is called once per frame
void Update () {
if(device.GetPress(Valve.VR.EVRButtonId.k_EButton_S$$anonymous$$mVR_Touchpad))
{
Vector2 touch = device.GetAxis();
Debug.Log(Time.time + " touch " + touch);
}
}
}
Looks like it was a firmware issue. After updating my controller firmware, it is tracking the touchpad pretty accurately.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Manipulating the location of the VR controllers (Vive/SteamVR) in script 0 Answers
How to detect which button a SteamVR action is bound to? 0 Answers
How to disable position and rotation measurement from VR headset gyroscope? 2 Answers