Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class PlayerInteraction : MonoBehaviour
  6. {
  7.  
  8.  
  9. public float speed = 10;
  10. public bool canHold = true;
  11. public Transform guide;
  12. [SerializeField] LayerMask interactionLayers = ~0;
  13.  
  14. private GameObject pickUpAble;
  15. private Interactable interactable;
  16. private RaycastHit hit;
  17. private bool placeAbleBarrelHeld;
  18.  
  19.  
  20.  
  21. void Update()
  22. {
  23. HandleInput();
  24. HandleRaycasting();
  25.  
  26. if (!canHold && pickUpAble)
  27. {
  28. pickUpAble.transform.position = guide.position;
  29. pickUpAble.transform.RotateAround(guide.position, guide.right, 100 * Input.GetAxis("Mouse ScrollWheel"));
  30. }
  31.  
  32. }
  33.  
  34. private void HandleInput()
  35. {
  36. if (Input.GetMouseButtonDown(0))
  37. {
  38. if (!canHold)
  39. Throw();
  40. else
  41. Pickup();
  42. }
  43. if (Input.GetMouseButtonDown(1))
  44. {
  45. if (!canHold)
  46. SetDown();
  47. }
  48. if (Input.GetKeyDown(KeyCode.E))
  49. {
  50. if (interactable)
  51. {
  52. interactable.Interact();
  53. }
  54. if (!placeAbleBarrelHeld)
  55. {
  56. return;
  57. }
  58. else if (pickUpAble.GetComponent<DrinkBarrel>().CheckForSpawning())
  59. {
  60. ClearHands();
  61. }
  62.  
  63.  
  64.  
  65. }
  66. }
  67.  
  68. private void HandleRaycasting()
  69. {
  70. if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 3f, interactionLayers))
  71. {
  72. if (hit.collider.GetComponentInChildren<Pickup>() || hit.transform.GetComponentInParent<Pickup>() || hit.transform.GetComponent<Pickup>())
  73. {
  74. if (!pickUpAble)
  75. {
  76. pickUpAble = hit.transform.gameObject;
  77. if(pickUpAble.GetComponent<DrinkBarrel>())
  78. {
  79. placeAbleBarrelHeld = true;
  80. }
  81. }
  82. }
  83. else if (canHold)
  84. {
  85. pickUpAble = null;
  86. }
  87. if (hit.collider.GetComponent<Interactable>())
  88. {
  89. interactable = hit.collider.GetComponent<Interactable>();
  90. }
  91. else
  92. {
  93. interactable = null;
  94. }
  95. }
  96.  
  97.  
  98. Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 3f, Color.yellow);
  99. }
  100.  
  101. private void ClearHands()
  102. {
  103. pickUpAble = null;
  104. canHold = true;
  105. placeAbleBarrelHeld = false;
  106. }
  107.  
  108. private void SetDown()
  109. {
  110. if (!pickUpAble)
  111. return;
  112.  
  113. pickUpAble.GetComponent<Rigidbody>().useGravity = true;
  114. pickUpAble.GetComponent<Rigidbody>().isKinematic = false;
  115.  
  116. guide.GetChild(0).parent = null;
  117. ClearHands();
  118.  
  119. }
  120.  
  121.  
  122. private void Pickup()
  123. {
  124. if (!pickUpAble)
  125. return;
  126.  
  127.  
  128. pickUpAble.transform.SetParent(guide);
  129.  
  130.  
  131. pickUpAble.GetComponent<Rigidbody>().useGravity = false;
  132. pickUpAble.GetComponent<Rigidbody>().isKinematic = true;
  133.  
  134. pickUpAble.transform.position = guide.position;
  135.  
  136.  
  137. canHold = false;
  138.  
  139. }
  140.  
  141. private void Throw()
  142. {
  143. if (!pickUpAble)
  144. return;
  145.  
  146. pickUpAble.GetComponent<Rigidbody>().useGravity = true;
  147. pickUpAble.GetComponent<Rigidbody>().isKinematic = false;
  148.  
  149. guide.GetChild(0).gameObject.GetComponent<Rigidbody>().velocity = transform.forward * speed;
  150. guide.GetChild(0).parent = null;
  151. ClearHands();
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement