Skip to content

Commit

Permalink
fix: #692 by always adding connectionToClient when rebuilding observers
Browse files Browse the repository at this point in the history
  • Loading branch information
miwarnec committed Apr 1, 2019
1 parent 8f3c8ed commit ab44ac8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
78 changes: 34 additions & 44 deletions Assets/Mirror/Components/NetworkProximityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,52 @@ public override bool OnCheckObserver(NetworkConnection newObserver)

public override bool OnRebuildObservers(HashSet<NetworkConnection> observers, bool initial)
{
// only add self as observer if force hidden
if (forceHidden)
return false;

// find players within range
switch (checkMethod)
{
// ensure player can still see themself
if (connectionToClient != null)
{
observers.Add(connectionToClient);
}
}
// otherwise add everyone in proximity
else
{
// find players within range
switch (checkMethod)
case CheckMethod.Physics3D:
{
case CheckMethod.Physics3D:
{
// cast without allocating GC for maximum performance
int hitCount = Physics.OverlapSphereNonAlloc(transform.position, visRange, hitsBuffer3D, castLayers);
if (hitCount == hitsBuffer3D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapSphere test for " + name + " has filled the whole buffer(" + hitsBuffer3D.Length + "). Some results might have been omitted. Consider increasing buffer size.");
// cast without allocating GC for maximum performance
int hitCount = Physics.OverlapSphereNonAlloc(transform.position, visRange, hitsBuffer3D, castLayers);
if (hitCount == hitsBuffer3D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapSphere test for " + name + " has filled the whole buffer(" + hitsBuffer3D.Length + "). Some results might have been omitted. Consider increasing buffer size.");

for (int i = 0; i < hitCount; i++)
for (int i = 0; i < hitCount; i++)
{
Collider hit = hitsBuffer3D[i];
// collider might be on pelvis, often the NetworkIdentity is in a parent
// (looks in the object itself and then parents)
NetworkIdentity identity = hit.GetComponentInParent<NetworkIdentity>();
// (if an object has a connectionToClient, it is a player)
if (identity != null && identity.connectionToClient != null)
{
Collider hit = hitsBuffer3D[i];
// collider might be on pelvis, often the NetworkIdentity is in a parent
// (looks in the object itself and then parents)
NetworkIdentity identity = hit.GetComponentInParent<NetworkIdentity>();
// (if an object has a connectionToClient, it is a player)
if (identity != null && identity.connectionToClient != null)
{
observers.Add(identity.connectionToClient);
}
observers.Add(identity.connectionToClient);
}
break;
}
break;
}

case CheckMethod.Physics2D:
{
// cast without allocating GC for maximum performance
int hitCount = Physics2D.OverlapCircleNonAlloc(transform.position, visRange, hitsBuffer2D, castLayers);
if (hitCount == hitsBuffer2D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapCircle test for " + name + " has filled the whole buffer(" + hitsBuffer2D.Length + "). Some results might have been omitted. Consider increasing buffer size.");
case CheckMethod.Physics2D:
{
// cast without allocating GC for maximum performance
int hitCount = Physics2D.OverlapCircleNonAlloc(transform.position, visRange, hitsBuffer2D, castLayers);
if (hitCount == hitsBuffer2D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapCircle test for " + name + " has filled the whole buffer(" + hitsBuffer2D.Length + "). Some results might have been omitted. Consider increasing buffer size.");

for (int i = 0; i < hitCount; i++)
for (int i = 0; i < hitCount; i++)
{
Collider2D hit = hitsBuffer2D[i];
// collider might be on pelvis, often the NetworkIdentity is in a parent
// (looks in the object itself and then parents)
NetworkIdentity identity = hit.GetComponentInParent<NetworkIdentity>();
// (if an object has a connectionToClient, it is a player)
if (identity != null && identity.connectionToClient != null)
{
Collider2D hit = hitsBuffer2D[i];
// collider might be on pelvis, often the NetworkIdentity is in a parent
// (looks in the object itself and then parents)
NetworkIdentity identity = hit.GetComponentInParent<NetworkIdentity>();
// (if an object has a connectionToClient, it is a player)
if (identity != null && identity.connectionToClient != null)
{
observers.Add(identity.connectionToClient);
}
observers.Add(identity.connectionToClient);
}
break;
}
break;
}
}

Expand Down
9 changes: 9 additions & 0 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,15 @@ public void RebuildObservers(bool initialize)
result |= comp.OnRebuildObservers(newObservers, initialize);
}

// if player connection: ensure player always see himself no matter what.
// -> fixes https://github.com/vis2k/Mirror/issues/692 where a
// player might teleport out of the ProximityChecker's cast,
// losing the own connection as observer.
if (connectionToClient != null && connectionToClient.isReady)
{
newObservers.Add(connectionToClient);
}

// if no component implemented OnRebuildObservers, then add all
// connections.
if (!result)
Expand Down

0 comments on commit ab44ac8

Please sign in to comment.