-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerMovement.cs
More file actions
188 lines (150 loc) · 4.84 KB
/
ControllerMovement.cs
File metadata and controls
188 lines (150 loc) · 4.84 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class ControllerMovement : MonoBehaviour
{
public float defaultSpeed = 0;
[SerializeField, Range(5f, 100f)]
public float maxSpeed = 50f;
public float alphaFactor = 0.001f;
public bool HMDBased = false;
public bool activate = true;
public Foveate cameraL;
public Foveate cameraR;
public Transform cameraTransform;
[Header("Hack")]
public bool isAutoMove = false;
private float currentSpeed = 0f;
private float acceleration = 0f;
private bool isCollided = false;
private InputDevice leftDevice;
private InputDevice helmet;
private CharacterController cc;
void GetController()
{
List<InputDevice> devices = new List<InputDevice>();
//get helmet and controller
InputDeviceCharacteristics inputDevices = (InputDeviceCharacteristics.Left | InputDeviceCharacteristics.Controller) & InputDeviceCharacteristics.HeadMounted;
InputDevices.GetDevicesWithCharacteristics(inputDevices, devices);
if (devices.Count > 1)
{
//Debug.Log("count: " + devices.Count);
helmet = devices[0];
leftDevice = devices[1];
}
}
// Start is called before the first frame update
void Start()
{
GetController();
cc = GetComponent<CharacterController>();
currentSpeed = defaultSpeed;
UpdateFoveationAlpha();
}
private void UpdateFoveationAlpha()
{
if (cameraL.foveationAlpha < 0.08f)
{
cameraL.foveationAlpha = currentSpeed * alphaFactor;
cameraR.foveationAlpha = currentSpeed * alphaFactor;
if (cameraL.noFoveation)
{
cameraL.foveationAlpha = 0f;
cameraR.foveationAlpha = 0f;
}
}
//Debug.Log("current alpha:" + cameraL.foveationAlpha);
}
private void UpdateDarkScale()
{
if (currentSpeed > defaultSpeed)
{
cameraL.darkScale = (1 - currentSpeed / maxSpeed) * maxSpeed + (currentSpeed / maxSpeed) * 20;
cameraR.darkScale = (1 - currentSpeed / maxSpeed) * maxSpeed + (currentSpeed / maxSpeed) * 20;
}
//if (currentSpeed < defaultSpeed)
//{
// cameraL.darkScale = maxSpeed;
// cameraR.darkScale = maxSpeed;
//}
}
private void UpdateSpeed()
{
// speed state
currentSpeed += acceleration * Time.deltaTime;
//Debug.Log("current speed: " + currentSpeed);
if (currentSpeed > maxSpeed)
{
currentSpeed = maxSpeed;
}
if (currentSpeed < defaultSpeed && !isCollided)
{
currentSpeed = defaultSpeed;
}
if (isCollided)
{
currentSpeed = 0f;
isCollided = false;
}
}
private void ResetValues()
{
cameraL.darkScale = maxSpeed;
cameraR.darkScale = maxSpeed;
currentSpeed = defaultSpeed;
cameraL.foveationAlpha = 0.0f;
cameraR.foveationAlpha = 0.0f;
}
// Update is called once per frame
void FixedUpdate()
{
if (activate)
{
if (!leftDevice.isValid || !helmet.isValid)
{
GetController();
}
leftDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerVal);
leftDevice.TryGetFeatureValue(CommonUsages.gripButton, out bool isClicked);
if (isClicked)
{
isAutoMove = !isAutoMove;
}
UpdateFoveationAlpha();
if (isAutoMove)
{
// Avoid flying to the sky
Vector3 transformXZ = cameraTransform.forward;
transformXZ.y = 0f;
// Switch orientation control between HMD and controller
cc.Move(currentSpeed * Time.deltaTime * (HMDBased ? transformXZ : -transform.forward));
if (triggerVal > 0.1f)
{
acceleration = 2f;
}
else
{
acceleration = -3f;
}
UpdateSpeed();
UpdateDarkScale();
}
else
{
ResetValues();
}
}
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (isAutoMove)
{
if(hit.collider.tag == "Wall")
{
//Debug.Log("hit from controller");
isCollided = true;
}
}
}
}