-
Notifications
You must be signed in to change notification settings - Fork 0
/
RightControllerInput.cs
94 lines (73 loc) · 2.65 KB
/
RightControllerInput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Zoilo Mercedes
* Right Controller Input Manager
*/
using UnityEngine;
using System;
public class RightControllerInput : MonoBehaviour {
// controller references
SteamVR_Controller.Device controller;
SteamVR_TrackedObject trackedObj;
// testing pane with touchpad
GameObject objectMenu;
private float touchLast;
private float touchCurrent;
private float distance;
public float swipeSpeed = 50;
// private bool hasSwipedLeft;
// private bool hasSwipedRight;
void Awake () {
trackedObj = GetComponent<SteamVR_TrackedObject>();
objectMenu = transform.GetChild(1).gameObject;
}
void Update () {
controller = SteamVR_Controller.Input((int)trackedObj.index);
// right hand functions
if(controller.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
Debug.Log("right touched!");
// setting initial touch position for swiping
if(controller.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
touchLast = controller.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).x;
// activate/deactivate objectMenu on touchpad press
if(controller.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)){
Debug.Log("right tp pressed down!");
objectMenu.SetActive(!objectMenu.activeSelf);
}
// if objectMenu is active and player is touching touchpad,
// rotate the objectMenu based on swiping left/right on touchpad.
if(objectMenu.activeSelf && controller.GetTouch(SteamVR_Controller.ButtonMask.Touchpad)){
touchCurrent = controller.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).x;
distance = touchCurrent - touchLast;
touchLast = touchCurrent;
objectMenu.transform.Rotate(Vector3.forward * distance * swipeSpeed);
}
}
public float throwForce = 1.5f;
void OnTriggerStay(Collider col){
if(col.gameObject.CompareTag("grabable")){
if(controller.GetPressUp(SteamVR_Controller.ButtonMask.Grip))
ThrowObject(col);
else if(controller.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
GrabObject(col);
}
}
void GrabObject(Collider coll){
coll.transform.SetParent(gameObject.transform); // make controller parent
coll.GetComponent<Rigidbody>().isKinematic = true; // turn off physics
controller.TriggerHapticPulse(2000); // vibrate controller
Debug.Log("Grabbing object!");
}
void ThrowObject(Collider coll){
coll.transform.SetParent(null);
Rigidbody rigidBody = coll.GetComponent<Rigidbody>();
rigidBody.isKinematic = false;
rigidBody.velocity = controller.velocity * throwForce;
rigidBody.angularVelocity = controller.angularVelocity;
Debug.Log("Released object!");
}
// void ThrowObject(){
// }
// void GrabObject(){
// }
// void PlaceObject(){
// }
}