Advertisement
Guest User

Eyes.cs

a guest
Oct 8th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. #region
  2. using System;
  3. using UnityEngine;
  4.  
  5. #endregion
  6.  
  7. [Serializable]
  8. public class Eyes
  9. {
  10.     public float fieldOfView = 60.0f;
  11.     public Transform transform;
  12.     public float viewDistance = Mathf.Infinity;
  13.  
  14.     public bool TargetIsReachable(Vector3 targetPosition)
  15.     {
  16.         var distance = Vector3.Distance(this.transform.position, targetPosition);
  17.         if (this.TargetIsInSight(targetPosition))
  18.         {
  19.             var hits = Physics.RaycastAll(this.transform.position, targetPosition - this.transform.position, distance);
  20.             foreach (var hit in hits)
  21.                 if (hit.transform.root.gameObject.isStatic)
  22.                     return false;
  23.             return true;
  24.         }
  25.         return false;
  26.     }
  27.  
  28.     public bool TargetIsInSight(Vector3 targetPosition)
  29.     {
  30.         var distance = Vector3.Distance(this.transform.position, targetPosition);
  31.         if (distance > this.viewDistance)
  32.             return false;
  33.         if (Vector3.Angle(this.transform.position + this.transform.forward, targetPosition) > this.fieldOfView)
  34.             return false;
  35.         return true;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement