Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- public class RaycastDoor : MonoBehaviour
- {
- public float rayLength = 10f;
- public LayerMask raycastLayerMask;
- public float activationTime = 2f;
- private float activationTimer = 0f;
- private bool isActivating = false;
- public Slider activationSlider;
- public GameObject interactIcon;
- private void Update()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, rayLength, raycastLayerMask))
- {
- if (hit.collider.CompareTag("Door"))
- {
- interactIcon.SetActive(true);
- if (Input.GetKey(KeyCode.E))
- {
- if (!isActivating)
- {
- StartActivation();
- }
- activationTimer += Time.deltaTime;
- UpdateActivationProgress(activationTimer / activationTime);
- if (activationTimer >= activationTime)
- {
- CompleteActivation(hit.collider.gameObject);
- }
- }
- else
- {
- if (isActivating)
- {
- StopActivation();
- }
- }
- }
- else
- {
- interactIcon.SetActive(false);
- if (isActivating)
- {
- StopActivation();
- }
- }
- }
- else
- {
- interactIcon.SetActive(false);
- if (isActivating)
- {
- StopActivation();
- }
- }
- }
- private void StartActivation()
- {
- isActivating = true;
- activationTimer = 0f;
- }
- private void StopActivation()
- {
- isActivating = false;
- activationTimer = 0f;
- UpdateActivationProgress(0f);
- }
- private void CompleteActivation(GameObject door)
- {
- door.GetComponent<DoorInteractable>().ToggleDoor();
- StopActivation();
- }
- private void UpdateActivationProgress(float progress)
- {
- activationSlider.value = progress;
- }
- private void OnDrawGizmos()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawRay(transform.position, transform.forward * rayLength);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment