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.

70 lines
3.7 KiB

  1. // Snap Rotate Object Control Action|ObjectControlActions|25030
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Provides the ability to snap rotate a GameObject through the world `y` axis in the scene by updating the Transform rotation in defined steps when the corresponding Object Control axis changes.
  7. /// </summary>
  8. /// <remarks>
  9. /// > The effect is a immediate snap rotation to quickly face in a new direction.
  10. ///
  11. /// **Required Components:**
  12. /// * `VRTK_ObjectControl` - The Object Control script to listen for the axis changes on.
  13. ///
  14. /// **Script Usage:**
  15. /// * Place the `VRTK_SnapRotateObjectControlAction` script on any active scene GameObject.
  16. /// * Link the required Object Control script to the `Object Control Script` parameter of this script.
  17. /// * Set the `Listen On Axis Change` parameter on this script to the axis change to affect with this movement type.
  18. /// </remarks>
  19. /// <example>
  20. /// `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.
  21. ///
  22. /// To enable the Snap Rotate Object Control Action, ensure one of the `TouchpadControlOptions` children (located under the Controller script alias) has the `Snap Rotate` control script active.
  23. /// </example>
  24. [AddComponentMenu("VRTK/Scripts/Locomotion/Object Control Actions/VRTK_SnapRotateObjectControlAction")]
  25. public class VRTK_SnapRotateObjectControlAction : VRTK_BaseObjectControlAction
  26. {
  27. [Tooltip("The angle to rotate for each snap.")]
  28. public float anglePerSnap = 30f;
  29. [Tooltip("The snap angle multiplier to be applied when the modifier button is pressed.")]
  30. public float angleMultiplier = 1.5f;
  31. [Tooltip("The amount of time required to pass before another snap rotation can be carried out.")]
  32. public float snapDelay = 0.5f;
  33. [Tooltip("The speed for the headset to fade out and back in. Having a blink between rotations can reduce nausia.")]
  34. public float blinkTransitionSpeed = 0.6f;
  35. [Range(-1f, 1f)]
  36. [Tooltip("The threshold the listened axis needs to exceed before the action occurs. This can be used to limit the snap rotate to a single axis direction (e.g. pull down to flip rotate). The threshold is ignored if it is 0.")]
  37. public float axisThreshold = 0f;
  38. protected float snapDelayTimer = 0f;
  39. protected override void Process(GameObject controlledGameObject, Transform directionDevice, Vector3 axisDirection, float axis, float deadzone, bool currentlyFalling, bool modifierActive)
  40. {
  41. CheckForPlayerBeforeRotation(controlledGameObject);
  42. if (snapDelayTimer < Time.time && ValidThreshold(axis))
  43. {
  44. float angle = Rotate(axis, modifierActive);
  45. if (angle != 0f)
  46. {
  47. Blink(blinkTransitionSpeed);
  48. RotateAroundPlayer(controlledGameObject, angle);
  49. }
  50. }
  51. CheckForPlayerAfterRotation(controlledGameObject);
  52. }
  53. protected virtual bool ValidThreshold(float axis)
  54. {
  55. return (axisThreshold == 0f || ((axisThreshold > 0f && axis >= axisThreshold) || (axisThreshold < 0f && axis <= axisThreshold)));
  56. }
  57. protected virtual float Rotate(float axis, bool modifierActive)
  58. {
  59. snapDelayTimer = Time.time + snapDelay;
  60. int directionMultiplier = GetAxisDirection(axis);
  61. return (anglePerSnap * (modifierActive ? angleMultiplier : 1)) * directionMultiplier;
  62. }
  63. }
  64. }