8 using System.Collections;
9 using System.Collections.Generic;
11 namespace Valve.VR.InteractionSystem
19 [Tooltip(
"Virtual transform corresponding to the meatspace tracking origin. Devices are tracked relative to this." )]
20 public Transform trackingOriginTransform;
22 [Tooltip(
"List of possible transforms for the head/HMD, including the no-SteamVR fallback camera." )]
23 public Transform[] hmdTransforms;
25 [Tooltip(
"List of possible Hands, including no-SteamVR fallback Hands." )]
28 [Tooltip(
"Reference to the physics collider that follows the player's HMD position." )]
29 public Collider headCollider;
31 [Tooltip(
"These objects are enabled when SteamVR is available" )]
32 public GameObject rigSteamVR;
34 [Tooltip(
"These objects are enabled when SteamVR is not available, or when the user toggles out of VR" )]
35 public GameObject rig2DFallback;
37 [Tooltip(
"The audio listener for this player" )]
38 public Transform audioListener;
40 public bool allowToggleTo2D =
true;
46 private static Player _instance;
47 public static Player instance
51 if ( _instance == null )
53 _instance = FindObjectOfType<Player>();
68 for (
int i = 0; i < hands.Length; i++ )
70 if ( hands[i].gameObject.activeInHierarchy )
85 public Hand GetHand(
int i )
87 for (
int j = 0; j < hands.Length; j++ )
89 if ( !hands[j].gameObject.activeInHierarchy )
112 for (
int j = 0; j < hands.Length; j++ )
114 if ( !hands[j].gameObject.activeInHierarchy )
119 if ( hands[j].GuessCurrentHandType() !=
Hand.HandType.Left )
133 public Hand rightHand
137 for (
int j = 0; j < hands.Length; j++ )
139 if ( !hands[j].gameObject.activeInHierarchy )
144 if ( hands[j].GuessCurrentHandType() !=
Hand.HandType.Right )
190 public Transform hmdTransform
194 for (
int i = 0; i < hmdTransforms.Length; i++ )
196 if ( hmdTransforms[i].gameObject.activeInHierarchy )
197 return hmdTransforms[i];
207 public float eyeHeight
211 Transform hmd = hmdTransform;
214 Vector3 eyeOffset = Vector3.Project( hmd.position - trackingOriginTransform.position, trackingOriginTransform.up );
215 return eyeOffset.magnitude / trackingOriginTransform.lossyScale.x;
225 public Vector3 feetPositionGuess
229 Transform hmd = hmdTransform;
232 return trackingOriginTransform.position + Vector3.ProjectOnPlane( hmd.position - trackingOriginTransform.position, trackingOriginTransform.up );
234 return trackingOriginTransform.position;
242 public Vector3 bodyDirectionGuess
246 Transform hmd = hmdTransform;
249 Vector3 direction = Vector3.ProjectOnPlane( hmd.forward, trackingOriginTransform.up );
250 if ( Vector3.Dot( hmd.up, trackingOriginTransform.up ) < 0.0f )
255 direction = -direction;
259 return trackingOriginTransform.forward;
267 if ( trackingOriginTransform == null )
269 trackingOriginTransform = this.transform;
279 if (
SteamVR.instance != null )
281 ActivateRig( rigSteamVR );
286 ActivateRig( rig2DFallback );
295 if (
this != instance )
304 Gizmos.color = Color.white;
305 Gizmos.DrawIcon( feetPositionGuess,
"vr_interaction_system_feet.png" );
307 Gizmos.color = Color.cyan;
308 Gizmos.DrawLine( feetPositionGuess, feetPositionGuess + trackingOriginTransform.up * eyeHeight );
311 Gizmos.color = Color.blue;
312 Vector3 bodyDirection = bodyDirectionGuess;
313 Vector3 bodyDirectionTangent = Vector3.Cross( trackingOriginTransform.up, bodyDirection );
314 Vector3 startForward = feetPositionGuess + trackingOriginTransform.up * eyeHeight * 0.75f;
315 Vector3 endForward = startForward + bodyDirection * 0.33f;
316 Gizmos.DrawLine( startForward, endForward );
317 Gizmos.DrawLine( endForward, endForward - 0.033f * ( bodyDirection + bodyDirectionTangent ) );
318 Gizmos.DrawLine( endForward, endForward - 0.033f * ( bodyDirection - bodyDirectionTangent ) );
320 Gizmos.color = Color.red;
321 int count = handCount;
322 for (
int i = 0; i < count; i++ )
324 Hand hand = GetHand( i );
326 if ( hand.startingHandType ==
Hand.HandType.Left )
328 Gizmos.DrawIcon( hand.transform.position,
"vr_interaction_system_left_hand.png" );
330 else if ( hand.startingHandType ==
Hand.HandType.Right )
332 Gizmos.DrawIcon( hand.transform.position,
"vr_interaction_system_right_hand.png" );
336 Hand.HandType guessHandType = hand.GuessCurrentHandType();
338 if ( guessHandType ==
Hand.HandType.Left )
340 Gizmos.DrawIcon( hand.transform.position,
"vr_interaction_system_left_hand_question.png" );
342 else if ( guessHandType ==
Hand.HandType.Right )
344 Gizmos.DrawIcon( hand.transform.position,
"vr_interaction_system_right_hand_question.png" );
348 Gizmos.DrawIcon( hand.transform.position,
"vr_interaction_system_unknown_hand.png" );
356 public void Draw2DDebug()
358 if ( !allowToggleTo2D )
366 int left = Screen.width / 2 - width / 2;
367 int top = Screen.height - height - 10;
369 string text = ( rigSteamVR.activeSelf ) ?
"2D Debug" :
"VR";
371 if ( GUI.Button(
new Rect( left, top, width, height ), text ) )
373 if ( rigSteamVR.activeSelf )
375 ActivateRig( rig2DFallback );
379 ActivateRig( rigSteamVR );
386 private void ActivateRig( GameObject rig )
388 rigSteamVR.SetActive( rig == rigSteamVR );
389 rig2DFallback.SetActive( rig == rig2DFallback );
393 audioListener.transform.parent = hmdTransform;
394 audioListener.transform.localPosition = Vector3.zero;
395 audioListener.transform.localRotation = Quaternion.identity;
401 public void PlayerShotSelf()