Skip to content

Instantly share code, notes, and snippets.

@momo-the-monster
Created October 29, 2019 01:27
Show Gist options
  • Save momo-the-monster/0a048d90d86de3eeda0e7abb169d965d to your computer and use it in GitHub Desktop.
Save momo-the-monster/0a048d90d86de3eeda0e7abb169d965d to your computer and use it in GitHub Desktop.
Unity XR Cross-Platform Input
using UnityEngine;
using UnityEngine.XR;
namespace MMM.XR
{
public class InputExplore : MonoBehaviour
{
[SerializeField] private GameObject _leftHandPrefab;
[SerializeField] private GameObject _rightHandPrefab;
[SerializeField] private Transform _handContainer;
private Transform _leftHandTransform;
private Transform _rightHandTransform;
private InputDevice _head;
private InputDevice _leftHand;
private InputDevice _rightHand;
void Start()
{
InitTransforms();
}
private void InitTransforms()
{
_leftHandTransform = Instantiate(_leftHandPrefab, _handContainer).transform;
_rightHandTransform = Instantiate(_rightHandPrefab, _handContainer).transform;
}
void Update()
{
if (_leftHand.IsValid)
{
_leftHandTransform.position = InputTracking.GetLocalPosition(XRNode.LeftHand);
_leftHandTransform.rotation = InputTracking.GetLocalRotation(XRNode.LeftHand);
}
else
{
_leftHand = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
}
if (_rightHand.IsValid)
{
_rightHandTransform.position = InputTracking.GetLocalPosition(XRNode.RightHand);
_rightHandTransform.rotation = InputTracking.GetLocalRotation(XRNode.RightHand);
}
else
{
_rightHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment