Documentation for the Unity C# Library
Loading...
Searching...
No Matches
VisibilityControlPoint.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using TMPro;
4using UnityEngine;
5using UnityEngine.UI;
6
8{
9 public class VisibilityControlPoint : MonoBehaviour
10 {
11 private Camera _camera;
12 private Transform _cameraTransform;
13
14 private const string UI_LAYER_NAME = "UI";
15
16 public void Start()
17 {
20 }
21
22 public void Update()
23 {
24 var movableUIElement = PingInCameraView();
25
26 if (movableUIElement != null)
27 {
28 movableUIElement.ProcessIntersection();
29 }
30 }
31
33 {
34 Vector3 viewport = _camera.WorldToViewportPoint(transform.position);
35 bool inCameraFrustum = IsInRange01(viewport.x) && IsInRange01(viewport.y);
36 bool inFrontOfCamera = viewport.z > 0;
37
38 if (inCameraFrustum && inFrontOfCamera)
39 {
40 RaycastHit hitInfo;
41 bool objectBlockingPoint = false;
42
43 Vector3 directionBetween = transform.position - _cameraTransform.position;
44 directionBetween = directionBetween.normalized;
45
46 float distance = Vector3.Distance(_cameraTransform.position, transform.position);
47
48 var layer_mask = LayerMask.GetMask(UI_LAYER_NAME);
49
50 if(Physics.Raycast(_cameraTransform.position, directionBetween, out hitInfo, distance + 0.05f, layer_mask)) {
51 if(hitInfo.point != transform.position) {
52 return hitInfo.collider.GetComponent<IMovableUIElement>();
53 }
54 }
55 }
56
57 return null;
58 }
59
60 private bool IsInRange01(float a) {
61 return a > 0 && a < 1;
62 }
63 }
64}