PickUp2Handed.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion;
  4. using RootMotion.FinalIK;
  5. namespace RootMotion.Demos {
  6. /// <summary>
  7. /// Picking up an arbitrary object with both hands.
  8. /// </summary>
  9. public abstract class PickUp2Handed : MonoBehaviour {
  10. // GUI for testing
  11. public int GUIspace;
  12. void OnGUI() {
  13. GUILayout.BeginHorizontal();
  14. GUILayout.Space(GUIspace);
  15. if (!holding) {
  16. if (GUILayout.Button("Pick Up " + obj.name)) {
  17. interactionSystem.StartInteraction(FullBodyBipedEffector.LeftHand, obj, false);
  18. interactionSystem.StartInteraction(FullBodyBipedEffector.RightHand, obj, false);
  19. }
  20. } else {
  21. GUILayout.BeginVertical();
  22. if (holdingRight)
  23. {
  24. if (GUILayout.Button("Release Right"))
  25. {
  26. interactionSystem.ResumeInteraction(FullBodyBipedEffector.RightHand);
  27. }
  28. }
  29. if (holdingLeft)
  30. {
  31. if (GUILayout.Button("Release Left"))
  32. {
  33. interactionSystem.ResumeInteraction(FullBodyBipedEffector.LeftHand);
  34. }
  35. }
  36. if (GUILayout.Button("Drop " + obj.name)) {
  37. interactionSystem.ResumeAll();
  38. }
  39. GUILayout.EndVertical();
  40. }
  41. GUILayout.EndHorizontal();
  42. }
  43. protected abstract void RotatePivot();
  44. public InteractionSystem interactionSystem; // The InteractionSystem of the character
  45. public InteractionObject obj; // The object to pick up
  46. public Transform pivot; // The pivot point of the hand targets
  47. public Transform holdPoint; // The point where the object will lerp to when picked up
  48. public float pickUpTime = 0.3f; // Maximum lerp speed of the object. Decrease this value to give the object more weight
  49. private float holdWeight, holdWeightVel;
  50. private Vector3 pickUpPosition;
  51. private Quaternion pickUpRotation;
  52. void Start() {
  53. // Listen to interaction events
  54. interactionSystem.OnInteractionStart += OnStart;
  55. interactionSystem.OnInteractionPause += OnPause;
  56. interactionSystem.OnInteractionResume += OnDrop;
  57. }
  58. // Called by the InteractionSystem when an interaction is paused (on trigger)
  59. private void OnPause(FullBodyBipedEffector effectorType, InteractionObject interactionObject) {
  60. if (effectorType != FullBodyBipedEffector.LeftHand) return;
  61. if (interactionObject != obj) return;
  62. // Make the object inherit the character's movement
  63. obj.transform.parent = interactionSystem.transform;
  64. // Make the object kinematic
  65. var r = obj.GetComponent<Rigidbody>();
  66. if (r != null) r.isKinematic = true;
  67. // Set object pick up position and rotation to current
  68. pickUpPosition = obj.transform.position;
  69. pickUpRotation = obj.transform.rotation;
  70. holdWeight = 0f;
  71. holdWeightVel = 0f;
  72. }
  73. // Called by the InteractionSystem when an interaction starts
  74. private void OnStart(FullBodyBipedEffector effectorType, InteractionObject interactionObject) {
  75. if (effectorType != FullBodyBipedEffector.LeftHand) return;
  76. if (interactionObject != obj) return;
  77. // Rotate the pivot of the hand targets
  78. RotatePivot();
  79. // Rotate the hold point so it matches the current rotation of the object
  80. holdPoint.rotation = obj.transform.rotation;
  81. }
  82. // Called by the InteractionSystem when an interaction is resumed from being paused
  83. private void OnDrop(FullBodyBipedEffector effectorType, InteractionObject interactionObject) {
  84. if (holding) return;
  85. if (interactionObject != obj) return;
  86. // Make the object independent of the character
  87. obj.transform.parent = null;
  88. // Turn on physics for the object
  89. if (obj.GetComponent<Rigidbody>() != null) obj.GetComponent<Rigidbody>().isKinematic = false;
  90. }
  91. void LateUpdate() {
  92. if (holding) {
  93. // Smoothing in the hold weight
  94. holdWeight = Mathf.SmoothDamp(holdWeight, 1f, ref holdWeightVel, pickUpTime);
  95. // Interpolation
  96. obj.transform.position = Vector3.Lerp(pickUpPosition, holdPoint.position, holdWeight);
  97. obj.transform.rotation = Quaternion.Lerp(pickUpRotation, holdPoint.rotation, holdWeight);
  98. }
  99. }
  100. // Are we currently holding the object?
  101. private bool holding {
  102. get {
  103. return holdingLeft || holdingRight;
  104. }
  105. }
  106. // Are we currently holding the object with left hand?
  107. private bool holdingLeft
  108. {
  109. get
  110. {
  111. return interactionSystem.IsPaused(FullBodyBipedEffector.LeftHand) && interactionSystem.GetInteractionObject(FullBodyBipedEffector.LeftHand) == obj;
  112. }
  113. }
  114. // Are we currently holding the object with right hand?
  115. private bool holdingRight
  116. {
  117. get
  118. {
  119. return interactionSystem.IsPaused(FullBodyBipedEffector.RightHand) && interactionSystem.GetInteractionObject(FullBodyBipedEffector.RightHand) == obj;
  120. }
  121. }
  122. // Clean up delegates
  123. void OnDestroy() {
  124. if (interactionSystem == null) return;
  125. interactionSystem.OnInteractionStart -= OnStart;
  126. interactionSystem.OnInteractionPause -= OnPause;
  127. interactionSystem.OnInteractionResume -= OnDrop;
  128. }
  129. }
  130. }