public class TouchpadMovement : MonoBehaviour {
public Transform forwardDirection;
OVRPlayerController oVPC;
// Use this for initialization
void Start () {
oVPC=GetComponent<OVRPlayerController>();
OVRTouchpad.Create ();
OVRTouchpad.TouchHandler += HandleTouchHandler;
}
void HandleTouchHandler (object sender, System.EventArgs e)
{
OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
OVRTouchpad.TouchEvent touchEvent = touchArgs.TouchType;
/*if(touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
{
//TODO: Insert code here to handle a single tap. Note that there are other TouchTypes you can check for like directional swipes, but double tap is not currently implemented I believe.
}*/
switch (touchEvent) {
case OVRTouchpad.TouchEvent.SingleTap :
//Do something for Single Tap
break;
case OVRTouchpad.TouchEvent.Left :
oVPC.UpdateMovement(Vector3.left);
break;
case OVRTouchpad.TouchEvent.Right :
oVPC.UpdateMovement(Vector3.right);
break;
case OVRTouchpad.TouchEvent.Up :
oVPC.UpdateMovement(Vector3.forward);
break;
case OVRTouchpad.TouchEvent.Down :
//oVPC.UpdateMovement(Vector3.back);
break;
}
}

using UnityEngine;
using System;
/// <summary>
/// Interface class to a touchpad.
/// </summary>
public static class OVRTouchpad
{
/// <summary>
/// Touch Type.
/// </summary>
public enum TouchEvent
{
SingleTap,
Left,
Right,
Up,
Down,
};
/// <summary>
/// Details about a touch event.
/// </summary>
public class TouchArgs : EventArgs
{
public TouchEvent TouchType;
}
/// <summary>
/// Occurs when touched.
/// </summary>
public static event EventHandler TouchHandler;
/// <summary>
/// Native Touch State.
/// </summary>
enum TouchState
{
Init,
Down,
Stationary,
Move,
Up
};
static TouchState touchState = TouchState.Init;
static Vector2 moveAmount;
static float minMovMagnitude = 100.0f; // Tune this to gage between click and swipe
// mouse
static Vector3 moveAmountMouse;
static float minMovMagnitudeMouse = 25.0f;
// Disable the unused variable warning
#pragma warning disable 0414
// Ensures that the TouchpadHelper will be created automatically upon start of the scene.
static private OVRTouchpadHelper touchpadHelper =
(new GameObject("OVRTouchpadHelper")).AddComponent<OVRTouchpadHelper>();
#pragma warning restore 0414
/// <summary>
/// Add the Touchpad game object into the scene.
/// </summary>
static public void Create()
{
// Does nothing but call constructor to add game object into scene
}
static public void Update()
{
/*
// TOUCHPAD INPUT
if (Input.touchCount > 0)
{
switch(Input.GetTouch(0).phase)
{
case(TouchPhase.Began):
touchState = TouchState.Down;
// Get absolute location of touch
moveAmount = Input.GetTouch(0).position;
break;
case(TouchPhase.Moved):
touchState = TouchState.Move;
break;
case(TouchPhase.Stationary):
touchState = TouchState.Stationary;
break;
case(TouchPhase.Ended):
moveAmount -= Input.GetTouch(0).position;
HandleInput(touchState, ref moveAmount);
touchState = TouchState.Init;
break;
case(TouchPhase.Canceled):
Debug.Log( "CANCELLED\n" );
touchState = TouchState.Init;
break;
}
}
*/
// MOUSE INPUT
if (Input.GetMouseButtonDown(0))
{
moveAmountMouse = Input.mousePosition;
touchState = TouchState.Down;
}
else if (Input.GetMouseButtonUp(0))
{
moveAmountMouse -= Input.mousePosition;
HandleInputMouse(ref moveAmountMouse);
touchState = TouchState.Init;
}
}
static public void OnDisable()
{
}
/// <summary>
/// Determines if input was a click or swipe and sends message to all prescribers.
/// </summary>
static void HandleInput(TouchState state, ref Vector2 move)
{
if ((move.magnitude < minMovMagnitude) || (touchState == TouchState.Stationary))
{
//Debug.Log( "CLICK" );
}
else if (touchState == TouchState.Move)
{
move.Normalize();
// Left
if(Mathf.Abs(move.x) > Mathf.Abs (move.y))
{
if(move.x > 0.0f)
{
//Debug.Log( "SWIPE: LEFT" );
}
else
{
//Debug.Log( "SWIPE: RIGHT" );
}
}
// Right
else
{
if(move.y > 0.0f)
{
//Debug.Log( "SWIPE: DOWN" );
}
else
{
//Debug.Log( "SWIPE: UP" );
}
}
}
}
static void HandleInputMouse(ref Vector3 move)
{
if (move.magnitude < minMovMagnitudeMouse)
{
if (TouchHandler != null)
{
TouchHandler(null, new TouchArgs() { TouchType = TouchEvent.SingleTap });
}
}
else
{
move.Normalize();
// Left/Right
if (Mathf.Abs(move.x) > Mathf.Abs(move.y))
{
if (move.x > 0.0f)
{
if (TouchHandler != null)
{
TouchHandler(null, new TouchArgs () { TouchType = TouchEvent.Left });
}
}
else
{
if (TouchHandler != null)
{
TouchHandler(null, new TouchArgs () { TouchType = TouchEvent.Right });
}
}
}
// Up/Down
else
{
if (move.y > 0.0f)
{
if (TouchHandler != null)
{
TouchHandler(null, new TouchArgs () { TouchType = TouchEvent.Down });
}
}
else
{
if(TouchHandler != null)
{
TouchHandler(null, new TouchArgs () { TouchType = TouchEvent.Up });
}
}
}
}
}
}
/// <summary>
/// This singleton class gets created and stays resident in the application. It is used to
/// trap the touchpad values, which get broadcast to any listener on the "Touchpad" channel.
/// </summary>
public sealed class OVRTouchpadHelper : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
void Start()
{
// Add a listener to the OVRMessenger for testing
OVRTouchpad.TouchHandler += LocalTouchEventCallback;
}
void Update()
{
OVRTouchpad.Update();
}
public void OnDisable()
{
OVRTouchpad.OnDisable();
}
void LocalTouchEventCallback(object sender, EventArgs args)
{
var touchArgs = (OVRTouchpad.TouchArgs)args;
OVRTouchpad.TouchEvent touchEvent = touchArgs.TouchType;
switch(touchEvent)
{
case OVRTouchpad.TouchEvent.SingleTap:
//Debug.Log("SINGLE CLICK\n");
break;
case OVRTouchpad.TouchEvent.Left:
//Debug.Log("LEFT SWIPE\n");
break;
case OVRTouchpad.TouchEvent.Right:
//Debug.Log("RIGHT SWIPE\n");
break;
case OVRTouchpad.TouchEvent.Up:
//Debug.Log("UP SWIPE\n");
break;
case OVRTouchpad.TouchEvent.Down:
//Debug.Log("DOWN SWIPE\n");
break;
}
}
}

