|
1 | | -using MLAPI.MonoBehaviours.Core; |
| 1 | +using MLAPI.MonoBehaviours.Core; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using UnityEngine; |
4 | 4 |
|
@@ -37,31 +37,49 @@ public enum CheckMethod |
37 | 37 | [Tooltip("How often (in seconds) that this object should update the set of players that can see it.")] |
38 | 38 | public float VisibilityUpdateInterval = 1.0f; // in seconds |
39 | 39 |
|
| 40 | + /// <summary> |
| 41 | + /// Filter to check objects only on specific layers. |
| 42 | + /// </summary> |
| 43 | + [Tooltip("Filter to check objects only on specific layers.")] |
| 44 | + public LayerMask layerMask; |
| 45 | + |
| 46 | + /// If enabled, the object will always be hidden from players. |
| 47 | + /// </summary> |
| 48 | + [Tooltip("Enable to force this object to be hidden from players.")] |
| 49 | + public bool ForceHidden = false; |
| 50 | + |
40 | 51 | /// <summary> |
41 | 52 | /// The method to use for checking distance |
42 | 53 | /// </summary> |
43 | 54 | [Tooltip("Which method to use for checking proximity of players.\n\nPhysics3D uses 3D physics to determine proximity.\n\nPhysics2D uses 2D physics to determine proximity.")] |
44 | 55 | public CheckMethod CheckType = CheckMethod.Physics3D; |
45 | 56 |
|
46 | 57 | /// <summary> |
47 | | - /// If enabled, the object will always be hidden from players. |
| 58 | + /// Specifies whether this query should hit Triggers. |
48 | 59 | /// </summary> |
49 | | - [Tooltip("Enable to force this object to be hidden from players.")] |
50 | | - public bool ForceHidden = false; |
| 60 | + [Tooltip("Specifies whether this query should hit Triggers (Physics3D only).")] |
| 61 | + public QueryTriggerInteraction queryTriggerInteraction3D = QueryTriggerInteraction.UseGlobal; |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Min / Max depth range (2D only). |
| 65 | + /// </summary> |
| 66 | + [Tooltip("Min / Max depth range (Physics2D only).")] |
| 67 | + public MinMax2D depth2D; |
51 | 68 |
|
| 69 | + /// <summary> |
52 | 70 | private float lastUpdateTime; |
53 | 71 |
|
54 | | - private void Update() |
55 | | - { |
56 | | - if (!isServer) |
57 | | - return; |
| 72 | + //private void FixedUpdate() |
| 73 | + //{ |
| 74 | + // if (!isServer) |
| 75 | + // return; |
58 | 76 |
|
59 | | - if (Time.time - lastUpdateTime > VisibilityUpdateInterval) |
60 | | - { |
61 | | - RebuildObservers(); |
62 | | - lastUpdateTime = NetworkingManager.singleton.NetworkTime; |
63 | | - } |
64 | | - } |
| 77 | + // if (Time.time - lastUpdateTime > VisibilityUpdateInterval) |
| 78 | + // { |
| 79 | + // RebuildObservers(); |
| 80 | + // lastUpdateTime = NetworkingManager.singleton.NetworkTime; |
| 81 | + // } |
| 82 | + //} |
65 | 83 |
|
66 | 84 | /// <summary> |
67 | 85 | /// Called when a new client connects |
@@ -100,46 +118,67 @@ public override bool OnRebuildObservers(HashSet<uint> observers) |
100 | 118 | return true; |
101 | 119 | } |
102 | 120 |
|
103 | | - switch (CheckType) |
| 121 | + if (Time.time - lastUpdateTime > VisibilityUpdateInterval) |
104 | 122 | { |
105 | | - case CheckMethod.Physics3D: |
106 | | - { |
107 | | - int hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders); |
108 | | - //We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found. |
109 | | - if (hits >= colliders.Length) |
110 | | - { |
111 | | - //Resize colliders array |
112 | | - colliders = new Collider[(int)((hits + 2)* 1.3f)]; |
113 | | - hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders); |
114 | | - } |
115 | | - for (int i = 0; i < hits; i++) |
116 | | - { |
117 | | - var uv = colliders[i].GetComponent<NetworkedObject>(); |
118 | | - if (uv != null && uv.isPlayerObject) |
119 | | - observers.Add(uv.OwnerClientId); |
120 | | - } |
121 | | - return true; |
122 | | - } |
123 | | - case CheckMethod.Physics2D: |
124 | | - { |
125 | | - int hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d); |
126 | | - //We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found. |
127 | | - if (hits >= colliders.Length) |
| 123 | + switch (CheckType) |
| 124 | + { |
| 125 | + case CheckMethod.Physics3D: |
128 | 126 | { |
129 | | - //Resize colliders array |
130 | | - colliders2d = new Collider2D[(int)((hits + 2) * 1.3f)]; |
131 | | - hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d); |
| 127 | + int hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D); |
| 128 | + //We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found. |
| 129 | + if (hits >= colliders.Length) |
| 130 | + { |
| 131 | + //Resize colliders array |
| 132 | + colliders = new Collider[(int)((hits + 2) * 1.3f)]; |
| 133 | + hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D); |
| 134 | + } |
| 135 | + for (int i = 0; i < hits; i++) |
| 136 | + { |
| 137 | + var uv = colliders[i].GetComponent<NetworkedObject>(); |
| 138 | + if (uv != null && uv.isPlayerObject) |
| 139 | + observers.Add(uv.OwnerClientId); |
| 140 | + } |
| 141 | + lastUpdateTime = NetworkingManager.singleton.NetworkTime; |
| 142 | + return true; |
132 | 143 | } |
133 | | - for (int i = 0; i < hits; i++) |
| 144 | + case CheckMethod.Physics2D: |
134 | 145 | { |
135 | | - var uv = colliders2d[i].GetComponent<NetworkedObject>(); |
136 | | - if (uv != null && (uv.isPlayerObject)) |
137 | | - observers.Add(uv.OwnerClientId); |
| 146 | + int hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth); |
| 147 | + //We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found. |
| 148 | + if (hits >= colliders.Length) |
| 149 | + { |
| 150 | + //Resize colliders array |
| 151 | + colliders2d = new Collider2D[(int)((hits + 2) * 1.3f)]; |
| 152 | + hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth); |
| 153 | + } |
| 154 | + for (int i = 0; i < hits; i++) |
| 155 | + { |
| 156 | + var uv = colliders2d[i].GetComponent<NetworkedObject>(); |
| 157 | + if (uv != null && (uv.isPlayerObject)) |
| 158 | + observers.Add(uv.OwnerClientId); |
| 159 | + } |
| 160 | + lastUpdateTime = NetworkingManager.singleton.NetworkTime; |
| 161 | + return true; |
138 | 162 | } |
139 | | - return true; |
140 | | - } |
| 163 | + } |
141 | 164 | } |
142 | 165 | return false; |
143 | 166 | } |
144 | 167 | } |
145 | 168 | } |
| 169 | + |
| 170 | +[System.Serializable] |
| 171 | +public class MinMax2D |
| 172 | +{ |
| 173 | + /// <summary> |
| 174 | + /// Only include objects with a Z coordinate (depth) greater than or equal to this value. |
| 175 | + /// </summary> |
| 176 | + [Tooltip("Only include objects with a Z coordinate (depth) greater than or equal to this value.")] |
| 177 | + public float minDepth = -Mathf.Infinity; |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Only include objects with a Z coordinate (depth) less than or equal to this value. |
| 181 | + /// </summary> |
| 182 | + [Tooltip("Only include objects with a Z coordinate (depth) less than or equal to this value.")] |
| 183 | + public float maxDepth = Mathf.Infinity; |
| 184 | +} |
0 commit comments