Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /*
  4. * This class is attached to a door handle. The door handle is child of a door.
  5. */
  6. public class PhysicsOpenDoor : MonoBehaviour
  7. {
  8. [SerializeField] private Rigidbody door;
  9. [SerializeField] private Vector3 force;
  10. [SerializeField] private Vector3 cross;
  11. [SerializeField] private bool holdingHandle;
  12. [SerializeField] private float angle;
  13. private const float forceMultiplier = 1500f;
  14.  
  15. private void OnTriggerStay(Collider coll)
  16. {
  17. Debug.Log("Trigger Stay");
  18. Transform controller = coll.transform;
  19. if (Input.GetAxis(controller.name + " Grip Squeeze") != 0)
  20. {
  21. Debug.Log("Grip Pressed");
  22. holdingHandle = true;
  23.  
  24. // Direction vector from the door's pivot point to the hand's current position
  25. Vector3 doorPivotToHand = controller.transform.position - transform.parent.position;
  26.  
  27. // Ignore the y axis of the direction vector
  28. // doorPivotToHand.y = 0;
  29.  
  30. // Direction vector from door handle to hand's current position
  31. force = controller.transform.position - transform.position;
  32.  
  33. // Cross product between force and direction.
  34. cross = Vector3.Cross(doorPivotToHand, force);
  35. angle = Vector3.Angle(doorPivotToHand, force);
  36. }
  37. else
  38. {
  39. holdingHandle = false;
  40. }
  41. }
  42.  
  43. void Update()
  44. {
  45. if (holdingHandle)
  46. {
  47. // Apply cross product and calculated angle to
  48. door.angularVelocity = cross * angle * forceMultiplier;
  49. }
  50. }
  51.  
  52. private void OnTriggerExit()
  53. {
  54. // Set angular velocity to zero if the hand stops hovering
  55. door.angularVelocity = Vector3.zero;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement