@@ -55,20 +55,32 @@ public enum CheckMethod
55
55
public CheckMethod CheckType = CheckMethod . Physics3D ;
56
56
57
57
/// <summary>
58
- /// Specifies whether this query should hit Triggers (Physics3D only .
58
+ /// Specifies whether this query should hit Triggers.
59
59
/// </summary>
60
60
[ Tooltip ( "Specifies whether this query should hit Triggers (Physics3D only)." ) ]
61
61
public QueryTriggerInteraction queryTriggerInteraction3D = QueryTriggerInteraction . UseGlobal ;
62
62
63
63
/// <summary>
64
- /// Min / Max depth range (Physics2D only).
64
+ /// Min / Max depth range (2D only).
65
65
/// </summary>
66
66
[ Tooltip ( "Min / Max depth range (Physics2D only)." ) ]
67
67
public Depth2D depth2D ;
68
68
69
69
/// <summary>
70
70
private float lastUpdateTime ;
71
71
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
+
72
84
/// <summary>
73
85
/// Called when a new client connects
74
86
/// </summary>
@@ -106,67 +118,68 @@ public override bool OnRebuildObservers(HashSet<uint> observers)
106
118
return true ;
107
119
}
108
120
109
- if ( Time . time - lastUpdateTime > VisibilityUpdateInterval )
121
+ switch ( CheckType )
110
122
{
111
- switch ( CheckType )
112
- {
113
- case CheckMethod . Physics3D :
123
+ case CheckMethod . Physics3D :
124
+ {
125
+ if ( ! Physics . CheckSphere ( transform . position , Range , layerMask , queryTriggerInteraction3D ) )
114
126
{
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
130
128
return true ;
131
129
}
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 )
133
134
{
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 ) ;
150
156
}
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
+ }
152
165
}
153
166
return false ;
154
167
}
155
168
}
169
+ }
156
170
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 ;
165
179
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 ;
172
185
}
0 commit comments