FrontWheelTracker.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class FrontWheelTracker : MonoBehaviour
  6. {
  7. private const String KEY_POS_X = "vive_tracker_pos_x";
  8. private const String KEY_POS_Y = "vive_tracker_pos_y";
  9. private const String KEY_POS_Z = "vive_tracker_pos_z";
  10. private const String KEY_ROT_X = "vive_tracker_rot_x";
  11. private const String KEY_ROT_Y = "vive_tracker_rot_y";
  12. private const String KEY_ROT_Z = "vive_tracker_rot_z";
  13. private Transform trackerTransform;
  14. private Vector3 zeroRot;
  15. private Vector3 zeroPos;
  16. public Vector3 Rotation => trackerTransform.rotation.eulerAngles - zeroRot;
  17. public Vector3 Position => trackerTransform.position - zeroPos;
  18. public Vector3 ZeroRot => zeroRot;
  19. public Vector3 ZeroPos => zeroPos;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. trackerTransform = transform;
  24. if (PlayerPrefs.HasKey(KEY_POS_X) && PlayerPrefs.HasKey(KEY_POS_Y) && PlayerPrefs.HasKey(KEY_POS_Z))
  25. {
  26. var x = PlayerPrefs.GetFloat(KEY_POS_X);
  27. var y = PlayerPrefs.GetFloat(KEY_POS_Y);
  28. var z = PlayerPrefs.GetFloat(KEY_POS_Z);
  29. zeroPos = new Vector3(x, y, z);
  30. }
  31. if (PlayerPrefs.HasKey(KEY_ROT_X) && PlayerPrefs.HasKey(KEY_ROT_Y) && PlayerPrefs.HasKey(KEY_ROT_Z))
  32. {
  33. var x = PlayerPrefs.GetFloat(KEY_ROT_X);
  34. var y = PlayerPrefs.GetFloat(KEY_ROT_Y);
  35. var z = PlayerPrefs.GetFloat(KEY_ROT_Z);
  36. zeroRot = new Vector3(x, y, z);
  37. }
  38. }
  39. // Update is called once per frame
  40. void Update()
  41. {
  42. }
  43. //sets current position as zero -> straighten handlebar and call this function
  44. public void Calibrate()
  45. {
  46. zeroRot = trackerTransform.rotation.eulerAngles;
  47. zeroPos = trackerTransform.position;
  48. PlayerPrefs.SetFloat(KEY_ROT_X, zeroRot.x);
  49. PlayerPrefs.SetFloat(KEY_ROT_Y, zeroRot.y);
  50. PlayerPrefs.SetFloat(KEY_ROT_Z, zeroRot.z);
  51. PlayerPrefs.SetFloat(KEY_POS_X, zeroPos.x);
  52. PlayerPrefs.SetFloat(KEY_POS_Y, zeroPos.y);
  53. PlayerPrefs.SetFloat(KEY_POS_Z, zeroPos.z);
  54. PlayerPrefs.Save();
  55. }
  56. }