- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerSight : MonoBehaviour
- {
- private Camera mainCamera;
- private ILookAround target;
- public static LinkedList<GameObject> gameObjects = new LinkedList<GameObject>();
- public static HashSet<GameObject> canSee = new HashSet<GameObject>(8);
- private Transform player;
-
- void Start()
- {
- mainCamera = Camera.main;
- player = GameObject.FindGameObjectWithTag("Player")?.transform;
- }
-
- void Update()
- {
-
- if (player != null)
- {
- foreach (GameObject g in gameObjects)
- {
- Vector3 pos = mainCamera.WorldToViewportPoint(g.transform.position);
- if (pos.x > 0 && pos.x < 1 && pos.y > 0 && pos.y < 1)
- {
-
- Vector3 direction = g.transform.position - mainCamera.transform.position;
- Ray r = new Ray(mainCamera.transform.position, GetSightDirection());
-
- TestRaycast(r);
-
- }
- }
- }
- }
- private Vector3 GetSightDirection()
- {
- Vector3 offset = player.transform.position + (player.transform.forward * 2);
- return Vector3.Normalize(offset- new Vector3(mainCamera.transform.position.x, offset.y, mainCamera.transform.position.z));
- }
- private void TestRaycast(Ray r)
- {
-
- RaycastHit hit;
- if (Physics.Raycast(r, out hit, mainCamera.farClipPlane, LayerMask.GetMask("Nightmare"), QueryTriggerInteraction.Collide))
- {
- ILookAround e = hit.collider.GetComponent<ILookAround>();
- Debug.Log(gameObject);
- if (e != null)
- {
-
- if (target != e && gameObject.transform.parent != null)
- {
- e.Looking(gameObject.transform.parent.gameObject, true);
- if (target != null)
- {
- target.Looking(null, false);
- }
- target = e;
- }
- }
- else
- {
-
- if (target != null)
- {
- target.Looking(null, false);
- target = null;
- }
- }
- }
- }
- }