void Start ()
{
OVRTouchpad.Create();
OVRTouchpad.TouchHandler += HandleTouchHandler;
}
void HandleTouchHandler(object sender, System.EventArgs e)
{
OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Left)
{
stuff for left move
}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Right)
{
stuff for right
}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
{
Stuff for click
}
}

using UnityEngine;
using System.Collections;
public class GearVRInput : MonoBehaviour
{
static public float GetAxisX;
static public float GetAxisY;
bool mouseDown = false;
Vector3 mousePosition;
bool MovingDirectionLockedToX;
bool MovingDirectionLockedToY;
void Update()
{
if (Input.GetMouseButtonDown(0) && mouseDown == false)
{
// Initial Press
GetAxisX = GetAxisY = 0f;
MovingDirectionLockedToX = false;
MovingDirectionLockedToY = false;
mousePosition = Input.mousePosition;
mouseDown = true;
}
else if (Input.GetMouseButtonUp(0) && mouseDown)
{
// Released
GetAxisX = GetAxisY = 0f;
MovingDirectionLockedToX = false;
MovingDirectionLockedToY = false;
mouseDown = false;
}
else if (mouseDown)
{
// Detect Axis Movement
Vector3 newMousePosition = Input.mousePosition;
Vector3 deltaMousePosition = mousePosition - newMousePosition;
float x = deltaMousePosition.x > 5 ? 1f : (deltaMousePosition.x < -5 ? -1f : 0);
float y = deltaMousePosition.y > 5 ? 1f : (deltaMousePosition.y < -5 ? -1f : 0);
// which direction do we care about ?
if (MovingDirectionLockedToX == false && MovingDirectionLockedToY == false && (Mathf.Abs (x) > 0 || Mathf.Abs (y) > 0))
{
if (Mathf.Abs ( deltaMousePosition.x) > Mathf.Abs( deltaMousePosition.y))
{
MovingDirectionLockedToX= true;
}
else
{
MovingDirectionLockedToY= true;
}
}
if (MovingDirectionLockedToX && Mathf.Abs (x) > 0)
{
GetAxisX = x;
}
if (MovingDirectionLockedToY && Mathf.Abs (y) > 0)
{
GetAxisY = y;
}
mousePosition = newMousePosition;
}
}
}







