ArrowHand.cs
8.19 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
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
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: The object attached to the player's hand that spawns and fires the
// arrow
//
//=============================================================================
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Valve.VR.InteractionSystem
{
//-------------------------------------------------------------------------
public class ArrowHand : MonoBehaviour
{
private Hand hand;
private Longbow bow;
private GameObject currentArrow;
public GameObject arrowPrefab;
public Transform arrowNockTransform;
public float nockDistance = 0.1f;
public float lerpCompleteDistance = 0.08f;
public float rotationLerpThreshold = 0.15f;
public float positionLerpThreshold = 0.15f;
private bool allowArrowSpawn = true;
private bool nocked;
private GrabTypes nockedWithType = GrabTypes.None;
private bool inNockRange = false;
private bool arrowLerpComplete = false;
public SoundPlayOneshot arrowSpawnSound;
private AllowTeleportWhileAttachedToHand allowTeleport = null;
public int maxArrowCount = 10;
private List<GameObject> arrowList;
//-------------------------------------------------
void Awake()
{
allowTeleport = GetComponent<AllowTeleportWhileAttachedToHand>();
//allowTeleport.teleportAllowed = true;
allowTeleport.overrideHoverLock = false;
arrowList = new List<GameObject>();
}
//-------------------------------------------------
private void OnAttachedToHand( Hand attachedHand )
{
hand = attachedHand;
FindBow();
}
//-------------------------------------------------
private GameObject InstantiateArrow()
{
GameObject arrow = Instantiate( arrowPrefab, arrowNockTransform.position, arrowNockTransform.rotation ) as GameObject;
arrow.name = "Bow Arrow";
arrow.transform.parent = arrowNockTransform;
Util.ResetTransform( arrow.transform );
arrowList.Add( arrow );
while ( arrowList.Count > maxArrowCount )
{
GameObject oldArrow = arrowList[0];
arrowList.RemoveAt( 0 );
if ( oldArrow )
{
Destroy( oldArrow );
}
}
return arrow;
}
//-------------------------------------------------
private void HandAttachedUpdate( Hand hand )
{
if ( bow == null )
{
FindBow();
}
if ( bow == null )
{
return;
}
if ( allowArrowSpawn && ( currentArrow == null ) ) // If we're allowed to have an active arrow in hand but don't yet, spawn one
{
currentArrow = InstantiateArrow();
arrowSpawnSound.Play();
}
float distanceToNockPosition = Vector3.Distance( transform.parent.position, bow.nockTransform.position );
// If there's an arrow spawned in the hand and it's not nocked yet
if ( !nocked )
{
// If we're close enough to nock position that we want to start arrow rotation lerp, do so
if ( distanceToNockPosition < rotationLerpThreshold )
{
float lerp = Util.RemapNumber( distanceToNockPosition, rotationLerpThreshold, lerpCompleteDistance, 0, 1 );
arrowNockTransform.rotation = Quaternion.Lerp( arrowNockTransform.parent.rotation, bow.nockRestTransform.rotation, lerp );
}
else // Not close enough for rotation lerp, reset rotation
{
arrowNockTransform.localRotation = Quaternion.identity;
}
// If we're close enough to the nock position that we want to start arrow position lerp, do so
if ( distanceToNockPosition < positionLerpThreshold )
{
float posLerp = Util.RemapNumber( distanceToNockPosition, positionLerpThreshold, lerpCompleteDistance, 0, 1 );
posLerp = Mathf.Clamp( posLerp, 0f, 1f );
arrowNockTransform.position = Vector3.Lerp( arrowNockTransform.parent.position, bow.nockRestTransform.position, posLerp );
}
else // Not close enough for position lerp, reset position
{
arrowNockTransform.position = arrowNockTransform.parent.position;
}
// Give a haptic tick when lerp is visually complete
if ( distanceToNockPosition < lerpCompleteDistance )
{
if ( !arrowLerpComplete )
{
arrowLerpComplete = true;
hand.TriggerHapticPulse( 500 );
}
}
else
{
if ( arrowLerpComplete )
{
arrowLerpComplete = false;
}
}
// Allow nocking the arrow when controller is close enough
if ( distanceToNockPosition < nockDistance )
{
if ( !inNockRange )
{
inNockRange = true;
bow.ArrowInPosition();
}
}
else
{
if ( inNockRange )
{
inNockRange = false;
}
}
GrabTypes bestGrab = hand.GetBestGrabbingType(GrabTypes.Pinch, true);
// If arrow is close enough to the nock position and we're pressing the trigger, and we're not nocked yet, Nock
if ( ( distanceToNockPosition < nockDistance ) && bestGrab != GrabTypes.None && !nocked )
{
if ( currentArrow == null )
{
currentArrow = InstantiateArrow();
}
nocked = true;
nockedWithType = bestGrab;
bow.StartNock( this );
hand.HoverLock( GetComponent<Interactable>() );
allowTeleport.teleportAllowed = false;
currentArrow.transform.parent = bow.nockTransform;
Util.ResetTransform( currentArrow.transform );
Util.ResetTransform( arrowNockTransform );
}
}
// If arrow is nocked, and we release the trigger
if ( nocked && hand.IsGrabbingWithType(nockedWithType) == false )
{
if ( bow.pulled ) // If bow is pulled back far enough, fire arrow, otherwise reset arrow in arrowhand
{
FireArrow();
}
else
{
arrowNockTransform.rotation = currentArrow.transform.rotation;
currentArrow.transform.parent = arrowNockTransform;
Util.ResetTransform( currentArrow.transform );
nocked = false;
nockedWithType = GrabTypes.None;
bow.ReleaseNock();
hand.HoverUnlock( GetComponent<Interactable>() );
allowTeleport.teleportAllowed = true;
}
bow.StartRotationLerp(); // Arrow is releasing from the bow, tell the bow to lerp back to controller rotation
}
}
//-------------------------------------------------
private void OnDetachedFromHand( Hand hand )
{
Destroy( gameObject );
}
//-------------------------------------------------
private void FireArrow()
{
currentArrow.transform.parent = null;
Arrow arrow = currentArrow.GetComponent<Arrow>();
arrow.shaftRB.isKinematic = false;
arrow.shaftRB.useGravity = true;
arrow.shaftRB.transform.GetComponent<BoxCollider>().enabled = true;
arrow.arrowHeadRB.isKinematic = false;
arrow.arrowHeadRB.useGravity = true;
arrow.arrowHeadRB.transform.GetComponent<BoxCollider>().enabled = true;
arrow.arrowHeadRB.AddForce( currentArrow.transform.forward * bow.GetArrowVelocity(), ForceMode.VelocityChange );
arrow.arrowHeadRB.AddTorque( currentArrow.transform.forward * 10 );
nocked = false;
nockedWithType = GrabTypes.None;
currentArrow.GetComponent<Arrow>().ArrowReleased( bow.GetArrowVelocity() );
bow.ArrowReleased();
allowArrowSpawn = false;
Invoke( "EnableArrowSpawn", 0.5f );
StartCoroutine( ArrowReleaseHaptics() );
currentArrow = null;
allowTeleport.teleportAllowed = true;
}
//-------------------------------------------------
private void EnableArrowSpawn()
{
allowArrowSpawn = true;
}
//-------------------------------------------------
private IEnumerator ArrowReleaseHaptics()
{
yield return new WaitForSeconds( 0.05f );
hand.otherHand.TriggerHapticPulse( 1500 );
yield return new WaitForSeconds( 0.05f );
hand.otherHand.TriggerHapticPulse( 800 );
yield return new WaitForSeconds( 0.05f );
hand.otherHand.TriggerHapticPulse( 500 );
yield return new WaitForSeconds( 0.05f );
hand.otherHand.TriggerHapticPulse( 300 );
}
//-------------------------------------------------
private void OnHandFocusLost( Hand hand )
{
gameObject.SetActive( false );
}
//-------------------------------------------------
private void OnHandFocusAcquired( Hand hand )
{
gameObject.SetActive( true );
}
//-------------------------------------------------
private void FindBow()
{
bow = hand.otherHand.GetComponentInChildren<Longbow>();
}
}
}