46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
|
using Interactive;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace Player
|
|||
|
{
|
|||
|
public class Interactions : MonoBehaviour
|
|||
|
{
|
|||
|
public float maxHitDistance = 5f;
|
|||
|
public bool canInteract;
|
|||
|
|
|||
|
private Camera _cam;
|
|||
|
private RaycastHit _hit;
|
|||
|
private int _playerLayerMask, _defaultLayerMask;
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
_cam = Camera.main;
|
|||
|
_playerLayerMask = LayerMask.GetMask("Player");
|
|||
|
_defaultLayerMask = LayerMask.GetMask("Default");
|
|||
|
}
|
|||
|
|
|||
|
void Update()
|
|||
|
{
|
|||
|
Ray ray = _cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
|
|||
|
Interact(ray);
|
|||
|
}
|
|||
|
|
|||
|
void Interact(Ray ray)
|
|||
|
{
|
|||
|
if (Physics.Raycast(ray, out _hit, maxHitDistance, _defaultLayerMask))
|
|||
|
{
|
|||
|
IInteractable interactable = _hit.transform.gameObject.GetComponent<IInteractable>();
|
|||
|
canInteract = interactable != null;
|
|||
|
|
|||
|
if (canInteract && Input.GetKeyDown(KeyCode.E))
|
|||
|
{
|
|||
|
interactable.Interact();
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
canInteract = false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|