Guest User

Untitled

a guest
Jan 23rd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemySight : MonoBehaviour
  5. {
  6.     public static EnemySight Instance;
  7.     private RaycastHit SightInfo;
  8.     private Collider LastSight;
  9.     private int SightMask = 1 << 14; // i dont want raytrace to collide with layer 14
  10.     // and cant put it into ignoreraycast for custom collision reason
  11.  
  12.     void Awake()
  13.     {
  14.         Instance = this;
  15.         SightMask = ~SightMask;
  16.     }
  17.  
  18.     void OnTriggerEnter(Collider collider)
  19.     {
  20.         if (collider.gameObject.tag == "Player")
  21.         {
  22.             ProcessSight(collider);
  23.         }
  24.     }
  25.  
  26.     void OnTriggerStay(Collider collider)
  27.     {
  28.         if (collider.gameObject.tag == "Player")
  29.         {
  30.             ProcessSight(collider);
  31.         }
  32.     }
  33.  
  34.     void ProcessSight(Collider collider)
  35.     {
  36.         Ray MyRay = new Ray(transform.parent.transform.position + Vector3.up, (collider.transform.position - Vector3.up) - transform.parent.transform.position);
  37.         if (Physics.Raycast(MyRay, out SightInfo, Mathf.Infinity, SightMask))
  38.         {
  39.             if (SightInfo.collider.gameObject.tag == "Player")
  40.             {
  41.                 LastSight = SightInfo.collider;
  42.                 Debug.DrawRay(transform.parent.transform.position + Vector3.up, (collider.transform.position - Vector3.up) - transform.parent.transform.position);
  43.                 EnemyAI.Instance.SetAttention(LastSight, true, false, false);
  44.             }
  45.         }
  46.         else if (LastSight != null)
  47.         {
  48.             if (LastSight.gameObject.tag == "Player")
  49.             EnemyAI.Instance.RemoveThisAttention(LastSight);
  50.         }
  51.  
  52.     }
  53. }
Add Comment
Please, Sign In to add comment