/
ControllerInputManager.cs
214 lines (181 loc) · 7.76 KB
/
ControllerInputManager.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerInputManager : MonoBehaviour {
private SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device device;
// Choose Hand
[SerializeField]
private bool isLeftHand;
// Force applied to thrown objects
private float throwForce = 1.5f;
// Menu / Swipping mechanism
[SerializeField]
private ObjectMenuManager objectMenu;
private float swipeSum;
private float touchLast;
private float touchCurrent;
private float distance;
private bool hasSwipedLeft;
private bool hasSwipedRight;
// Teleporter
[SerializeField]
private GameObject player;
[SerializeField]
private Ball ball; // Used to call associated methods to prevent cheating
[SerializeField]
private LayerMask laserMask; // Where you can teleport to
[SerializeField]
private LayerMask invalidLaserMask; // Where you cannot teleport to
[SerializeField]
private GameObject teleportAimerObject; // teleport cylinder
private LineRenderer laser; // laser pointer
private Vector3 teleportLocation; // teleport 3D position
private string playArea = "PlayArea";
private float yNudgeAmount = 1.5f; // specific to teleportAimerObject height
private int maxDistance = 5;
private RaycastHit hit;
// Use this for initialization
void Start() {
trackedObj = GetComponent<SteamVR_TrackedObject>();
laser = GetComponentInChildren<LineRenderer>();
}
// Update is called once per frame
void Update() {
device = SteamVR_Controller.Input((int)trackedObj.index);
// Left hand functionality - teleportation
if (isLeftHand) {
if (device.GetPress(SteamVR_Controller.ButtonMask.Grip)) {
laser.gameObject.SetActive(true);
teleportAimerObject.gameObject.SetActive(true);
laser.SetPosition(0, transform.position); // start laser from hand controller
if (Physics.Raycast(transform.position, transform.forward, out hit, maxDistance, laserMask)) {
teleportLocation = hit.point;
laser.SetPosition(1, teleportLocation);
// aimer position
teleportAimerObject.transform.position = new Vector3(teleportLocation.x, teleportLocation.y + yNudgeAmount, teleportLocation.z);
}
else {
// teleportLocation = new Vector3(transform.forward.x * 15 + transform.position.x, transform.forward.y * 15 + transform.position.y, transform.forward.z * 15 + transform.position.z);
Vector3 tempTeleportLocation = transform.position + (transform.forward * maxDistance);
if (Physics.Raycast(tempTeleportLocation, -Vector3.up, out hit, maxDistance, laserMask)) {
teleportLocation = new Vector3(transform.forward.x * maxDistance + transform.position.x, hit.point.y, transform.forward.z * maxDistance + transform.position.z);
}
laser.SetPosition(1, transform.forward * maxDistance + transform.position);
// aimer position
teleportAimerObject.transform.position = teleportLocation + new Vector3(0, 0, 0);
}
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Grip)) {
laser.gameObject.SetActive(false);
teleportAimerObject.gameObject.SetActive(false);
player.transform.position = teleportLocation;
// Prevent cheating - disabled the ball when player is not on platform, enable otherwise
if (hit.transform.gameObject.tag != playArea) {
ball.DisableBall();
}
else {
ball.EnableBall();
}
}
}
// Right hand functionality - Menu
if (!isLeftHand) {
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad)) {
touchLast = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).x;
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad)) {
EnableMenu();
touchCurrent = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).x; // get location of thumb with respect to x-axis
distance = touchCurrent - touchLast;
touchLast = touchCurrent; // cache our position for the next frame
swipeSum += distance;
if (!hasSwipedRight) {
if (swipeSum > 0.5f) {
swipeSum = 0;
SwipeRight();
hasSwipedRight = true;
hasSwipedLeft = false;
}
}
if (!hasSwipedLeft) {
if (swipeSum < -0.5f) {
swipeSum = 0;
SwipeLeft();
hasSwipedLeft = true;
hasSwipedRight = false;
}
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
// Spawn object currently selected by menu
SpawnObject();
}
}
// Restart variables when finger is lifted
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Touchpad)) {
swipeSum = 0;
touchCurrent = 0;
touchLast = 0;
hasSwipedLeft = false;
hasSwipedRight = false;
DisableMenu();
}
}
}
// Physics frames
private void OnTriggerStay(Collider col) {
if (col.gameObject.CompareTag("Throwable")) {
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
GrabObject(col);
}
else if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) {
ThrowObject(col);
}
}
if (col.gameObject.CompareTag("Structure")) {
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
GrabObject(col);
}
else if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) {
PlaceObject(col);
}
}
}
// Physics methods for interacting with objects
void GrabObject(Collider col) {
col.transform.SetParent(gameObject.transform); // attach the object to our controller
col.GetComponent<Rigidbody>().isKinematic = true; // turn off physics
device.TriggerHapticPulse(8000); // controller vibration
Debug.Log("You are touching down the trigger on an object.");
}
void PlaceObject(Collider col) {
col.transform.parent = null;
}
void ThrowObject(Collider col) {
col.transform.SetParent(null); // detach object from controller
Rigidbody rigidBody = col.GetComponent<Rigidbody>();
rigidBody.isKinematic = false; // turn on physics
// Set velocity based on controller movement
rigidBody.velocity = device.velocity * throwForce;
rigidBody.angularVelocity = device.angularVelocity;
Debug.Log("You have released the trigger");
}
// Menu interaction methods
void SpawnObject() {
objectMenu.SpawnCurrentObject();
}
void SwipeLeft() {
objectMenu.MenuLeft();
Debug.Log("SwipeLeft");
}
void SwipeRight() {
objectMenu.MenuRight();
Debug.Log("SwipeRight");
}
void EnableMenu() {
objectMenu.gameObject.SetActive(true);
}
void DisableMenu() {
objectMenu.gameObject.SetActive(false);
}
}