using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VR = UnityEngine.VR;
/// <summary>
/// A head-tracked stereoscopic virtual reality camera rig.
/// </summary>
[ExecuteInEditMode]
public class OVRCameraRig : MonoBehaviour
{
/// <summary>
/// The left eye camera.
/// </summary>
public Camera leftEyeCamera { get; private set; }
/// <summary>
/// The right eye camera.
/// </summary>
public Camera rightEyeCamera { get; private set; }
/// <summary>
/// Provides a root transform for all anchors in tracking space.
/// </summary>
public Transform trackingSpace { get; private set; }
/// <summary>
/// Always coincides with the pose of the left eye.
/// </summary>
public Transform leftEyeAnchor { get; private set; }
/// <summary>
/// Always coincides with average of the left and right eye poses.
/// </summary>
public Transform centerEyeAnchor { get; private set; }
/// <summary>
/// Always coincides with the pose of the right eye.
/// </summary>
public Transform rightEyeAnchor { get; private set; }
/// <summary>
/// Always coincides with the pose of the left hand.
/// </summary>
public Transform leftHandAnchor { get; private set; }
/// <summary>
/// Always coincides with the pose of the right hand.
/// </summary>
public Transform rightHandAnchor { get; private set; }
/// <summary>
/// Always coincides with the pose of the tracker.
/// </summary>
public Transform trackerAnchor { get; private set; }
/// <summary>
/// Occurs when the eye pose anchors have been set.
/// </summary>
public event System.Action<OVRCameraRig> UpdatedAnchors;
private readonly string trackingSpaceName = "TrackingSpace";
private readonly string trackerAnchorName = "TrackerAnchor";
private readonly string eyeAnchorName = "EyeAnchor";
private readonly string handAnchorName = "HandAnchor";
private readonly string legacyEyeAnchorName = "Camera";
#if UNITY_ANDROID && !UNITY_EDITOR
bool correctedTrackingSpace = false;
#endif
#region Unity Messages
private void Awake()
{
EnsureGameObjectIntegrity();
}
private void Start()
{
EnsureGameObjectIntegrity();
if (!Application.isPlaying)
return;
UpdateAnchors();
}
private void Update()
{
EnsureGameObjectIntegrity();
if (!Application.isPlaying)
return;
UpdateAnchors();
#if UNITY_ANDROID && !UNITY_EDITOR
if (!correctedTrackingSpace)
{
//HACK: Unity 5.1.1p3 double-counts the head model on Android. Subtract it off in the reference frame.
var headModel = new Vector3(0f, OVRManager.profile.eyeHeight - OVRManager.profile.neckHeight, OVRManager.profile.eyeDepth);
var eyePos = -headModel + centerEyeAnchor.localRotation * headModel;
if ((eyePos - centerEyeAnchor.localPosition).magnitude > 0.01f)
{
trackingSpace.localPosition = trackingSpace.localPosition - 2f * (trackingSpace.localRotation * headModel);
correctedTrackingSpace = true;
}
}
#endif
}
#endregion
private void UpdateAnchors()
{
bool monoscopic = OVRManager.instance.monoscopic;
OVRPose tracker = OVRManager.tracker.GetPose(0d);
trackerAnchor.localRotation = tracker.orientation;
centerEyeAnchor.localRotation = VR.InputTracking.GetLocalRotation(VR.VRNode.CenterEye);
leftEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : VR.InputTracking.GetLocalRotation(VR.VRNode.LeftEye);
rightEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : VR.InputTracking.GetLocalRotation(VR.VRNode.RightEye);
leftHandAnchor.localRotation = OVRInput.GetLocalHandRotation(OVRInput.Hand.Left);
rightHandAnchor.localRotation = OVRInput.GetLocalHandRotation(OVRInput.Hand.Right);
trackerAnchor.localPosition = tracker.position;
centerEyeAnchor.localPosition = VR.InputTracking.GetLocalPosition(VR.VRNode.CenterEye);
leftEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : VR.InputTracking.GetLocalPosition(VR.VRNode.LeftEye);
rightEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : VR.InputTracking.GetLocalPosition(VR.VRNode.RightEye);
leftHandAnchor.localPosition = OVRInput.GetLocalHandPosition(OVRInput.Hand.Left);
rightHandAnchor.localPosition = OVRInput.GetLocalHandPosition(OVRInput.Hand.Right);
if (UpdatedAnchors != null)
{
UpdatedAnchors(this);
}
}
public void EnsureGameObjectIntegrity()
{
if (trackingSpace == null)
trackingSpace = ConfigureRootAnchor(trackingSpaceName);
if (leftEyeAnchor == null)
leftEyeAnchor = ConfigureEyeAnchor(trackingSpace, VR.VRNode.LeftEye);
if (centerEyeAnchor == null)
centerEyeAnchor = ConfigureEyeAnchor(trackingSpace, VR.VRNode.CenterEye);
if (rightEyeAnchor == null)
rightEyeAnchor = ConfigureEyeAnchor(trackingSpace, VR.VRNode.RightEye);
if (leftHandAnchor == null)
leftHandAnchor = ConfigureHandAnchor(trackingSpace, OVRPlugin.Node.LeftHand);
if (rightHandAnchor == null)
rightHandAnchor = ConfigureHandAnchor(trackingSpace, OVRPlugin.Node.RightHand);
if (trackerAnchor == null)
trackerAnchor = ConfigureTrackerAnchor(trackingSpace);
if (leftEyeCamera == null || rightEyeCamera == null)
{
Camera centerEyeCamera = centerEyeAnchor.GetComponent<Camera>();
if (centerEyeCamera == null)
{
centerEyeCamera = centerEyeAnchor.gameObject.AddComponent<Camera>();
}
// Only the center eye camera should now render.
var cameras = gameObject.GetComponentsInChildren<Camera>();
for (int i = 0; i < cameras.Length; i++)
{
Camera cam = cameras[i];
if (cam == centerEyeCamera)
continue;
if (cam && (cam.transform == leftEyeAnchor || cam.transform == rightEyeAnchor) && cam.enabled)
{
Debug.LogWarning("Having a Camera on " + cam.name + " is deprecated. Disabling the Camera. Please use the Camera on " + centerEyeCamera.name + " instead.");
cam.enabled = false;
// Use "MainCamera" if the previous cameras used it.
if (cam.CompareTag("MainCamera"))
centerEyeCamera.tag = "MainCamera";
}
}
leftEyeCamera = centerEyeCamera;
rightEyeCamera = centerEyeCamera;
}
}
private Transform ConfigureRootAnchor(string name)
{
Transform root = transform.Find(name);
if (root == null)
{
root = new GameObject(name).transform;
}
root.parent = transform;
root.localScale = Vector3.one;
root.localPosition = Vector3.zero;
root.localRotation = Quaternion.identity;
return root;
}
private Transform ConfigureEyeAnchor(Transform root, VR.VRNode eye)
{
string eyeName = (eye == VR.VRNode.CenterEye) ? "Center" : (eye == VR.VRNode.LeftEye) ? "Left" : "Right";
string name = eyeName + eyeAnchorName;
Transform anchor = transform.Find(root.name + "/" + name);
if (anchor == null)
{
anchor = transform.Find(name);
}
if (anchor == null)
{
string legacyName = legacyEyeAnchorName + eye.ToString();
anchor = transform.Find(legacyName);
}
if (anchor == null)
{
anchor = new GameObject(name).transform;
}
anchor.name = name;
anchor.parent = root;
anchor.localScale = Vector3.one;
anchor.localPosition = Vector3.zero;
anchor.localRotation = Quaternion.identity;
return anchor;
}
private Transform ConfigureHandAnchor(Transform root, OVRPlugin.Node hand)
{
string handName = (hand == OVRPlugin.Node.LeftHand) ? "Left" : "Right";
string name = handName + handAnchorName;
Transform anchor = transform.Find(root.name + "/" + name);
if (anchor == null)
{
anchor = transform.Find(name);
}
if (anchor == null)
{
anchor = new GameObject(name).transform;
}
anchor.name = name;
anchor.parent = root;
anchor.localScale = Vector3.one;
anchor.localPosition = Vector3.zero;
anchor.localRotation = Quaternion.identity;
return anchor;
}
private Transform ConfigureTrackerAnchor(Transform root)
{
string name = trackerAnchorName;
Transform anchor = transform.Find(root.name + "/" + name);
if (anchor == null)
{
anchor = new GameObject(name).transform;
}
anchor.parent = root;
anchor.localScale = Vector3.one;
anchor.localPosition = Vector3.zero;
anchor.localRotation = Quaternion.identity;
return anchor;
}
}

