Created
February 1, 2017 23:37
-
-
Save ltuozzo/112138ad42c9530de15b5bc22226b739 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class FieldOfView : MonoBehaviour { | |
public float viewRadius; | |
[Range(0, 360)] | |
public float viewAngle; | |
public LayerMask targetMask; | |
public LayerMask obstacleMask; | |
[HideInInspector] | |
public List<Transform> visibleTargets = new List<Transform>(); | |
void Start() { | |
StartCoroutine("FindTargetWithDelay", 0.2f); | |
} | |
IEnumerator FindTargetWithDelay(float delay) { | |
while (true) { | |
yield return new WaitForSeconds(delay); | |
FindVisibleTargets(); | |
} | |
} | |
//Finds targets inside field of view not blocked by walls | |
void FindVisibleTargets() { | |
visibleTargets.Clear(); | |
//Adds targets in view radius to an array | |
Collider2D[] targetsInViewRadius = Physics2D.OverlapCircleAll(transform.position, viewRadius, targetMask); | |
//For every targetsInViewRadius it checks if they are inside the field of view | |
for (int i = 0; i < targetsInViewRadius.Length; i++) { | |
Transform target = targetsInViewRadius[i].transform; | |
Vector3 dirToTarget = (target.position - transform.position).normalized; | |
if (Vector3.Angle(transform.up, dirToTarget) < viewAngle / 2) { | |
float dstToTarget = Vector3.Distance(transform.position, target.position); | |
//If line draw from object to target is not interrupted by wall, add target to list of visible | |
//targets | |
if (!Physics2D.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask)) { | |
visibleTargets.Add(target); | |
} | |
} | |
} | |
} | |
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal) { | |
if (!angleIsGlobal) { | |
angleInDegrees -= transform.eulerAngles.z; | |
} | |
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad), 0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
[CustomEditor (typeof (FieldOfView))] | |
public class FieldOfViewEditor : Editor { | |
void OnSceneGUI() { | |
FieldOfView fow = (FieldOfView)target; | |
//Draws view reach | |
Handles.color = Color.black; | |
Handles.DrawWireArc(fow.transform.position, Vector3.forward, Vector3.up, 360, fow.viewRadius); | |
//Draws cone of view | |
Vector3 viewAngleA = fow.DirFromAngle(-fow.viewAngle / 2, false); | |
Vector3 viewAngleB = fow.DirFromAngle(fow.viewAngle / 2, false); | |
Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleA * fow.viewRadius); | |
Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleB * fow.viewRadius); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment