Skip to content

Commit a37ff36

Browse files
author
Chris Langsenkamp
authored
Exposed additional properties and removed Update()
Exposed layerMask for OverlapSphereNonAlloc and OverlapCircleNonAlloc. Exposed queryTriggerInteraction for OverlapSphereNonAlloc. Exposed minDepth and maxDepth for OverlapCircleNonAlloc. Slightly rearranged properties to be a bit more logical. Update should've been FixedUpdate to begin with, but I've eliminated it in favor of checking the time since last build of observers and only rebuilding if it has gone stale.
1 parent 48722a4 commit a37ff36

File tree

1 file changed

+86
-47
lines changed

1 file changed

+86
-47
lines changed

MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs

Lines changed: 86 additions & 47 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,31 +37,49 @@ 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 MinMax2D depth2D;
5168

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

54-
private void Update()
55-
{
56-
if (!isServer)
57-
return;
72+
//private void FixedUpdate()
73+
//{
74+
// if (!isServer)
75+
// return;
5876

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+
//}
6583

6684
/// <summary>
6785
/// Called when a new client connects
@@ -100,46 +118,67 @@ public override bool OnRebuildObservers(HashSet<uint> observers)
100118
return true;
101119
}
102120

103-
switch (CheckType)
121+
if (Time.time - lastUpdateTime > VisibilityUpdateInterval)
104122
{
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:
128126
{
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;
132143
}
133-
for (int i = 0; i < hits; i++)
144+
case CheckMethod.Physics2D:
134145
{
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;
138162
}
139-
return true;
140-
}
163+
}
141164
}
142165
return false;
143166
}
144167
}
145168
}
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

Comments
 (0)