From 277f18280ed9efd84342b539da91bf5ed512bef9 Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 9 Oct 2018 10:56:31 +0200 Subject: [PATCH] Added comments for OnRebuildObservers return value and adjusted NetworkProximityChecker to make this less weird. --- Mirror/Runtime/NetworkBehaviour.cs | 2 + Mirror/Runtime/NetworkProximityChecker.cs | 64 +++++++++++++---------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/Mirror/Runtime/NetworkBehaviour.cs b/Mirror/Runtime/NetworkBehaviour.cs index ee9fc6a94..e58b7067c 100644 --- a/Mirror/Runtime/NetworkBehaviour.cs +++ b/Mirror/Runtime/NetworkBehaviour.cs @@ -571,6 +571,8 @@ public virtual void OnStopAuthority() { } + // return true when overwriting so that Mirror knows that we wanted to + // rebuild observers ourselves. otherwise it uses built in rebuild. public virtual bool OnRebuildObservers(HashSet observers, bool initialize) { return false; diff --git a/Mirror/Runtime/NetworkProximityChecker.cs b/Mirror/Runtime/NetworkProximityChecker.cs index 6cc3c2546..ce8d2ad21 100644 --- a/Mirror/Runtime/NetworkProximityChecker.cs +++ b/Mirror/Runtime/NetworkProximityChecker.cs @@ -58,6 +58,7 @@ public override bool OnCheckObserver(NetworkConnection newObserver) public override bool OnRebuildObservers(HashSet observers, bool initial) { + // only add self as observer if force hidden if (forceHidden) { // ensure player can still see themself @@ -66,49 +67,54 @@ public override bool OnRebuildObservers(HashSet observers, bo { observers.Add(uv.connectionToClient); } - return true; } - - // find players within range - switch (checkMethod) + // otherwise add everyone in proximity + else { - case CheckMethod.Physics3D: + // find players within range + switch (checkMethod) { - Collider[] hits = Physics.OverlapSphere(transform.position, visRange, castLayers); - for (int i = 0; i < hits.Length; i++) + case CheckMethod.Physics3D: { - Collider hit = hits[i]; - // collider might be on pelvis, often the NetworkIdentity is in a parent - // (looks in the object itself and then parents) - NetworkIdentity uv = hit.GetComponentInParent(); - // (if an object has a connectionToClient, it is a player) - if (uv != null && uv.connectionToClient != null) + Collider[] hits = Physics.OverlapSphere(transform.position, visRange, castLayers); + for (int i = 0; i < hits.Length; i++) { - observers.Add(uv.connectionToClient); + Collider hit = hits[i]; + // collider might be on pelvis, often the NetworkIdentity is in a parent + // (looks in the object itself and then parents) + NetworkIdentity uv = hit.GetComponentInParent(); + // (if an object has a connectionToClient, it is a player) + if (uv != null && uv.connectionToClient != null) + { + observers.Add(uv.connectionToClient); + } } + break; } - return true; - } - case CheckMethod.Physics2D: - { - Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, visRange, castLayers); - for (int i = 0; i < hits.Length; i++) + case CheckMethod.Physics2D: { - Collider2D hit = hits[i]; - // collider might be on pelvis, often the NetworkIdentity is in a parent - // (looks in the object itself and then parents) - NetworkIdentity uv = hit.GetComponentInParent(); - // (if an object has a connectionToClient, it is a player) - if (uv != null && uv.connectionToClient != null) + Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, visRange, castLayers); + for (int i = 0; i < hits.Length; i++) { - observers.Add(uv.connectionToClient); + Collider2D hit = hits[i]; + // collider might be on pelvis, often the NetworkIdentity is in a parent + // (looks in the object itself and then parents) + NetworkIdentity uv = hit.GetComponentInParent(); + // (if an object has a connectionToClient, it is a player) + if (uv != null && uv.connectionToClient != null) + { + observers.Add(uv.connectionToClient); + } } + break; } - return true; } } - return false; + + // always return true when overwriting OnRebuildObservers so that + // Mirror knows not to use the built in rebuild method. + return true; } // called hiding and showing objects on the host