-
Notifications
You must be signed in to change notification settings - Fork 1
/
ControllerControlController.cs
135 lines (109 loc) · 5.02 KB
/
ControllerControlController.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
using UnityEngine;
using UnityEngine.InputSystem;
/**
* simulate VR devices with a game controller
*
* consists of 2 scripts:
*
* ControllerControlController (YOU ARE HERE!)
* Manages the inputs and maps them to the different devices to control: headset, left and right controller.
*
* ControllerControl
* One for each device: headset, left and right controller.
* Manages the commands from the ControllerControlController to simulate position and rotation changes as well as button presses.
*
*/
namespace David.Controller.ControllerControl
{
public enum WhatToControl { Headset = 0, LeftController = 1, RightController = 2 };
public class ControllerControlController : MonoBehaviour
{
public GameObject headset;
public GameObject leftController;
public GameObject rightController;
public GameObject baseController;
public float moveSpeed = 5;
public float rotationSpeed = 5;
public WhatToControl whatToControl = 0;
private Gamepad gamepad;
private bool leftShoulderPressed = false;
private bool rightShoulderPressed = false;
// Start is called before the first frame update
void Start()
{
// ToDo: Check if the ControllerControl script is attached and correctly configured OR add them automatically.
gamepad = Gamepad.current;
// move camera (and base) to "standing" position
headset.transform.Translate(new Vector3(0, 1.75f, 0));
baseController.transform.Translate(new Vector3(0, 1.75f, 0));
// move controllers in front of camera
rightController.transform.Translate(new Vector3(0.2f, -0.1f, 0.5f));
leftController.transform.Translate(new Vector3(-0.2f, -0.1f, 0.5f));
}
// Update is called once per frame
void Update()
{
if (gamepad == null)
return; // No gamepad connected.
// iterate whatToControl with shoulder buttons
{
if (gamepad.leftShoulder.wasPressedThisFrame)
{
if (!leftShoulderPressed)
{
// previous WhatToControl
int wTCtmp = (((int)whatToControl) - 1);
if (wTCtmp < 0) { wTCtmp = 2; }
whatToControl = (WhatToControl)wTCtmp;
leftShoulderPressed = true;
}
}
if (gamepad.leftShoulder.wasReleasedThisFrame)
{
leftShoulderPressed = false;
}
if (gamepad.rightShoulder.wasPressedThisFrame)
{
if (!rightShoulderPressed)
{
// previous WhatToControl
int wTCtmp = (((int)whatToControl) + 1);
if (wTCtmp > 2) { wTCtmp = 0; }
whatToControl = (WhatToControl)wTCtmp;
rightShoulderPressed = true;
}
}
if (gamepad.rightShoulder.wasReleasedThisFrame)
{
rightShoulderPressed = false;
}
}
Transform transform = headset.transform;
if (whatToControl == WhatToControl.LeftController) { transform = leftController.transform; }
if (whatToControl == WhatToControl.RightController) { transform = rightController.transform; }
Vector2 dpad = gamepad.dpad.ReadValue();
Vector2 leftStick = gamepad.leftStick.ReadValue();
Vector2 rightStick = gamepad.rightStick.ReadValue();
float tilt = -dpad.x;
float yMove = dpad.y;
float xMove = leftStick.x;
float zMove = leftStick.y;
float rotateHorizontal = rightStick.x;
float rotateVertical = -rightStick.y;
Quaternion quaternion = Quaternion.AngleAxis(tilt, transform.right);
transform.Translate(new Vector3(xMove, yMove, zMove) * moveSpeed * Time.deltaTime);
transform.RotateAround(Vector3.up, rotateHorizontal * rotationSpeed * Time.deltaTime);
transform.RotateAround(transform.right, rotateVertical * rotationSpeed * Time.deltaTime);
transform.RotateAround(transform.forward, tilt * rotationSpeed * Time.deltaTime);
// in case you move main, also move controllers (via base controller parent)
if (whatToControl == 0)
{
transform = baseController.transform;
transform.Translate(new Vector3(xMove, yMove, zMove) * moveSpeed * Time.deltaTime);
transform.RotateAround(Vector3.up, rotateHorizontal * rotationSpeed * Time.deltaTime);
transform.RotateAround(transform.right, rotateVertical * rotationSpeed * Time.deltaTime);
transform.RotateAround(transform.forward, tilt * rotationSpeed * Time.deltaTime);
}
}
}
}