Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using System.Net.Sockets;
- using System.Linq;
- public class FieldOfView : MonoBehaviour
- {
- [Header("===== PROPERTIES =====")]
- [SerializeField] private bool isWatching = true;
- [Space]
- [Header("===== Character Properties =====")]
- [Range(0.1f, 1f)]
- [SerializeField] private float reactionTimes = 0.25f;
- [SerializeField] private float faceOffest = 0.2f;
- [SerializeField] private float characterEyesHeight = 1f;
- [Range(0, 360)]
- [SerializeField] private float fieldOfviewAngle = 10f;
- [SerializeField] private float viewDistance = 5f;
- private Vector3 characterEye = Vector3.zero;
- [Space]
- [SerializeField] private LayerMask targetLayers = 0;
- [SerializeField] private LayerMask obstacleMask = 0;
- //[HideInInspector]
- public List<GameObject> visibleTargets = new List<GameObject>();
- private Collider[] targetsInViewRadius;
- private Transform target = null;
- private Vector3 dirToTarget = Vector3.zero;
- private float dstToTarget = 0f;
- private Ray ray;
- private RaycastHit hit;
- private void Awake()
- {
- characterEye = new Vector3(transform.position.x + faceOffest, transform.position.y + characterEyesHeight, transform.position.z);
- }
- private void Start()
- {
- StartCoroutine("FindTargetsWithDelay", reactionTimes);
- }
- IEnumerator FindTargetsWithDelay(float reactionTimes)
- {
- while (isWatching)
- {
- yield return new WaitForSeconds(reactionTimes);
- FindVisibleTargets();
- }
- }
- private void FindVisibleTargets()
- {
- visibleTargets.Clear();
- targetsInViewRadius = Physics.OverlapSphere(transform.position, viewDistance, targetLayers);
- for (int i = 0; i < targetsInViewRadius.Length; i++)
- {
- Transform target = targetsInViewRadius[i].transform;
- // you are checking yourself!
- if (target != null && target.gameObject == gameObject)
- continue;
- // this target is already present!
- if (target != null && visibleTargets.Contains(target.gameObject))
- continue;
- Vector3 dirToTarget = (target.position - transform.position).normalized;
- if (Vector3.Angle(transform.forward, dirToTarget) < fieldOfviewAngle / 2)
- {
- float dstToTarget = Vector3.Distance(transform.position, target.position);
- if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
- {
- if(Physics.Raycast(transform.position, transform.forward, out hit, targetLayers))
- visibleTargets.Add(target.gameObject);
- }
- }
- }
- }
- private void OnDrawGizmos()
- {
- // work out the start point of the vision cone
- Vector3 startPoint = Mathf.Cos(-visionConeAngle * Mathf.Deg2Rad) * transform.forward +
- Mathf.Sin(-visionConeAngle * Mathf.Deg2Rad) * transform.right;
- // draw the vision cone
- Handles.color = visionConeColor;
- Handles.DrawSolidArc(transform.position, Vector3.up, startPoint, visionConeAngle * 2f, visionConeRange);
- // draw the hearing range
- Handles.color = hearingRangeColor;
- Handles.DrawSolidArc(transform.position, Vector3.up, startPoint, visionConeAngle - (360 - visionConeAngle), hearingRange);
- // draw the detector range
- Handles.color = proximityRangeColour;
- Handles.DrawSolidDisc(transform.position, Vector3.up, proximityDetectionRange);
- }
- }
Add Comment
Please, Sign In to add comment