Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.4 KiB

  1. // Unity SDK Controller Tracker|SDK_Unity|005
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. #if UNITY_2017_2_OR_NEWER
  6. using UnityEngine.XR;
  7. #else
  8. using UnityEngine.VR;
  9. using XRNode = UnityEngine.VR.VRNode;
  10. #endif
  11. /// <summary>
  12. /// The Controller Tracker enables the GameObject to track it's position/rotation to the available connected VR Controller via the `UnityEngine.VR` library.
  13. /// </summary>
  14. /// <remarks>
  15. /// The Unity Controller Tracker is attached to the `[UnityBase_CameraRig]` prefab on the child `LeftHandAnchor` and `RightHandAnchor` to enable controller tracking.
  16. /// </remarks>
  17. public class SDK_UnityControllerTracker : MonoBehaviour
  18. {
  19. [Tooltip("The Unity VRNode to track.")]
  20. public XRNode nodeType;
  21. [Tooltip("The unique index to assign to the controller.")]
  22. public uint index;
  23. [Tooltip("The Unity Input name for the trigger axis.")]
  24. public string triggerAxisName = "";
  25. [Tooltip("The Unity Input name for the grip axis.")]
  26. public string gripAxisName = "";
  27. [Tooltip("The Unity Input name for the touchpad horizontal axis.")]
  28. public string touchpadHorizontalAxisName = "";
  29. [Tooltip("The Unity Input name for the touchpad vertical axis.")]
  30. public string touchpadVerticalAxisName = "";
  31. protected virtual void OnEnable()
  32. {
  33. CheckAxisIsValid(triggerAxisName, "triggerAxisName");
  34. CheckAxisIsValid(gripAxisName, "gripAxisName");
  35. CheckAxisIsValid(touchpadHorizontalAxisName, "touchpadHorizontalAxisName");
  36. CheckAxisIsValid(touchpadVerticalAxisName, "touchpadVerticalAxisName");
  37. }
  38. protected virtual string GetVarName<T>(T item) where T : class
  39. {
  40. return VRTK_SharedMethods.GetPropertyFirstName<T>();
  41. }
  42. protected virtual void CheckAxisIsValid(string axisName, string varName)
  43. {
  44. try
  45. {
  46. Input.GetAxis(axisName);
  47. }
  48. catch (System.ArgumentException ae)
  49. {
  50. VRTK_Logger.Warn(ae.Message + " on index [" + index + "] variable [" + varName + "]");
  51. }
  52. }
  53. protected virtual void FixedUpdate()
  54. {
  55. transform.localPosition = InputTracking.GetLocalPosition(nodeType);
  56. transform.localRotation = InputTracking.GetLocalRotation(nodeType);
  57. }
  58. }
  59. }