Skip to content

Commit 0e967a6

Browse files
author
Chris Langsenkamp
authored
Update NetworkedProximity.cs
1 parent 5ae7852 commit 0e967a6

File tree

1 file changed

+66
-53
lines changed

1 file changed

+66
-53
lines changed

MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,32 @@ public enum CheckMethod
5555
public CheckMethod CheckType = CheckMethod.Physics3D;
5656

5757
/// <summary>
58-
/// Specifies whether this query should hit Triggers (Physics3D only.
58+
/// Specifies whether this query should hit Triggers.
5959
/// </summary>
6060
[Tooltip("Specifies whether this query should hit Triggers (Physics3D only).")]
6161
public QueryTriggerInteraction queryTriggerInteraction3D = QueryTriggerInteraction.UseGlobal;
6262

6363
/// <summary>
64-
/// Min / Max depth range (Physics2D only).
64+
/// Min / Max depth range (2D only).
6565
/// </summary>
6666
[Tooltip("Min / Max depth range (Physics2D only).")]
6767
public Depth2D depth2D;
6868

6969
/// <summary>
7070
private float lastUpdateTime;
7171

72+
private void FixedUpdate()
73+
{
74+
if (!isServer)
75+
return;
76+
77+
if (Time.time - lastUpdateTime > VisibilityUpdateInterval)
78+
{
79+
RebuildObservers();
80+
lastUpdateTime = NetworkingManager.singleton.NetworkTime;
81+
}
82+
}
83+
7284
/// <summary>
7385
/// Called when a new client connects
7486
/// </summary>
@@ -106,67 +118,68 @@ public override bool OnRebuildObservers(HashSet<uint> observers)
106118
return true;
107119
}
108120

109-
if (Time.time - lastUpdateTime > VisibilityUpdateInterval)
121+
switch (CheckType)
110122
{
111-
switch (CheckType)
112-
{
113-
case CheckMethod.Physics3D:
123+
case CheckMethod.Physics3D:
124+
{
125+
if (!Physics.CheckSphere(transform.position, Range, layerMask, queryTriggerInteraction3D))
114126
{
115-
int hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D);
116-
//We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found.
117-
if (hits >= colliders.Length)
118-
{
119-
//Resize colliders array
120-
colliders = new Collider[(int)((hits + 2) * 1.3f)];
121-
hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D);
122-
}
123-
for (int i = 0; i < hits; i++)
124-
{
125-
var uv = colliders[i].GetComponent<NetworkedObject>();
126-
if (uv != null && uv.isPlayerObject)
127-
observers.Add(uv.OwnerClientId);
128-
}
129-
lastUpdateTime = NetworkingManager.singleton.NetworkTime;
127+
// observers was cleared above and there's nothing nearby...short circuit return
130128
return true;
131129
}
132-
case CheckMethod.Physics2D:
130+
131+
int hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D);
132+
//We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found.
133+
if (hits >= colliders.Length)
133134
{
134-
int hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth);
135-
//We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found.
136-
if (hits >= colliders.Length)
137-
{
138-
//Resize colliders array
139-
colliders2d = new Collider2D[(int)((hits + 2) * 1.3f)];
140-
hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth);
141-
}
142-
for (int i = 0; i < hits; i++)
143-
{
144-
var uv = colliders2d[i].GetComponent<NetworkedObject>();
145-
if (uv != null && (uv.isPlayerObject))
146-
observers.Add(uv.OwnerClientId);
147-
}
148-
lastUpdateTime = NetworkingManager.singleton.NetworkTime;
149-
return true;
135+
//Resize colliders array
136+
colliders = new Collider[(int)((hits + 2) * 1.3f)];
137+
hits = Physics.OverlapSphereNonAlloc(transform.position, Range, colliders, layerMask, queryTriggerInteraction3D);
138+
}
139+
for (int i = 0; i < hits; i++)
140+
{
141+
var uv = colliders[i].GetComponent<NetworkedObject>();
142+
if (uv != null && uv.isPlayerObject)
143+
observers.Add(uv.OwnerClientId);
144+
}
145+
return true;
146+
}
147+
case CheckMethod.Physics2D:
148+
{
149+
int hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth);
150+
//We check if it's equal to since the OverlapSphereNonAlloc only returns what it actually wrote, not what it found.
151+
if (hits >= colliders.Length)
152+
{
153+
//Resize colliders array
154+
colliders2d = new Collider2D[(int)((hits + 2) * 1.3f)];
155+
hits = Physics2D.OverlapCircleNonAlloc(transform.position, Range, colliders2d, layerMask, depth2D.minDepth, depth2D.maxDepth);
150156
}
151-
}
157+
for (int i = 0; i < hits; i++)
158+
{
159+
var uv = colliders2d[i].GetComponent<NetworkedObject>();
160+
if (uv != null && (uv.isPlayerObject))
161+
observers.Add(uv.OwnerClientId);
162+
}
163+
return true;
164+
}
152165
}
153166
return false;
154167
}
155168
}
169+
}
156170

157-
[System.Serializable]
158-
public class Depth2D
159-
{
160-
/// <summary>
161-
/// Only include objects with a Z coordinate (depth) greater than or equal to this value.
162-
/// </summary>
163-
[Tooltip("Only include objects with a Z coordinate (depth) greater than or equal to this value.")]
164-
public float minDepth = -Mathf.Infinity;
171+
[System.Serializable]
172+
public class Depth2D
173+
{
174+
/// <summary>
175+
/// Only include objects with a Z coordinate (depth) greater than or equal to this value.
176+
/// </summary>
177+
[Tooltip("Only include objects with a Z coordinate (depth) greater than or equal to this value.")]
178+
public float minDepth = -Mathf.Infinity;
165179

166-
/// <summary>
167-
/// Only include objects with a Z coordinate (depth) less than or equal to this value.
168-
/// </summary>
169-
[Tooltip("Only include objects with a Z coordinate (depth) less than or equal to this value.")]
170-
public float maxDepth = Mathf.Infinity;
171-
}
180+
/// <summary>
181+
/// Only include objects with a Z coordinate (depth) less than or equal to this value.
182+
/// </summary>
183+
[Tooltip("Only include objects with a Z coordinate (depth) less than or equal to this value.")]
184+
public float maxDepth = Mathf.Infinity;
172185
}

0 commit comments

Comments
 (0)