Guest User

RaycastDoor Script

a guest
Apr 29th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class RaycastDoor : MonoBehaviour
  5. {
  6. public float rayLength = 10f;
  7. public LayerMask raycastLayerMask;
  8.  
  9. public float activationTime = 2f;
  10. private float activationTimer = 0f;
  11. private bool isActivating = false;
  12. public Slider activationSlider;
  13. public GameObject interactIcon;
  14. private void Update()
  15. {
  16. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  17. RaycastHit hit;
  18.  
  19. if (Physics.Raycast(ray, out hit, rayLength, raycastLayerMask))
  20. {
  21. if (hit.collider.CompareTag("Door"))
  22. {
  23. interactIcon.SetActive(true);
  24. if (Input.GetKey(KeyCode.E))
  25. {
  26. if (!isActivating)
  27. {
  28. StartActivation();
  29. }
  30.  
  31. activationTimer += Time.deltaTime;
  32. UpdateActivationProgress(activationTimer / activationTime);
  33.  
  34. if (activationTimer >= activationTime)
  35. {
  36. CompleteActivation(hit.collider.gameObject);
  37. }
  38. }
  39. else
  40. {
  41. if (isActivating)
  42. {
  43. StopActivation();
  44. }
  45. }
  46. }
  47. else
  48. {
  49. interactIcon.SetActive(false);
  50.  
  51. if (isActivating)
  52. {
  53. StopActivation();
  54. }
  55. }
  56. }
  57. else
  58. {
  59. interactIcon.SetActive(false);
  60.  
  61. if (isActivating)
  62. {
  63. StopActivation();
  64. }
  65. }
  66. }
  67.  
  68.  
  69. private void StartActivation()
  70. {
  71. isActivating = true;
  72. activationTimer = 0f;
  73. }
  74.  
  75. private void StopActivation()
  76. {
  77. isActivating = false;
  78. activationTimer = 0f;
  79. UpdateActivationProgress(0f);
  80. }
  81. private void CompleteActivation(GameObject door)
  82. {
  83. door.GetComponent<DoorInteractable>().ToggleDoor();
  84. StopActivation();
  85. }
  86.  
  87. private void UpdateActivationProgress(float progress)
  88. {
  89. activationSlider.value = progress;
  90. }
  91.  
  92. private void OnDrawGizmos()
  93. {
  94. Gizmos.color = Color.red;
  95. Gizmos.DrawRay(transform.position, transform.forward * rayLength);
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment