diff --git a/Assets/Mirror/Components/InterestManagement/Distance/DistanceInterestManagement.cs b/Assets/Mirror/Components/InterestManagement/Distance/DistanceInterestManagement.cs index e435d1e22..111054a6e 100644 --- a/Assets/Mirror/Components/InterestManagement/Distance/DistanceInterestManagement.cs +++ b/Assets/Mirror/Components/InterestManagement/Distance/DistanceInterestManagement.cs @@ -6,21 +6,30 @@ namespace Mirror { public class DistanceInterestManagement : InterestManagement { - [Tooltip("The maximum range that objects will be visible at.")] + [Tooltip("The maximum range that objects will be visible at. Add DistanceInterestManagementCustomRange onto NetworkIdentities for custom ranges.")] public int visRange = 10; [Tooltip("Rebuild all every 'rebuildInterval' seconds.")] public float rebuildInterval = 1; double lastRebuildTime; + // helper function to get vis range for a given object, or default. + int GetVisRange(NetworkIdentity identity) + { + DistanceInterestManagementCustomRange custom = identity.GetComponent(); + return custom != null ? custom.visRange : visRange; + } + public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnection newObserver) { - return Vector3.Distance(identity.transform.position, newObserver.identity.transform.position) < visRange; + int range = GetVisRange(identity); + return Vector3.Distance(identity.transform.position, newObserver.identity.transform.position) < range; } public override void OnRebuildObservers(NetworkIdentity identity, HashSet newObservers, bool initialize) { - // 'transform.' calls GetComponent, only do it once + // cache range and .transform because both call GetComponent. + int range = GetVisRange(identity); Vector3 position = identity.transform.position; // brute force distance check @@ -36,7 +45,7 @@ public override void OnRebuildObservers(NetworkIdentity identity, HashSet