-
Notifications
You must be signed in to change notification settings - Fork 2
/
FirstPersonController(For VR mode, touchinput).cs
executable file
·379 lines (318 loc) · 12.6 KB
/
FirstPersonController(For VR mode, touchinput).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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
[RequireComponent(typeof (AudioSource))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private bool m_IsWalking;
[SerializeField] private float m_WalkSpeed;
[SerializeField] private float m_RunSpeed;
[SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
[SerializeField] private float m_JumpSpeed;
[SerializeField] private float m_StickToGroundForce;
[SerializeField] private float m_GravityMultiplier;
[SerializeField] private MouseLook m_MouseLook;
[SerializeField] private bool m_UseFovKick;
[SerializeField] private FOVKick m_FovKick = new FOVKick();
[SerializeField] private bool m_UseHeadBob;
[SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
[SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
[SerializeField] private float m_StepInterval;
[SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
[SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground.
[SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.
private Camera m_Camera;
private bool m_Jump;
//Added by aaron;
private bool vr_Jump;
private bool vr_Jumping;
private float m_YRotation;
private Vector2 m_Input;
private Vector3 m_MoveDir = Vector3.zero;
private CharacterController m_CharacterController;
private CollisionFlags m_CollisionFlags;
private bool m_PreviouslyGrounded;
private Vector3 m_OriginalCameraPosition;
private float m_StepCycle;
private float m_NextStep;
private bool m_Jumping;
private AudioSource m_AudioSource;
public GameObject head;
private float _moveDirection; // 1 or -1
private Gyroscope vr_Gyro;
private float vr_GyroAccYAxis = 0;
public int tmp_count;
public int tmp_device;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
m_Camera = Camera.main;
m_OriginalCameraPosition = m_Camera.transform.localPosition;
m_FovKick.Setup(m_Camera);
m_HeadBob.Setup(m_Camera, m_StepInterval);
m_StepCycle = 0f;
m_NextStep = m_StepCycle/2f;
m_Jumping = false;
m_AudioSource = GetComponent<AudioSource>();
m_MouseLook.Init(transform , m_Camera.transform);
_moveDirection = 1;
tmp_count = 0;
tmp_device = 0;
}
// Update is called once per frame
private void Update()
{
RotateView();
//Debug.Log("Update time :" + Time.deltaTime);
// the jump state needs to read here to make sure it is not missed // comment by aaron, yes, quite right, I missed when putting it in FixedUpdate
if (!m_Jump)
{
vr_Gyro = Input.gyro;
vr_GyroAccYAxis = vr_Gyro.userAcceleration.y;
if (vr_GyroAccYAxis > 2 && vr_GyroAccYAxis <= 3) {
print ("jump in update over 2 acc is" + vr_GyroAccYAxis);
} else if (vr_GyroAccYAxis >= 1 && vr_GyroAccYAxis <= 2) {
print ("jump in update over 1 acc is" + vr_GyroAccYAxis);
m_Jump = true;
} else if (vr_GyroAccYAxis >= 0.1 && vr_GyroAccYAxis < 1) {
print ("jump in update over 0.1 acc is" + vr_GyroAccYAxis);
m_Jump = false;
} else if (vr_GyroAccYAxis >= 0.08 && vr_GyroAccYAxis < 0.1) {
print ("jump in update over 0.08 acc is" + vr_GyroAccYAxis);
m_Jump = false;
} else if (vr_GyroAccYAxis >= 0.05 && vr_GyroAccYAxis < 0.08) {
print ("jump in update over 0.05 acc is" + vr_GyroAccYAxis);
m_Jump = false;
} else if (vr_GyroAccYAxis >= 0.02 && vr_GyroAccYAxis < 0.05) {
print ("jump in update over 0.02 acc is" + vr_GyroAccYAxis);
m_Jump = false;
} else if (vr_GyroAccYAxis > 0 && vr_GyroAccYAxis < 0.02) {
print ("jump in update over 0, acc is" + vr_GyroAccYAxis);
m_Jump = false;
}
}
#if !MOBILE_INPUT
if (!m_Jump)
{
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
}
#endif
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
{
StartCoroutine(m_JumpBob.DoBobCycle());
PlayLandingSound();
m_MoveDir.y = 0f;
m_Jumping = false;
}
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
{
m_MoveDir.y = 0f;
}
m_PreviouslyGrounded = m_CharacterController.isGrounded;
}
private void PlayLandingSound()
{
m_AudioSource.clip = m_LandSound;
m_AudioSource.Play();
m_NextStep = m_StepCycle + .5f;
}
private void FixedUpdate()
{
float speed;
GetInput(out speed);
//Debug.Log("FixedUpdate time :" + Time.deltaTime);
// always move along the camera forward as it is the direction that it being aimed at
//Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
//For Auto walk, uncomment this line.
//m_Input.y=0.2f;
// if (m_walk == true) {
// m_Input.y = 0.2f;
// } else {
// m_Input.y = 0;
// }
//
// if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
// {
// StartCoroutine(m_JumpBob.DoBobCycle());
// PlayLandingSound();
// m_MoveDir.y = 0f;
// m_Jumping = false;
// }
// if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
// {
// m_MoveDir.y = 0f;
// }
//
// m_PreviouslyGrounded = m_CharacterController.isGrounded;
Vector3 desiredMove = head.transform.forward*m_Input.y + head.transform.right*m_Input.x;
//print (desiredMove);
// get a normal for the surface that is being touched to move along it
RaycastHit hitInfo;
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
m_MoveDir.x = desiredMove.x*speed;
m_MoveDir.z = desiredMove.z*speed;
if (m_CharacterController.isGrounded)
{
m_MoveDir.y = -m_StickToGroundForce;
if (m_Jump)
{
m_MoveDir.y = m_JumpSpeed;
PlayJumpSound();
m_Jump = false;
m_Jumping = true;
}
}
else
{
m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
ProgressStepCycle(speed);
UpdateCameraPosition(speed);
m_MouseLook.UpdateCursorLock();
}
private void PlayJumpSound()
{
m_AudioSource.clip = m_JumpSound;
m_AudioSource.Play();
}
private void ProgressStepCycle(float speed)
{
if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
{
m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
Time.fixedDeltaTime;
}
if (!(m_StepCycle > m_NextStep))
{
return;
}
m_NextStep = m_StepCycle + m_StepInterval;
PlayFootStepAudio();
}
private void PlayFootStepAudio()
{
if (!m_CharacterController.isGrounded)
{
return;
}
// pick & play a random footstep sound from the array,
// excluding sound at index 0
int n = Random.Range(1, m_FootstepSounds.Length);
m_AudioSource.clip = m_FootstepSounds[n];
m_AudioSource.PlayOneShot(m_AudioSource.clip);
// move picked sound to index 0 so it's not picked next time
m_FootstepSounds[n] = m_FootstepSounds[0];
m_FootstepSounds[0] = m_AudioSource.clip;
}
private void UpdateCameraPosition(float speed)
{
Vector3 newCameraPosition;
if (!m_UseHeadBob)
{
return;
}
if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
{
m_Camera.transform.localPosition =
m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
(speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
newCameraPosition = m_Camera.transform.localPosition;
newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
}
else
{
newCameraPosition = m_Camera.transform.localPosition;
newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
}
m_Camera.transform.localPosition = newCameraPosition;
}
private void GetInput(out float speed)
{
// Read input
//float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
//float vertical = CrossPlatformInputManager.GetAxis("Vertical");
float horizontal = 0.0f;
float vertical = 0.0f;
int i = 0;
//Touch touch;
if (Input.touchCount > 0) {
print ("touchcount" + Input.touchCount);
for (i = 0; i < Input.touchCount; i++) {
Touch touch = Input.GetTouch (i);
if (touch.phase == TouchPhase.Began) {
//print ("Touch began");
if (touch.tapCount >= 2) {
//print ("Tap count >= 2");
_moveDirection = -1.0f;
}
else {
//print ("Tap count = 2");
_moveDirection = 1.0f;
}
}
else if (touch.phase == TouchPhase.Stationary) {
vertical = 0.1f * _moveDirection;
//print ("keep touching" + vertical);
}
}
} else
vertical = 0.0f;
// Use mouse to simulate the touch screen, if user touch then move forward
// Added by Aaron 2016.07.16
if (Input.GetMouseButton (0)) {
//print ("foward");
vertical = 0.2f;
}
bool waswalking = m_IsWalking;
#if !MOBILE_INPUT
// On standalone builds, walk/run speed is modified by a key press.
// keep track of whether or not the character is walking or running
m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
#endif
// set the desired speed to be walking or running
//speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
//Modify by aaron, set speed;
speed = m_WalkSpeed * 1.5f;
m_Input = new Vector2(horizontal, vertical);
// normalize input if it exceeds 1 in combined length:
if (m_Input.sqrMagnitude > 1)
{
m_Input.Normalize();
}
// handle speed change to give an fov kick
// only if the player is going to a run, is running and the fovkick is to be used
if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
{
StopAllCoroutines();
StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
}
}
private void RotateView()
{
m_MouseLook.LookRotation (transform, m_Camera.transform);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
//dont move the rigidbody if the character is on top of it
if (m_CollisionFlags == CollisionFlags.Below)
{
return;
}
if (body == null || body.isKinematic)
{
return;
}
body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
}
}
}