Last active
May 14, 2016 22:30
-
-
Save haydenjameslee/c0d3cb88434cd74ec30dfec6e2714eb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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