Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.InputSystem;
- [RequireComponent(typeof(BoxCollider))]
- public class CubeField : MonoBehaviour
- {
- [SerializeField] private LayerMask _fieldMask;
- private BoxCollider _fieldCollider;
- private bool _isEnterField = false;
- public UnityAction EnterFieldEvent;
- public UnityAction ExitFieldEvent;
- private void Awake()
- {
- _fieldCollider = GetComponent<BoxCollider>();
- }
- private void FixedUpdate()
- {
- if (TryGetHitInField(Mouse.current.position.ReadValue(), Camera.main))
- {
- if (_isEnterField == false)
- {
- _isEnterField = true;
- EnterFieldEvent?.Invoke();
- }
- }
- else
- {
- if (_isEnterField)
- {
- _isEnterField = false;
- ExitFieldEvent?.Invoke();
- }
- }
- }
- private bool TryGetHitInField(Vector3 screenPosition, Camera camera)
- {
- Ray ray = camera.ScreenPointToRay(screenPosition);
- if (Physics.Raycast(ray, float.MaxValue, _fieldMask))
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement