Skip to content

Instantly share code, notes, and snippets.

@haydenjameslee
Last active May 14, 2016 22:30
Show Gist options
  • Save haydenjameslee/c0d3cb88434cd74ec30dfec6e2714eb6 to your computer and use it in GitHub Desktop.
Save haydenjameslee/c0d3cb88434cd74ec30dfec6e2714eb6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class ViveTouchpadScroll : MonoBehaviour
{
Vector2 scrollDelta = Vector2.zero;
Vector2 lastTouchpadPosition = Vector2.zero;
SteamVR_Controller.Device _controller;
SteamVR_Controller.Device controller
{
get
{
if (_controller == null)
{
// Be careful if this index changes while the controller is cached
var index = GetComponent<SteamVR_TrackedObject>().index;
_controller = SteamVR_Controller.Input((int)index);
}
return _controller;
}
}
void Update()
{
if (controller.GetTouchDown(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
lastTouchpadPosition = GetTouchPosition();
}
if (controller.GetTouch(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
var touchPosition = GetTouchPosition();
float horizDelta = (touchPosition.x - lastTouchpadPosition.x) / Time.deltaTime;
float vertDelta = (touchPosition.y - lastTouchpadPosition.y) / Time.deltaTime;
scrollDelta = new Vector2(horizDelta, vertDelta);
lastTouchpadPosition = touchPosition;
}
else
{
scrollDelta = Vector2.zero;
}
}
Vector2 GetTouchPosition()
{
return controller.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
}
public float GetHorizontalScrollDelta()
{
return scrollDelta.x;
}
public float GetVerticalScrollDelta()
{
return scrollDelta.y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment