Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Player / PlayerSight.cs
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;
    // Start is called before the first frame update
    void Start()
    {
        mainCamera = Camera.main;
        player = GameObject.FindGameObjectWithTag("Player")?.transform;
    }

/*    private void OnDrawGizmos()
    {
        if (player != null)
        {
            Gizmos.color = Color.green;
            foreach (GameObject g in gameObjects)
            {
                // Vector3 direction = g.transform.position - mainCamera.transform.position;
                Vector3 direction = GetSightDirection();
                Gizmos.DrawRay(transform.position, direction);
            }
        }
        
    }*/


    // Update is called once per frame
    void Update()
    {
        //  canSee.Clear();
        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)
                {
                    //  Ray r = mainCamera.ViewportPointToRay(pos);


                    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)
            {
                // canSee.Add(hit.collider.transform.parent.gameObject);
                if (target != e && gameObject.transform.parent != null)
                {
                    e.Looking(gameObject.transform.parent.gameObject, true);

                    if (target != null)
                    {
                        target.Looking(null, false);
                    }

                    target = e;
                }
            }
            else
            {
             //   canSee.Remove(hit.collider.gameObject);
                if (target != null)
                {
                    target.Looking(null, false);
                    target = null;

                }
            }
        }
    }
}