SnapTurn.cs
6.14 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
// Copyright (c) Valve Corporation, All rights reserved. ======================================================================================================
using UnityEngine;
using System.Collections;
namespace Valve.VR.InteractionSystem
{
//-----------------------------------------------------------------------------
public class SnapTurn : MonoBehaviour
{
public float snapAngle = 90.0f;
public bool showTurnAnimation = true;
public AudioSource snapTurnSource;
public AudioClip rotateSound;
public GameObject rotateRightFX;
public GameObject rotateLeftFX;
public SteamVR_Action_Boolean snapLeftAction = SteamVR_Input.GetBooleanAction("SnapTurnLeft");
public SteamVR_Action_Boolean snapRightAction = SteamVR_Input.GetBooleanAction("SnapTurnRight");
public bool fadeScreen = true;
public float fadeTime = 0.1f;
public Color screenFadeColor = Color.black;
public float distanceFromFace = 1.3f;
public Vector3 additionalOffset = new Vector3(0, -0.3f, 0);
public static float teleportLastActiveTime;
private bool canRotate = true;
public float canTurnEverySeconds = 0.4f;
private void Start()
{
AllOff();
}
private void AllOff()
{
if (rotateLeftFX != null)
rotateLeftFX.SetActive(false);
if (rotateRightFX != null)
rotateRightFX.SetActive(false);
}
private void Update()
{
Player player = Player.instance;
if (canRotate && snapLeftAction != null && snapRightAction != null && snapLeftAction.activeBinding && snapRightAction.activeBinding)
{
//only allow snap turning after a quarter second after the last teleport
if (Time.time < (teleportLastActiveTime + canTurnEverySeconds))
return;
// only allow snap turning when not holding something
bool rightHandValid = player.rightHand.currentAttachedObject == null ||
(player.rightHand.currentAttachedObject != null
&& player.rightHand.currentAttachedTeleportManager != null
&& player.rightHand.currentAttachedTeleportManager.teleportAllowed);
bool leftHandValid = player.leftHand.currentAttachedObject == null ||
(player.leftHand.currentAttachedObject != null
&& player.leftHand.currentAttachedTeleportManager != null
&& player.leftHand.currentAttachedTeleportManager.teleportAllowed);
bool leftHandTurnLeft = snapLeftAction.GetStateDown(SteamVR_Input_Sources.LeftHand) && leftHandValid;
bool rightHandTurnLeft = snapLeftAction.GetStateDown(SteamVR_Input_Sources.RightHand) && rightHandValid;
bool leftHandTurnRight = snapRightAction.GetStateDown(SteamVR_Input_Sources.LeftHand) && leftHandValid;
bool rightHandTurnRight = snapRightAction.GetStateDown(SteamVR_Input_Sources.RightHand) && rightHandValid;
if (leftHandTurnLeft || rightHandTurnLeft)
{
RotatePlayer(-snapAngle);
}
else if (leftHandTurnRight || rightHandTurnRight)
{
RotatePlayer(snapAngle);
}
}
}
private Coroutine rotateCoroutine;
public void RotatePlayer(float angle)
{
if (rotateCoroutine != null)
{
StopCoroutine(rotateCoroutine);
AllOff();
}
rotateCoroutine = StartCoroutine(DoRotatePlayer(angle));
}
//-----------------------------------------------------
private IEnumerator DoRotatePlayer(float angle)
{
Player player = Player.instance;
canRotate = false;
snapTurnSource.panStereo = angle / 90;
snapTurnSource.PlayOneShot(rotateSound);
if (fadeScreen)
{
SteamVR_Fade.Start(Color.clear, 0);
Color tColor = screenFadeColor;
tColor = tColor.linear * 0.6f;
SteamVR_Fade.Start(tColor, fadeTime);
}
yield return new WaitForSeconds(fadeTime);
Vector3 playerFeetOffset = player.trackingOriginTransform.position - player.feetPositionGuess;
player.trackingOriginTransform.position -= playerFeetOffset;
player.transform.Rotate(Vector3.up, angle);
playerFeetOffset = Quaternion.Euler(0.0f, angle, 0.0f) * playerFeetOffset;
player.trackingOriginTransform.position += playerFeetOffset;
GameObject fx = angle > 0 ? rotateRightFX : rotateLeftFX;
if (showTurnAnimation)
ShowRotateFX(fx);
if (fadeScreen)
{
SteamVR_Fade.Start(Color.clear, fadeTime);
}
float startTime = Time.time;
float endTime = startTime + canTurnEverySeconds;
while (Time.time <= endTime)
{
yield return null;
UpdateOrientation(fx);
};
fx.SetActive(false);
canRotate = true;
}
void ShowRotateFX(GameObject fx)
{
if (fx == null)
return;
fx.SetActive(false);
UpdateOrientation(fx);
fx.SetActive(true);
UpdateOrientation(fx);
}
private void UpdateOrientation(GameObject fx)
{
Player player = Player.instance;
//position fx in front of face
this.transform.position = player.hmdTransform.position + (player.hmdTransform.forward * distanceFromFace);
this.transform.rotation = Quaternion.LookRotation(player.hmdTransform.position - this.transform.position, Vector3.up);
this.transform.Translate(additionalOffset, Space.Self);
this.transform.rotation = Quaternion.LookRotation(player.hmdTransform.position - this.transform.position, Vector3.up);
}
}
}