Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraLightDetector : MonoBehaviour
- {
- public StealthGlobe stealthGlobeUIElement;
- private CharacterController characterController;
- public bool IsPlayerIlluminated { get; private set; }
- public int TotalLightCollisions { get; private set; }
- private List<SphericalLightCollider> lights;
- // Start is called before the first frame update
- void Start()
- {
- lights = new List<SphericalLightCollider>();
- if (characterController == null)
- {
- characterController = this.GetComponent<CharacterController>();
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (this.lights.Count > 0)
- {
- TotalLightCollisions = 0;
- Physics.queriesHitTriggers = false;
- foreach (SphericalLightCollider light in this.lights)
- {
- RaycastHit raycastHit;
- Ray ray = new Ray(characterController.transform.position, light.gameObject.transform.position - characterController.transform.position);
- Physics.Raycast(ray, out raycastHit, Vector3.Distance(characterController.transform.position,light.gameObject.transform.position));
- if (raycastHit.collider == null)
- {
- /**
- * There are no obstructions along the path of the raycast, this means that the player
- * is completely affected by the light source.
- */
- Debug.DrawLine(ray.origin, light.transform.position, Color.green);
- TotalLightCollisions += 1;
- }
- else
- {
- /**
- * There was an obstruction along the path of the raycast. We will not
- * include this light source.
- */
- Debug.DrawLine(ray.origin, light.transform.position, Color.red);
- }
- }
- Physics.queriesHitTriggers = false;
- }
- /**
- * Illuminate or darken the Stealth Globe based upon our findings.
- */
- if (TotalLightCollisions > 0)
- {
- if (stealthGlobeUIElement != null)
- {
- if (!stealthGlobeUIElement.IsAnimating)
- {
- if (!stealthGlobeUIElement.IsIlluminated)
- {
- stealthGlobeUIElement.FadeIn();
- }
- }
- }
- }
- else
- {
- if (stealthGlobeUIElement != null)
- {
- if (!stealthGlobeUIElement.IsAnimating)
- {
- if (stealthGlobeUIElement.IsIlluminated)
- {
- stealthGlobeUIElement.FadeOut();
- }
- }
- }
- }
- }
- public void OnTriggerEnter(Collider hit)
- {
- if (hit != null)
- {
- if (hit.gameObject != null)
- {
- if (hit is SphereCollider)
- {
- SphericalLightCollider lightCollider = hit.GetComponent<SphericalLightCollider>();
- if (lightCollider != null)
- {
- if (!this.lights.Contains(lightCollider))
- {
- this.lights.Add(lightCollider);
- }
- }
- }
- }
- }
- }
- public void OnTriggerExit(Collider hit)
- {
- if (hit != null)
- {
- if (hit.gameObject != null)
- {
- if (hit is SphereCollider)
- {
- SphericalLightCollider lightCollider = hit.GetComponent<SphericalLightCollider>();
- if (lightCollider != null)
- {
- this.lights.Remove(lightCollider);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement