Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class EnemySight : MonoBehaviour
- {
- public static EnemySight Instance;
- private RaycastHit SightInfo;
- private Collider LastSight;
- private int SightMask = 1 << 14; // i dont want raytrace to collide with layer 14
- // and cant put it into ignoreraycast for custom collision reason
- void Awake()
- {
- Instance = this;
- SightMask = ~SightMask;
- }
- void OnTriggerEnter(Collider collider)
- {
- if (collider.gameObject.tag == "Player")
- {
- ProcessSight(collider);
- }
- }
- void OnTriggerStay(Collider collider)
- {
- if (collider.gameObject.tag == "Player")
- {
- ProcessSight(collider);
- }
- }
- void ProcessSight(Collider collider)
- {
- Ray MyRay = new Ray(transform.parent.transform.position + Vector3.up, (collider.transform.position - Vector3.up) - transform.parent.transform.position);
- if (Physics.Raycast(MyRay, out SightInfo, Mathf.Infinity, SightMask))
- {
- if (SightInfo.collider.gameObject.tag == "Player")
- {
- LastSight = SightInfo.collider;
- Debug.DrawRay(transform.parent.transform.position + Vector3.up, (collider.transform.position - Vector3.up) - transform.parent.transform.position);
- EnemyAI.Instance.SetAttention(LastSight, true, false, false);
- }
- }
- else if (LastSight != null)
- {
- if (LastSight.gameObject.tag == "Player")
- EnemyAI.Instance.RemoveThisAttention(LastSight);
- }
- }
- }
Add Comment
Please, Sign In to add comment