SHARE
TWEET

Line of Sight in Unity

a guest Mar 24th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. //MADE BY LSTHIMO
  7. public class PlayerAI : Player {
  8.     //Line of sight
  9.     [Header ("Line of sight")]
  10.     public Transform viewport;
  11.     [Space (5)]
  12.  
  13.     public float viewAngle = 110;
  14.     public float viewRadius = 10;
  15.     public LayerMask detectable;
  16.     public int detectableLayerIndex;
  17.     Collider playerCollider;
  18.  
  19.     void Start () {
  20.         playerCollider = GetComponent<Collider> ();
  21.     }
  22.  
  23.     void Update () {
  24.         GetObjectsInSight ();
  25.     }
  26.  
  27.     List<Collider> detectedColliders = new List<Collider> ();
  28.  
  29.     void GetObjectsInSight () {
  30.  
  31.         //Check if there is a detectable object in the view radius
  32.         if (Physics.CheckSphere (viewport.position, viewRadius, detectable)) {
  33.  
  34.             //Get the detectable objects that are in radius
  35.             Collider[] detectableCollidersArray = Physics.OverlapSphere (viewport.position, viewRadius, detectable);
  36.  
  37.             //Put the detectable objects in a list
  38.             detectedColliders.RemoveRange(0, detectedColliders.Count);
  39.  
  40.             foreach (Collider c in detectableCollidersArray) {
  41.                 if (c != playerCollider) {
  42.                     detectedColliders.Add (c);
  43.                 }
  44.             }
  45.  
  46.  
  47.             //Check if objects are detectable
  48.             for (int index = 0; index < detectedColliders.Count; index++) {
  49.  
  50.                 //Get collider
  51.                 Collider collider = detectedColliders[index];
  52.  
  53.                 //Get angle
  54.                 Vector3 selfPosV3 = viewport.position + viewport.forward * viewRadius;
  55.  
  56.                 Vector2 selfPos = new Vector2 (selfPosV3.x, selfPosV3.z);
  57.                 Vector2 detectablePos = new Vector2 (collider.transform.position.x, collider.transform.position.z);
  58.  
  59.                 float angle = Vector2.Angle (selfPos, detectablePos);
  60.  
  61.                 //Check if angle is in LOS
  62.                 if (angle >= -viewAngle / 2 && angle <= viewAngle / 2) {
  63.  
  64.                     //Set layer to ignore
  65.                     int detectableValue = detectable.value;
  66.                     detectableValue = ~detectableValue;
  67.  
  68.                     //Check if an object is obstructing the view
  69.                     if (Physics.Linecast (viewport.position, collider.bounds.center, detectableValue)) {
  70.                         detectedColliders.RemoveAt (index);
  71.                         index -= 1;
  72.                     }
  73.                 } else {
  74.                     detectedColliders.RemoveAt (index);
  75.                     index -= 1;
  76.                 }
  77.             }
  78.            
  79.             //Sort colliders by distance to viewpoint
  80.             detectedColliders = detectedColliders.OrderBy (point => Vector3.Distance (viewport.transform.position, point.transform.position)).ToList ();
  81.            
  82.             print ("There are " + detectedColliders.Count + " objects in my LOS");
  83.         }
  84.     }
  85.  
  86.     float Positive (float input) {
  87.         if (input < 0) {
  88.             return input * -1;
  89.         } else {
  90.             return input;
  91.         }
  92.     }
  93.  
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top