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.

80 lines
2.9 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class Controller_Hand : MonoBehaviour
  5. {
  6. public enum Hands
  7. {
  8. Right,
  9. Left
  10. }
  11. public Hands hand = Hands.Right;
  12. private Transform pointerFinger;
  13. private Transform gripFingers;
  14. private float maxRotation = 90f;
  15. private float originalPointerRotation;
  16. private float originalGripRotation;
  17. private float targetPointerRotation;
  18. private float targetGripRotation;
  19. private void Start()
  20. {
  21. GetComponentInParent<VRTK_InteractGrab>().GrabButtonPressed += DoGrabOn;
  22. GetComponentInParent<VRTK_InteractGrab>().GrabButtonReleased += DoGrabOff;
  23. GetComponentInParent<VRTK_InteractUse>().UseButtonPressed += DoUseOn;
  24. GetComponentInParent<VRTK_InteractUse>().UseButtonReleased += DoUseOff;
  25. var handContainer = "ModelPieces";
  26. pointerFinger = transform.Find(handContainer + "/PointerFingerContainer");
  27. gripFingers = transform.Find(handContainer + "/GripFingerContainer");
  28. if (hand == Hands.Left)
  29. {
  30. InversePosition(pointerFinger);
  31. InversePosition(gripFingers);
  32. InversePosition(transform.Find(handContainer + "/Palm"));
  33. InversePosition(transform.Find(handContainer + "/Thumb"));
  34. }
  35. originalPointerRotation = pointerFinger.localEulerAngles.y;
  36. originalGripRotation = gripFingers.localEulerAngles.y;
  37. targetPointerRotation = originalPointerRotation;
  38. targetGripRotation = originalGripRotation;
  39. }
  40. private void InversePosition(Transform givenTransform)
  41. {
  42. givenTransform.localPosition = new Vector3(givenTransform.localPosition.x * -1, givenTransform.localPosition.y, givenTransform.localPosition.z);
  43. givenTransform.localEulerAngles = new Vector3(givenTransform.localEulerAngles.x, givenTransform.localEulerAngles.y * -1, givenTransform.localEulerAngles.z);
  44. }
  45. private void DoGrabOn(object sender, ControllerInteractionEventArgs e)
  46. {
  47. targetGripRotation = maxRotation;
  48. }
  49. private void DoGrabOff(object sender, ControllerInteractionEventArgs e)
  50. {
  51. targetGripRotation = originalGripRotation;
  52. }
  53. private void DoUseOn(object sender, ControllerInteractionEventArgs e)
  54. {
  55. targetPointerRotation = maxRotation;
  56. }
  57. private void DoUseOff(object sender, ControllerInteractionEventArgs e)
  58. {
  59. targetPointerRotation = originalPointerRotation;
  60. }
  61. private void Update()
  62. {
  63. pointerFinger.localEulerAngles = new Vector3(targetPointerRotation, 0f, 0f);
  64. gripFingers.localEulerAngles = new Vector3(targetGripRotation, 0f, 0f);
  65. }
  66. }
  67. }