Files
Ipseity-Demo/Assets/Unheimlich_POC/Scripts/Handlers/Interactions/InteracionHandler.cs
T

67 lines
1.9 KiB
C#

using UnityEngine;
using Unheimlich.Core;
using Unheimlich.Utility;
using InputManager = Unheimlich.Managers.InputManager;
namespace Unheimlich.Interactions.Core
{
public class InteractionHandler : Singleton<InteractionHandler>
{
[Header("Settings")]
[SerializeField] private float _interactionRange = 1f;
[SerializeField] private LayerMask _interactableLayers;
[SerializeField] private float _raycastInterval = 0.1f;
private InteractableBase _currentInteractable;
private Camera _playerCamera;
private float _raycastTimer;
protected override void OnAwake()
{
_playerCamera = GetComponent<Camera>();
}
private void Update()
{
if (RaycastHelper.ThrottledRaycastComponent(
ref _raycastTimer,
_raycastInterval,
_playerCamera,
_interactionRange,
_interactableLayers,
out InteractableBase interactable))
{
_currentInteractable = interactable;
}
else if (_raycastTimer == 0f)
{
_currentInteractable = null;
}
HandleInteractionInput();
}
private void HandleInteractionInput()
{
var interactAction = InputManager.GetInteractAction();
if (interactAction != null && interactAction.WasPressedThisFrame())
{
if (_currentInteractable != null)
_currentInteractable.Interact();
}
}
private void OnDrawGizmosSelected()
{
if (_playerCamera != null)
{
Gizmos.color = _currentInteractable != null ? Color.green : Color.red;
Vector3 viewportCenter = new Vector3(0.5f, 0.5f, 0f);
Ray ray = _playerCamera.ViewportPointToRay(viewportCenter);
Gizmos.DrawRay(ray.origin, ray.direction * _interactionRange);
}
}
}
}