Skew

VR w Unity

Apr 19th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityXR = UnityEngine.XR;
  4.  
  5. public class VRInputProvider : InputProvider
  6. {
  7.     public Transform leftHand;
  8.     public Transform rightHand;
  9.     public Transform[] trackers;
  10.      
  11.     private List<XRNodeState> xrNodeStates = new List<XRNodeState>();
  12.     private Vector3 position;
  13.     private Quaternion rotation;
  14.      
  15.     void Start()
  16.     {
  17.         // Disable positional tracking for camera (because we are doing this by ourself later)
  18.         UnityXR.InputTracking.disablePositionalTracking = true;
  19.     }
  20.  
  21.     void Update()
  22.     {
  23.         // Warn if no nodes detected
  24.         if (xrNodeStates.Count == 0) Debug.LogWarning("No XR nodes detected! Make sure that OpenVR is enabled in settings and that headset is connected properly.");
  25.  
  26.         // Get updated node states
  27.         UnityXR.InputTracking.GetNodeStates(xrNodeStates);
  28.         int trackerId = 0;
  29.         foreach (XRNodeState state in xrNodeStates)
  30.         {
  31.             switch (state.nodeType)
  32.             {
  33.                 // Lewy kontroler
  34.                 case UnityXR.XRNode.LeftHand:
  35.                     if(state.TryGetPosition(out position) && state.TryGetRotation(out rotation))
  36.                     {
  37.                         leftHand.localPosition = position;
  38.                         leftHand.localRotation = rotation;
  39.                     }
  40.                     break;
  41.                 // Prawy kontroler
  42.                 case UnityXR.XRNode.RightHand:
  43.                     if(state.TryGetPosition(out position) && state.TryGetRotation(out rotation))
  44.                     {
  45.                         leftHand.localPosition = position;
  46.                         leftHand.localRotation = rotation;
  47.                     }
  48.                     break;
  49.                 // Trackery
  50.                 case UnityXR.XRNode.RightHand:
  51.                     if(state.TryGetPosition(out position) && state.TryGetRotation(out rotation))
  52.                     {
  53.                         if (receiver.Trackers.Length > trackerId)
  54.                         {
  55.                             trackers[trackerId].localPosition = position;
  56.                             trackers[trackerId].localRotation = rotation;
  57.                             trackerId++;
  58.                         }
  59.                         else
  60.                         {
  61.                             Debug.LogError($"Tracker {trackerId} connected, but there is no place for it!");
  62.                         }
  63.                     }
  64.                     break;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment