120 lines
3.0 KiB
C#
120 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Burst.CompilerServices;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering.HighDefinition;
|
|
using UnityEngine.UI;
|
|
using InfallibleCode;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class RaycastEvent : MonoBehaviour
|
|
{
|
|
[SerializeField] float _playerVisibleDistance = 20;
|
|
public Camera _camera;
|
|
public GameObject _currentHitObject;
|
|
//[HideInInspector] public RaycastHit hit;
|
|
LayerMask mask = -1;
|
|
|
|
[SerializeField] EventTrigger _eventTriggerIfLookingAt;
|
|
[SerializeField] EventTrigger _eventTriggerIfNotLookingAt;
|
|
public bool shouldInvokeEventIfNotLookingAt;
|
|
public bool FocusEvent;
|
|
|
|
private bool isFocusing;
|
|
|
|
[SerializeField] private string _eventTag;
|
|
|
|
private void Start()
|
|
{
|
|
_camera = PlayerManager.GetInstance().PlayerMainCamera.GetComponent<Camera>();
|
|
//_playerVisibleDistance = 20;
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (FocusEvent) //If It's a focus event (It means that the player is pressing a key to zoom the camera)
|
|
{
|
|
if (PlayerManager.GetInstance()._cameraMovement.IsZooming) //Player is zooming.
|
|
{
|
|
UpdateEvents();
|
|
isFocusing = true;
|
|
}
|
|
else
|
|
{
|
|
if (isFocusing)
|
|
{
|
|
if (shouldInvokeEventIfNotLookingAt)
|
|
{
|
|
_eventTriggerIfNotLookingAt.Invoke();
|
|
}
|
|
isFocusing = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UpdateEvents();
|
|
}
|
|
}
|
|
|
|
private void UpdateEvents()
|
|
{
|
|
if (isPlayerLookingAtEvent())
|
|
{
|
|
_eventTriggerIfLookingAt.Invoke();
|
|
}
|
|
else
|
|
{
|
|
if (isFocusing)
|
|
{
|
|
if (shouldInvokeEventIfNotLookingAt)
|
|
{
|
|
_eventTriggerIfNotLookingAt.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void IsFocusEvent()
|
|
{
|
|
FocusEvent = true;
|
|
}
|
|
|
|
public void IsNotFocusEvent()
|
|
{
|
|
FocusEvent = false;
|
|
}
|
|
|
|
public void ShouldInvokeEventIfNotLookingAt()
|
|
{
|
|
shouldInvokeEventIfNotLookingAt = true;
|
|
|
|
}
|
|
public void ShouldNotInvokeEventIfNotLookingAt()
|
|
{
|
|
shouldInvokeEventIfNotLookingAt = false;
|
|
}
|
|
|
|
public bool isPlayerLookingAtEvent()
|
|
{
|
|
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
|
|
if (Physics.Raycast(ray, out hit,_playerVisibleDistance, mask.value, QueryTriggerInteraction.Ignore))
|
|
{
|
|
Debug.DrawLine(ray.origin, hit.point, Color.red);
|
|
_currentHitObject = hit.transform.gameObject;
|
|
if (_currentHitObject == null) return false;
|
|
if (_currentHitObject.tag == _eventTag)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|