Advertisement
Krythic

CameraLightDetector

Mar 8th, 2024
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class CameraLightDetector : MonoBehaviour
  5. {
  6.     public StealthGlobe stealthGlobeUIElement;
  7.     private CharacterController characterController;
  8.     public bool IsPlayerIlluminated { get; private set; }
  9.     public int TotalLightCollisions { get; private set; }
  10.  
  11.     private List<SphericalLightCollider> lights;
  12.  
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.  
  17.         lights = new List<SphericalLightCollider>();
  18.  
  19.         if (characterController == null)
  20.         {
  21.             characterController = this.GetComponent<CharacterController>();
  22.         }
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         if (this.lights.Count > 0)
  29.         {
  30.             TotalLightCollisions = 0;
  31.             Physics.queriesHitTriggers = false;
  32.             foreach (SphericalLightCollider light in this.lights)
  33.             {
  34.                 RaycastHit raycastHit;
  35.                 Ray ray = new Ray(characterController.transform.position, light.gameObject.transform.position - characterController.transform.position);
  36.                 Physics.Raycast(ray, out raycastHit, Vector3.Distance(characterController.transform.position,light.gameObject.transform.position));
  37.                 if (raycastHit.collider == null)
  38.                 {
  39.                     /**
  40.                      * There are no obstructions along the path of the raycast, this means that the player
  41.                      * is completely affected by the light source.
  42.                      */
  43.                     Debug.DrawLine(ray.origin, light.transform.position, Color.green);
  44.                     TotalLightCollisions += 1;
  45.                 }
  46.                 else
  47.                 {
  48.                     /**
  49.                      * There was an obstruction along the path of the raycast. We will not
  50.                      * include this light source.
  51.                      */
  52.                     Debug.DrawLine(ray.origin, light.transform.position, Color.red);
  53.                 }
  54.  
  55.  
  56.  
  57.             }
  58.             Physics.queriesHitTriggers = false;
  59.         }
  60.         /**
  61.          * Illuminate or darken the Stealth Globe based upon our findings.
  62.          */
  63.         if (TotalLightCollisions > 0)
  64.         {
  65.             if (stealthGlobeUIElement != null)
  66.             {
  67.                 if (!stealthGlobeUIElement.IsAnimating)
  68.                 {
  69.                     if (!stealthGlobeUIElement.IsIlluminated)
  70.                     {
  71.                         stealthGlobeUIElement.FadeIn();
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         else
  77.         {
  78.             if (stealthGlobeUIElement != null)
  79.             {
  80.                 if (!stealthGlobeUIElement.IsAnimating)
  81.                 {
  82.                     if (stealthGlobeUIElement.IsIlluminated)
  83.                     {
  84.                         stealthGlobeUIElement.FadeOut();
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     public void OnTriggerEnter(Collider hit)
  92.     {
  93.  
  94.         if (hit != null)
  95.         {
  96.             if (hit.gameObject != null)
  97.             {
  98.                 if (hit is SphereCollider)
  99.                 {
  100.                     SphericalLightCollider lightCollider = hit.GetComponent<SphericalLightCollider>();
  101.                     if (lightCollider != null)
  102.                     {
  103.                         if (!this.lights.Contains(lightCollider))
  104.                         {
  105.                             this.lights.Add(lightCollider);
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     public void OnTriggerExit(Collider hit)
  114.     {
  115.  
  116.         if (hit != null)
  117.         {
  118.             if (hit.gameObject != null)
  119.             {
  120.                 if (hit is SphereCollider)
  121.                 {
  122.                     SphericalLightCollider lightCollider = hit.GetComponent<SphericalLightCollider>();
  123.                     if (lightCollider != null)
  124.                     {
  125.                         this.lights.Remove(lightCollider);
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement