Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Events;
- public class RaycastHelper2D : MonoBehaviour
- {
- [SerializeField] private Vector2 raycastDirection;
- [SerializeField] private float raycastDistance;
- [SerializeField] private bool hitEvent = true;
- [SerializeField] private LayerMask hitLayerMask;
- [SerializeField] private UnityEvent OnRaycastEvent;
- // Update is called once per frame
- void Update()
- {
- var hit = IsRaycastHit(hitEvent);
- if (hit)
- {
- OnRaycastEvent?.Invoke();
- }
- }
- private void OnDrawGizmosSelected()
- {
- if (!enabled) return;
- var p = transform.position;
- Gizmos.color = IsRaycastHit(hitEvent) ? Color.red : Color.green;
- Gizmos.DrawLine(p, (p + (Vector3)raycastDirection * raycastDistance));
- }
- private bool IsRaycastHit(bool hit)
- {
- var p = transform.position;
- var raycastHit = Physics2D.Raycast(p, raycastDirection, raycastDistance, hitLayerMask.value);
- var result = hit ? raycastHit.collider != null : raycastHit.collider == null;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement