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.

99 lines
4.2 KiB

  1. // Spring Lever|Controls3D|100080
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// This script extends VRTK_Lever to add spring force toward whichever end of the lever's range it is closest to.
  7. /// </summary>
  8. /// <remarks>
  9. /// The script will instantiate the required Rigidbody, Interactable and HingeJoint components automatically in case they do not exist yet. The joint is very tricky to setup automatically though and will only work in straight forward cases. If there are any issues, then create the HingeJoint component manually and configure it as needed.
  10. /// </remarks>
  11. [AddComponentMenu("VRTK/Scripts/Controls/3D/VRTK_SpringLever")]
  12. [System.Obsolete("`VRTK.VRTK_SpringLever` has been deprecated and can be recreated with `VRTK.Controllables.PhysicsBased.VRTK_PhysicsRotator`. This script will be removed in a future version of VRTK.")]
  13. public class VRTK_SpringLever : VRTK_Lever
  14. {
  15. [Tooltip("The strength of the spring force that will be applied upon the lever.")]
  16. public float springStrength = 10;
  17. [Tooltip("The damper of the spring force that will be applied upon the lever.")]
  18. public float springDamper = 10;
  19. [Tooltip("If this is checked then the spring will snap the lever to the nearest end point (either min or max angle). If it is unchecked, the lever will always snap to the min angle position.")]
  20. public bool snapToNearestLimit = false;
  21. [Tooltip("If this is checked then the spring will always be active even when grabbing the lever.")]
  22. public bool alwaysActive = false;
  23. protected bool wasTowardZero = true;
  24. protected bool isGrabbed = false;
  25. /// <summary>
  26. /// Override the original InitRequiredComponents() to add
  27. /// handling of spring forces on the hingeJoint
  28. /// </summary>
  29. protected override void InitRequiredComponents()
  30. {
  31. base.InitRequiredComponents();
  32. if (!leverHingeJoint.useSpring)
  33. {
  34. // If useSpring isn't set, the hingeJoint was probably automatically added - fix it
  35. leverHingeJoint.useSpring = true;
  36. JointSpring leverSpring = leverHingeJoint.spring;
  37. leverSpring.spring = springStrength;
  38. leverSpring.damper = springDamper;
  39. leverSpring.targetPosition = minAngle;
  40. leverHingeJoint.spring = leverSpring;
  41. }
  42. else
  43. {
  44. // If useSpring is set, the hingeJoint was manually added - respect its settings
  45. springStrength = leverHingeJoint.spring.spring;
  46. }
  47. }
  48. /// <summary>
  49. /// Adjust spring force during HandleUpdate()
  50. /// </summary>
  51. protected override void HandleUpdate()
  52. {
  53. base.HandleUpdate();
  54. ApplySpringForce();
  55. }
  56. protected override void InteractableObjectGrabbed(object sender, InteractableObjectEventArgs e)
  57. {
  58. base.InteractableObjectGrabbed(sender, e);
  59. isGrabbed = true;
  60. }
  61. protected override void InteractableObjectUngrabbed(object sender, InteractableObjectEventArgs e)
  62. {
  63. base.InteractableObjectUngrabbed(sender, e);
  64. isGrabbed = false;
  65. }
  66. protected virtual float GetSpringTarget(bool towardZero)
  67. {
  68. return (towardZero ? minAngle : maxAngle);
  69. }
  70. /// <summary>
  71. /// Check which direction the lever needs to be pushed in and
  72. /// switch spring direction as necessary
  73. /// </summary>
  74. protected virtual void ApplySpringForce()
  75. {
  76. leverHingeJoint.useSpring = (alwaysActive || !isGrabbed);
  77. if (leverHingeJoint.useSpring)
  78. {
  79. // get normalized value
  80. bool towardZero = (snapToNearestLimit ? (GetNormalizedValue() <= 50) : true);
  81. if (towardZero != wasTowardZero)
  82. {
  83. JointSpring leverSpring = leverHingeJoint.spring;
  84. leverSpring.targetPosition = GetSpringTarget(towardZero);
  85. leverHingeJoint.spring = leverSpring;
  86. wasTowardZero = towardZero;
  87. }
  88. }
  89. }
  90. }
  91. }