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.

102 lines
5.3 KiB

  1. // Warp Object Control Action|ObjectControlActions|25040
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Provides the ability to move a GameObject around by warping it across the `x/z` plane in the scene by updating the Transform position in defined steps when the corresponding Object Control axis changes.
  7. /// </summary>
  8. /// <remarks>
  9. /// > The effect is a immediate snap to a new position in the given direction.
  10. ///
  11. /// **Required Components:**
  12. /// * `VRTK_ObjectControl` - The Object Control script to listen for the axis changes on.
  13. ///
  14. /// **Optional Components:**
  15. /// * `VRTK_BodyPhysics` - The Body Physics script to utilise when checking for potential collisions on movement.
  16. ///
  17. /// **Script Usage:**
  18. /// * Place the `VRTK_WarpObjectControlAction` script on any active scene GameObject.
  19. /// * Link the required Object Control script to the `Object Control Script` parameter of this script.
  20. /// * Set the `Listen On Axis Change` parameter on this script to the axis change to affect with this movement type.
  21. /// </remarks>
  22. /// <example>
  23. /// `VRTK/Examples/017_CameraRig_TouchpadWalking` has a collection of walls and slopes that can be traversed by the user with the touchpad. There is also an area that can only be traversed if the user is crouching.
  24. ///
  25. /// To enable the Warp Object Control Action, ensure one of the `TouchpadControlOptions` children (located under the Controller script alias) has the `Warp` control script active.
  26. /// </example>
  27. [AddComponentMenu("VRTK/Scripts/Locomotion/Object Control Actions/VRTK_WarpObjectControlAction")]
  28. public class VRTK_WarpObjectControlAction : VRTK_BaseObjectControlAction
  29. {
  30. [Header("Warp Settings")]
  31. [Tooltip("The distance to warp in the facing direction.")]
  32. public float warpDistance = 1f;
  33. [Tooltip("The multiplier to be applied to the warp when the modifier button is pressed.")]
  34. public float warpMultiplier = 2f;
  35. [Tooltip("The amount of time required to pass before another warp can be carried out.")]
  36. public float warpDelay = 0.5f;
  37. [Tooltip("The height different in floor allowed to be a valid warp.")]
  38. public float floorHeightTolerance = 1f;
  39. [Tooltip("The speed for the headset to fade out and back in. Having a blink between warps can reduce nausia.")]
  40. public float blinkTransitionSpeed = 0.6f;
  41. [Header("Custom Settings")]
  42. [Tooltip("An optional Body Physics script to check for potential collisions in the moving direction. If any potential collision is found then the move will not take place. This can help reduce collision tunnelling.")]
  43. public VRTK_BodyPhysics bodyPhysics;
  44. protected float warpDelayTimer = 0f;
  45. protected Transform headset;
  46. protected override void Process(GameObject controlledGameObject, Transform directionDevice, Vector3 axisDirection, float axis, float deadzone, bool currentlyFalling, bool modifierActive)
  47. {
  48. if (warpDelayTimer < Time.time && axis != 0f)
  49. {
  50. Warp(controlledGameObject, directionDevice, axisDirection, axis, modifierActive);
  51. }
  52. }
  53. protected override void OnEnable()
  54. {
  55. internalBodyPhysics = bodyPhysics;
  56. base.OnEnable();
  57. headset = VRTK_DeviceFinder.HeadsetTransform();
  58. }
  59. protected virtual void Warp(GameObject controlledGameObject, Transform directionDevice, Vector3 axisDirection, float axis, bool modifierActive)
  60. {
  61. Vector3 objectCenter = GetObjectCenter(controlledGameObject.transform);
  62. Vector3 objectPosition = controlledGameObject.transform.TransformPoint(objectCenter);
  63. float distance = warpDistance * (modifierActive ? warpMultiplier : 1);
  64. int directionMultiplier = GetAxisDirection(axis);
  65. Vector3 targetPosition = objectPosition + (axisDirection * distance * directionMultiplier);
  66. float headMargin = 0.2f;
  67. RaycastHit warpRaycastHit;
  68. // direction raycast to stop near obstacles
  69. Vector3 raycastDirection = directionMultiplier * axisDirection;
  70. Vector3 startRayPosition = (controlledGameObject.transform == playArea ? headset.position : controlledGameObject.transform.position);
  71. if (Physics.Raycast(startRayPosition + (Vector3.up * headMargin), raycastDirection, out warpRaycastHit, distance - colliderRadius))
  72. {
  73. targetPosition = warpRaycastHit.point - (raycastDirection * colliderRadius);
  74. }
  75. // vertical raycast for height position
  76. if (Physics.Raycast(targetPosition + (Vector3.up * (floorHeightTolerance + headMargin)), Vector3.down, out warpRaycastHit, (floorHeightTolerance + headMargin) * 2))
  77. {
  78. targetPosition.y = warpRaycastHit.point.y + (colliderHeight / 2f);
  79. Vector3 finalPosition = targetPosition - objectPosition + controlledGameObject.transform.position;
  80. warpDelayTimer = Time.time + warpDelay;
  81. if (CanMove(bodyPhysics, controlledGameObject.transform.position, finalPosition))
  82. {
  83. controlledGameObject.transform.position = finalPosition;
  84. Blink(blinkTransitionSpeed);
  85. }
  86. }
  87. }
  88. }
  89. }