Guest User

Untitled

a guest
Jul 16th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using Valve.VR;
  4. using System;
  5.  
  6. public class WandController : MonoBehaviour {
  7.     private Valve.VR.EVRButtonId gripButton = Valve.VR.EVRButtonId.k_EButton_Grip; //Essentially this is accessing
  8.                                                                                    //SteamVR_TrackedObject.cs and telling our script
  9.                                                                                    //and defining it here.
  10.  
  11.  
  12.     private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
  13.     private Valve.VR.EVRButtonId Touchpad = Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad;
  14.  
  15.     private SteamVR_Controller.Device controller { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
  16.     private SteamVR_TrackedObject trackedObj;
  17.  
  18.     SteamVR_Controller.Device device;
  19.     SteamVR_TrackedObject MotionControl;
  20.  
  21.     Vector2 touchpadLoc; //Touchpad location.
  22.  
  23.     private Vector2 Up = new Vector2(0, 1);
  24.  
  25.     private GameObject pickup;
  26.  
  27.     // Use this for initialization
  28.     void Start ()
  29.     {
  30.         MotionControl = gameObject.GetComponent<SteamVR_TrackedObject>();
  31.         trackedObj = GetComponent<SteamVR_TrackedObject>();
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void Update()
  36.     {
  37.         if (controller == null)
  38.         {
  39.             Debug.Log("Controller not initalized");
  40.             return;
  41.         }
  42.  
  43.         if (controller.GetPressDown(gripButton) && pickup != null)
  44.         {
  45.             pickup.transform.parent = this.transform;
  46.             pickup.GetComponent<Rigidbody>().isKinematic = true;
  47.         }
  48.         if (controller.GetPressUp(gripButton) && pickup != null)
  49.         {
  50.             pickup.transform.parent = null;
  51.             pickup.GetComponent<Rigidbody>().isKinematic = false;
  52.             Debug.Log("Grip button was unpressed");
  53.         }
  54.  
  55.         device = SteamVR_Controller.Input((int)controller.index);
  56.         //If finger is on touchpad
  57.         if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
  58.         {
  59.             //Read the touchpad values
  60.             touchpadLoc = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
  61.             //Debug.Log(touchpadLoc);
  62.  
  63.             double targetUpXOld = 0.0; //Old target up value.
  64.             float targetUpX = (float)targetUpXOld;
  65.  
  66.             double targetUpYOld = 0.8;
  67.             float targetUpY = (float)targetUpYOld;
  68.  
  69.             Vector2 targetUp = new Vector2(targetUpX, targetUpY);
  70.  
  71.             if (Vector2.Distance(touchpadLoc, targetUp) < .15) //If the distance between finger (touchpadLoc) and the up zone (targetUp) is more than 0 do something.
  72.             {
  73.                 Debug.Log("Up position met.");
  74.             }
  75.  
  76.             double targetDownXOld = -0.0; //Old target up value.
  77.             float targetDownX = (float)targetUpXOld;
  78.  
  79.             double targetDownYOld = -0.8;
  80.             float targetDownY = (float)targetUpYOld;
  81.  
  82.             Vector2 targetDown = new Vector2(targetUpX, targetUpY);
  83.  
  84.             if (Vector2.Distance(touchpadLoc, targetDown) > .15)
  85.             {
  86.                 Debug.Log("Down position met.");
  87.             }
  88.         }
  89.     }
  90.     private void OnTriggerEnter(Collider collider)
  91.     {
  92.         pickup = collider.gameObject;
  93.     }
  94.     private void OnTriggerExit(Collider collider)
  95.     {
  96.         pickup = null;
  97.     }
  98. }
Add Comment
Please, Sign In to add comment