MaximilianPs

Unity - FieldOfView v0.5

Oct 20th, 2020 (edited)
594
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using System.Net.Sockets;
  6. using System.Linq;
  7.  
  8. public class FieldOfView : MonoBehaviour
  9. {
  10.     [Header("===== PROPERTIES =====")]
  11.     [SerializeField] private bool isWatching = true;
  12.     [Space]
  13.     [Header("===== Character Properties =====")]
  14.     [Range(0.1f, 1f)]
  15.     [SerializeField] private float reactionTimes = 0.25f;
  16.     [SerializeField] private float faceOffest = 0.2f;
  17.     [SerializeField] private float characterEyesHeight = 1f;
  18.     [Range(0, 360)]
  19.     [SerializeField] private float fieldOfviewAngle = 10f;
  20.     [SerializeField] private float viewDistance = 5f;
  21.                      private Vector3 characterEye = Vector3.zero;
  22.     [Space]
  23.     [SerializeField] private LayerMask targetLayers = 0;
  24.     [SerializeField] private LayerMask obstacleMask = 0;
  25.  
  26.     //[HideInInspector]
  27.     public List<GameObject> visibleTargets = new List<GameObject>();
  28.     private Collider[] targetsInViewRadius;
  29.     private Transform target = null;
  30.     private Vector3 dirToTarget = Vector3.zero;
  31.     private float dstToTarget = 0f;
  32.     private Ray ray;
  33.     private RaycastHit hit;
  34.  
  35.     private void Awake()
  36.     {
  37.         characterEye = new Vector3(transform.position.x + faceOffest, transform.position.y + characterEyesHeight, transform.position.z);
  38.     }
  39.  
  40.     private void Start()
  41.     {
  42.         StartCoroutine("FindTargetsWithDelay", reactionTimes);
  43.     }
  44.  
  45.     IEnumerator FindTargetsWithDelay(float reactionTimes)
  46.     {
  47.         while (isWatching)
  48.         {
  49.             yield return new WaitForSeconds(reactionTimes);
  50.             FindVisibleTargets();
  51.         }
  52.     }
  53.  
  54.     private void FindVisibleTargets()
  55.     {
  56.         visibleTargets.Clear();
  57.  
  58.         targetsInViewRadius = Physics.OverlapSphere(transform.position, viewDistance, targetLayers);
  59.  
  60.         for (int i = 0; i < targetsInViewRadius.Length; i++)
  61.         {
  62.             Transform target = targetsInViewRadius[i].transform;
  63.            
  64.             // you are checking yourself!
  65.             if (target != null && target.gameObject == gameObject)
  66.                 continue;
  67.  
  68.             // this target is already present!
  69.             if (target != null && visibleTargets.Contains(target.gameObject))
  70.                 continue;
  71.  
  72.             Vector3 dirToTarget = (target.position - transform.position).normalized;
  73.             if (Vector3.Angle(transform.forward, dirToTarget) < fieldOfviewAngle / 2)
  74.             {
  75.                 float dstToTarget = Vector3.Distance(transform.position, target.position);
  76.                 if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
  77.                 {
  78.                     if(Physics.Raycast(transform.position, transform.forward, out hit, targetLayers))
  79.                         visibleTargets.Add(target.gameObject);
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     private void OnDrawGizmosSelected()
  86.     {
  87.         characterEye = new Vector3(transform.position.x, transform.position.y + characterEyesHeight, transform.position.z - faceOffest);
  88.  
  89.         viewDistance = Handles.RadiusHandle(Quaternion.identity, characterEye, viewDistance);
  90.         Handles.color = new Color32(200, 16, 220, 50);
  91.         Handles.DrawSolidArc(characterEye, Vector3.up, transform.forward, fieldOfviewAngle / 2, viewDistance);
  92.         Handles.DrawSolidArc(characterEye, Vector3.up, transform.forward, -fieldOfviewAngle / 2, viewDistance);
  93.  
  94.         Vector3 viewAngleA = Helpers.DirFromAngle(transform, -fieldOfviewAngle / 2, false);
  95.         Vector3 viewAngleB = Helpers.DirFromAngle(transform, fieldOfviewAngle / 2, false);
  96.  
  97.         Handles.color = Color.blue;
  98.         Handles.DrawLine(characterEye, characterEye + viewAngleA * viewDistance);
  99.         Handles.DrawLine(characterEye, characterEye + viewAngleB * viewDistance);
  100.  
  101.         Handles.color = Color.red;
  102.         foreach (GameObject visibleTarget in visibleTargets)
  103.             Handles.DrawDottedLine(characterEye, visibleTarget.transform.position, 3f);
  104.  
  105.         //Handles.DrawBezier(characterEye, visibleTarget.transform.position,
  106.         //                  characterEye, visibleTarget.transform.position, Color.red, null, thickness);
  107.  
  108.     }
  109.  
  110. }
RAW Paste Data