Skip to content

Commit 7519d87

Browse files
authored
Merge pull request #66 from MrGadget1024/master
Updates to NetworkedProximity
2 parents 1ac0f43 + ece2298 commit 7519d87

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MLAPI.MonoBehaviours.Core;
1+
using MLAPI.MonoBehaviours.Core;
22
using System.Collections.Generic;
33
using UnityEngine;
44

@@ -37,26 +37,44 @@ public enum CheckMethod
3737
[Tooltip("How often (in seconds) that this object should update the set of players that can see it.")]
3838
public float VisibilityUpdateInterval = 1.0f; // in seconds
3939

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+
4051
/// <summary>
4152
/// The method to use for checking distance
4253
/// </summary>
4354
[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.")]
4455
public CheckMethod CheckType = CheckMethod.Physics3D;
4556

4657
/// <summary>
47-
/// If enabled, the object will always be hidden from players.
58+
/// Specifies whether this query should hit Triggers.
4859
/// </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 Depth2D depth2D;
5168

69+
/// <summary>
5270
private float lastUpdateTime;
5371

54-
private void Update()
72+
private void FixedUpdate()
5573
{
5674
if (!isServer)
5775
return;
5876

59-
if (Time.time - lastUpdateTime > VisibilityUpdateInterval)
77+
if (NetworkingManager.singleton.NetworkTime - lastUpdateTime > VisibilityUpdateInterval)
6078
{
6179
RebuildObservers();
6280
lastUpdateTime = NetworkingManager.singleton.NetworkTime;
@@ -104,13 +122,19 @@ public override bool OnRebuildObservers(HashSet<uint> observers)
104122
{
105123
case CheckMethod.Physics3D:
106124
{
107-
int hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders);
125+
if (!Physics.CheckSphere(transform.position, Range, layerMask, queryTriggerInteraction3D))
126+
{
127+
// observers was cleared above and there's nothing nearby...short circuit return
128+
return true;
129+
}
130+
131+
int hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D);
108132
//We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found.
109133
if (hits >= colliders.Length)
110134
{
111135
//Resize colliders array
112-
colliders = new Collider[(int)((hits + 2)* 1.3f)];
113-
hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders);
136+
colliders = new Collider[(int)((hits + 2) * 1.3f)];
137+
hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D);
114138
}
115139
for (int i = 0; i < hits; i++)
116140
{
@@ -122,13 +146,13 @@ public override bool OnRebuildObservers(HashSet<uint> observers)
122146
}
123147
case CheckMethod.Physics2D:
124148
{
125-
int hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d);
149+
int hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth);
126150
//We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found.
127151
if (hits >= colliders.Length)
128152
{
129153
//Resize colliders array
130154
colliders2d = new Collider2D[(int)((hits + 2) * 1.3f)];
131-
hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d);
155+
hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth);
132156
}
133157
for (int i = 0; i < hits; i++)
134158
{
@@ -142,4 +166,21 @@ public override bool OnRebuildObservers(HashSet<uint> observers)
142166
return false;
143167
}
144168
}
169+
170+
[System.Serializable]
171+
public class Depth2D
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+
}
145185
}
186+

UnityStupidity.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)