Advertisement
Ultimga

Untitled

Apr 26th, 2021
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Valve.VR;
  6.  
  7. public class GripPan : MonoBehaviour
  8. {
  9. // Reference to the hand for grip
  10. public SteamVR_Input_Sources handRight;
  11. public SteamVR_Input_Sources handLeft;
  12. public SteamVR_Action_Boolean GripOnOff;
  13. public GameObject player;
  14. public Valve.VR.InteractionSystem.Hand RightHand;
  15. public Valve.VR.InteractionSystem.Hand LeftHand;
  16.  
  17. public float floorHeight = 0f;
  18.  
  19. public float panRate = 3.0f;
  20. public bool useMomentum = true;
  21. public float momentumDrag = 5.0f;
  22.  
  23. bool isPanning;
  24. bool isGliding;
  25. Vector3 panStartPosition;
  26. Transform panHand;
  27. SteamVR_Input_Sources currentHand;
  28.  
  29. private Vector3 movementVector;
  30. private Vector3 glidingVector;
  31. private float glideTimePassed;
  32. private Vector3 grabPosition;
  33. private Vector3 grabOffPosition;
  34. float magnitude;
  35. Vector3 velocity;
  36. float grabTime;
  37.  
  38.  
  39. // Start is called before the first frame update
  40. void Start()
  41. {
  42. GripOnOff.AddOnStateDownListener(GripOn, handRight);
  43. GripOnOff.AddOnStateUpListener(GripOff, handRight);
  44. GripOnOff.AddOnStateDownListener(GripOn, handLeft);
  45. GripOnOff.AddOnStateUpListener(GripOff, handLeft);
  46. }
  47.  
  48. // Update is called once per frame
  49. void Update()
  50. {
  51. if (isPanning)
  52. {
  53. movementVector = panStartPosition - panHand.transform.position;
  54. Vector3 adjustedMovementVector = movementVector * panRate;
  55. player.transform.position += adjustedMovementVector;
  56. panStartPosition = panHand.position;
  57. glideTimePassed += Time.deltaTime;
  58. }
  59. else if (isGliding)
  60. {
  61. magnitude -= momentumDrag * Time.deltaTime;
  62. if (magnitude < 0) magnitude = 0;
  63.  
  64. player.transform.position += glidingVector * magnitude * Time.deltaTime;
  65. }
  66.  
  67. // Don't let player go below the 'floor'
  68. if (player.transform.position.y < floorHeight)
  69. player.transform.position = new Vector3(player.transform.position.x, floorHeight, player.transform.position.z);
  70. }
  71.  
  72. public void GripOff(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  73. {
  74. if (isPanning && currentHand == fromSource)
  75. {
  76. isPanning = false;
  77.  
  78. grabOffPosition = panHand.position;
  79.  
  80. if (useMomentum)
  81. {
  82. isGliding = true;
  83.  
  84. glidingVector = grabOffPosition - grabPosition;
  85. magnitude = glidingVector.magnitude / glideTimePassed;
  86. glidingVector.Normalize();
  87. }
  88. }
  89.  
  90. }
  91.  
  92. public void GripOn(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  93. {
  94. if (fromSource == SteamVR_Input_Sources.RightHand)
  95. {
  96. if (RightHand.hoveringInteractable == null)
  97. {
  98. panHand = RightHand.transform;
  99. panStartPosition = panHand.transform.position;
  100. isPanning = true;
  101. currentHand = fromSource;
  102. }
  103. }
  104. else if (fromSource == SteamVR_Input_Sources.LeftHand)
  105. {
  106. if (LeftHand.hoveringInteractable == null)
  107. {
  108. panHand = LeftHand.transform;
  109. panStartPosition = panHand.transform.position;
  110. isPanning = true;
  111. currentHand = fromSource;
  112. }
  113. }
  114. isGliding = false;
  115.  
  116. grabPosition = panHand.position;
  117. glideTimePassed = 0.0f;
  118. }
  119. }
  120.  
  121. // if (RightHand.hoveringInteractable == null && LeftHand.hoveringInteractable == null)
  122. // {
  123. // if (fromSource == SteamVR_Input_Sources.RightHand)
  124. // panHand = RightHand.transform;
  125. // else if (fromSource == SteamVR_Input_Sources.LeftHand)
  126. // panHand = LeftHand.transform;
  127.  
  128. // panStart = panHand.transform.position;
  129. // isPanning = true;
  130.  
  131. // if (fromSource != currentHand)
  132. // currentHand = fromSource;
  133. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement