Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityXR = UnityEngine.XR;
- public class VRInputProvider : InputProvider
- {
- public Transform leftHand;
- public Transform rightHand;
- public Transform[] trackers;
- private List<XRNodeState> xrNodeStates = new List<XRNodeState>();
- private Vector3 position;
- private Quaternion rotation;
- void Start()
- {
- // Disable positional tracking for camera (because we are doing this by ourself later)
- UnityXR.InputTracking.disablePositionalTracking = true;
- }
- void Update()
- {
- // Warn if no nodes detected
- if (xrNodeStates.Count == 0) Debug.LogWarning("No XR nodes detected! Make sure that OpenVR is enabled in settings and that headset is connected properly.");
- // Get updated node states
- UnityXR.InputTracking.GetNodeStates(xrNodeStates);
- int trackerId = 0;
- foreach (XRNodeState state in xrNodeStates)
- {
- switch (state.nodeType)
- {
- // Lewy kontroler
- case UnityXR.XRNode.LeftHand:
- if(state.TryGetPosition(out position) && state.TryGetRotation(out rotation))
- {
- leftHand.localPosition = position;
- leftHand.localRotation = rotation;
- }
- break;
- // Prawy kontroler
- case UnityXR.XRNode.RightHand:
- if(state.TryGetPosition(out position) && state.TryGetRotation(out rotation))
- {
- leftHand.localPosition = position;
- leftHand.localRotation = rotation;
- }
- break;
- // Trackery
- case UnityXR.XRNode.RightHand:
- if(state.TryGetPosition(out position) && state.TryGetRotation(out rotation))
- {
- if (receiver.Trackers.Length > trackerId)
- {
- trackers[trackerId].localPosition = position;
- trackers[trackerId].localRotation = rotation;
- trackerId++;
- }
- else
- {
- Debug.LogError($"Tracker {trackerId} connected, but there is no place for it!");
- }
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment