MaximilianPs

Unity - FieldOfView v0.6

Oct 20th, 2020 (edited)
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  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 OnDrawGizmos()
  86.         {
  87.             // work out the start point of the vision cone
  88.             Vector3 startPoint = Mathf.Cos(-visionConeAngle * Mathf.Deg2Rad) * transform.forward +
  89.                                     Mathf.Sin(-visionConeAngle * Mathf.Deg2Rad) * transform.right;
  90.  
  91.             // draw the vision cone
  92.             Handles.color = visionConeColor;
  93.             Handles.DrawSolidArc(transform.position, Vector3.up, startPoint, visionConeAngle * 2f, visionConeRange);
  94.  
  95.             // draw the hearing range
  96.             Handles.color = hearingRangeColor;
  97.             Handles.DrawSolidArc(transform.position, Vector3.up, startPoint, visionConeAngle - (360 - visionConeAngle), hearingRange);
  98.            
  99.             // draw the detector range
  100.             Handles.color = proximityRangeColour;
  101.             Handles.DrawSolidDisc(transform.position, Vector3.up, proximityDetectionRange);
  102.         }
  103. }
Add Comment
Please, Sign In to add comment