1
0

NetworkProximityChecker.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Mirror
  5. {
  6. /// <summary>
  7. /// Component that controls visibility of networked objects for players.
  8. /// <para>Any object with this component on it will not be visible to players more than a (configurable) distance away.</para>
  9. /// </summary>
  10. // Deprecated 2021-07-13
  11. [Obsolete(NetworkVisibilityObsoleteMessage.Message)]
  12. [AddComponentMenu("Network/NetworkProximityChecker")]
  13. [RequireComponent(typeof(NetworkIdentity))]
  14. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-proximity-checker")]
  15. public class NetworkProximityChecker : NetworkVisibility
  16. {
  17. /// <summary>
  18. /// The maximum range that objects will be visible at.
  19. /// </summary>
  20. [Tooltip("The maximum range that objects will be visible at.")]
  21. public int visRange = 10;
  22. /// <summary>
  23. /// How often (in seconds) that this object should update the list of observers that can see it.
  24. /// </summary>
  25. [Tooltip("How often (in seconds) that this object should update the list of observers that can see it.")]
  26. public float visUpdateInterval = 1;
  27. public override void OnStartServer()
  28. {
  29. InvokeRepeating(nameof(RebuildObservers), 0, visUpdateInterval);
  30. }
  31. public override void OnStopServer()
  32. {
  33. CancelInvoke(nameof(RebuildObservers));
  34. }
  35. void RebuildObservers()
  36. {
  37. netIdentity.RebuildObservers(false);
  38. }
  39. /// <summary>
  40. /// Callback used by the visibility system to determine if an observer (player) can see this object.
  41. /// <para>If this function returns true, the network connection will be added as an observer.</para>
  42. /// </summary>
  43. /// <param name="conn">Network connection of a player.</param>
  44. /// <returns>True if the player can see this object.</returns>
  45. public override bool OnCheckObserver(NetworkConnection conn)
  46. {
  47. return Vector3.Distance(conn.identity.transform.position, transform.position) < visRange;
  48. }
  49. /// <summary>
  50. /// Callback used by the visibility system to (re)construct the set of observers that can see this object.
  51. /// <para>Implementations of this callback should add network connections of players that can see this object to the observers set.</para>
  52. /// </summary>
  53. /// <param name="observers">The new set of observers for this object.</param>
  54. /// <param name="initialize">True if the set of observers is being built for the first time.</param>
  55. public override void OnRebuildObservers(HashSet<NetworkConnection> observers, bool initialize)
  56. {
  57. // if force hidden then return without adding any observers.
  58. // 'transform.' calls GetComponent, only do it once
  59. Vector3 position = transform.position;
  60. // brute force distance check
  61. // -> only player connections can be observers, so it's enough if we
  62. // go through all connections instead of all spawned identities.
  63. // -> compared to UNET's sphere cast checking, this one is orders of
  64. // magnitude faster. if we have 10k monsters and run a sphere
  65. // cast 10k times, we will see a noticeable lag even with physics
  66. // layers. but checking to every connection is fast.
  67. foreach (NetworkConnectionToClient conn in NetworkServer.connections.Values)
  68. {
  69. if (conn != null && conn.identity != null)
  70. {
  71. // check distance
  72. if (Vector3.Distance(conn.identity.transform.position, position) < visRange)
  73. {
  74. observers.Add(conn);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }