using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerLook : MonoBehaviour, ILookAround { public GameObject isObserving { get; private set; } private GameObject beingObservedBy; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (beingObservedBy != null) { Ray newRay = new Ray(transform.position + transform.forward, transform.forward); RaycastHit hit; if (Physics.Raycast(newRay, out hit, 400f)) { isObserving = hit.collider.gameObject; ILookAround[] iLookAround = isObserving.GetComponents<ILookAround>(); if (iLookAround.Length > 0) { foreach (ILookAround look in iLookAround) { look.Looking(gameObject, true); } } } else { if (isObserving != null) { ILookAround[] iLookAround = isObserving.GetComponents<ILookAround>(); if (iLookAround.Length > 0) { foreach (ILookAround look in iLookAround) { look.Looking(null, true); } } isObserving = null; } } } } void OnDrawGizmos() { Gizmos.color = Color.yellow; Gizmos.DrawRay(new Ray(transform.position + transform.forward, transform.forward)); } public void Looking(GameObject source,bool state) { beingObservedBy = source; } }