NetworkRigidbody.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using UnityEngine;
  3. namespace Mirror.Experimental
  4. {
  5. [AddComponentMenu("Network/ Experimental/Network Rigidbody")]
  6. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-rigidbody")]
  7. [Obsolete("Use the new NetworkRigidbodyReliable/Unreliable component with Snapshot Interpolation instead.")]
  8. public class NetworkRigidbody : NetworkBehaviour
  9. {
  10. [Header("Settings")]
  11. [SerializeField] internal Rigidbody target = null;
  12. [Tooltip("Set to true if moves come from owner client, set to false if moves always come from server")]
  13. public bool clientAuthority = false;
  14. [Header("Velocity")]
  15. [Tooltip("Syncs Velocity every SyncInterval")]
  16. [SerializeField] bool syncVelocity = true;
  17. [Tooltip("Set velocity to 0 each frame (only works if syncVelocity is false")]
  18. [SerializeField] bool clearVelocity = false;
  19. [Tooltip("Only Syncs Value if distance between previous and current is great than sensitivity")]
  20. [SerializeField] float velocitySensitivity = 0.1f;
  21. [Header("Angular Velocity")]
  22. [Tooltip("Syncs AngularVelocity every SyncInterval")]
  23. [SerializeField] bool syncAngularVelocity = true;
  24. [Tooltip("Set angularVelocity to 0 each frame (only works if syncAngularVelocity is false")]
  25. [SerializeField] bool clearAngularVelocity = false;
  26. [Tooltip("Only Syncs Value if distance between previous and current is great than sensitivity")]
  27. [SerializeField] float angularVelocitySensitivity = 0.1f;
  28. /// <summary>
  29. /// Values sent on client with authority after they are sent to the server
  30. /// </summary>
  31. readonly ClientSyncState previousValue = new ClientSyncState();
  32. protected override void OnValidate()
  33. {
  34. base.OnValidate();
  35. if (target == null)
  36. target = GetComponent<Rigidbody>();
  37. }
  38. #region Sync vars
  39. [SyncVar(hook = nameof(OnVelocityChanged))]
  40. Vector3 velocity;
  41. [SyncVar(hook = nameof(OnAngularVelocityChanged))]
  42. Vector3 angularVelocity;
  43. [SyncVar(hook = nameof(OnIsKinematicChanged))]
  44. bool isKinematic;
  45. [SyncVar(hook = nameof(OnUseGravityChanged))]
  46. bool useGravity;
  47. [SyncVar(hook = nameof(OnuDragChanged))]
  48. float drag;
  49. [SyncVar(hook = nameof(OnAngularDragChanged))]
  50. float angularDrag;
  51. /// <summary>
  52. /// Ignore value if is host or client with Authority
  53. /// </summary>
  54. /// <returns></returns>
  55. bool IgnoreSync => isServer || ClientWithAuthority;
  56. bool ClientWithAuthority => clientAuthority && isOwned;
  57. void OnVelocityChanged(Vector3 _, Vector3 newValue)
  58. {
  59. if (IgnoreSync)
  60. return;
  61. target.velocity = newValue;
  62. }
  63. void OnAngularVelocityChanged(Vector3 _, Vector3 newValue)
  64. {
  65. if (IgnoreSync)
  66. return;
  67. target.angularVelocity = newValue;
  68. }
  69. void OnIsKinematicChanged(bool _, bool newValue)
  70. {
  71. if (IgnoreSync)
  72. return;
  73. target.isKinematic = newValue;
  74. }
  75. void OnUseGravityChanged(bool _, bool newValue)
  76. {
  77. if (IgnoreSync)
  78. return;
  79. target.useGravity = newValue;
  80. }
  81. void OnuDragChanged(float _, float newValue)
  82. {
  83. if (IgnoreSync)
  84. return;
  85. target.drag = newValue;
  86. }
  87. void OnAngularDragChanged(float _, float newValue)
  88. {
  89. if (IgnoreSync)
  90. return;
  91. target.angularDrag = newValue;
  92. }
  93. #endregion
  94. internal void Update()
  95. {
  96. if (isServer)
  97. SyncToClients();
  98. else if (ClientWithAuthority)
  99. SendToServer();
  100. }
  101. internal void FixedUpdate()
  102. {
  103. if (clearAngularVelocity && !syncAngularVelocity)
  104. target.angularVelocity = Vector3.zero;
  105. if (clearVelocity && !syncVelocity)
  106. target.velocity = Vector3.zero;
  107. }
  108. /// <summary>
  109. /// Updates sync var values on server so that they sync to the client
  110. /// </summary>
  111. [Server]
  112. void SyncToClients()
  113. {
  114. // only update if they have changed more than Sensitivity
  115. Vector3 currentVelocity = syncVelocity ? target.velocity : default;
  116. Vector3 currentAngularVelocity = syncAngularVelocity ? target.angularVelocity : default;
  117. bool velocityChanged = syncVelocity && ((previousValue.velocity - currentVelocity).sqrMagnitude > velocitySensitivity * velocitySensitivity);
  118. bool angularVelocityChanged = syncAngularVelocity && ((previousValue.angularVelocity - currentAngularVelocity).sqrMagnitude > angularVelocitySensitivity * angularVelocitySensitivity);
  119. if (velocityChanged)
  120. {
  121. velocity = currentVelocity;
  122. previousValue.velocity = currentVelocity;
  123. }
  124. if (angularVelocityChanged)
  125. {
  126. angularVelocity = currentAngularVelocity;
  127. previousValue.angularVelocity = currentAngularVelocity;
  128. }
  129. // other rigidbody settings
  130. isKinematic = target.isKinematic;
  131. useGravity = target.useGravity;
  132. drag = target.drag;
  133. angularDrag = target.angularDrag;
  134. }
  135. /// <summary>
  136. /// Uses Command to send values to server
  137. /// </summary>
  138. [Client]
  139. void SendToServer()
  140. {
  141. if (!isOwned)
  142. {
  143. Debug.LogWarning("SendToServer called without authority");
  144. return;
  145. }
  146. SendVelocity();
  147. SendRigidBodySettings();
  148. }
  149. [Client]
  150. void SendVelocity()
  151. {
  152. double now = NetworkTime.localTime; // Unity 2019 doesn't have Time.timeAsDouble yet
  153. if (now < previousValue.nextSyncTime)
  154. return;
  155. Vector3 currentVelocity = syncVelocity ? target.velocity : default;
  156. Vector3 currentAngularVelocity = syncAngularVelocity ? target.angularVelocity : default;
  157. bool velocityChanged = syncVelocity && ((previousValue.velocity - currentVelocity).sqrMagnitude > velocitySensitivity * velocitySensitivity);
  158. bool angularVelocityChanged = syncAngularVelocity && ((previousValue.angularVelocity - currentAngularVelocity).sqrMagnitude > angularVelocitySensitivity * angularVelocitySensitivity);
  159. // if angularVelocity has changed it is likely that velocity has also changed so just sync both values
  160. // however if only velocity has changed just send velocity
  161. if (angularVelocityChanged)
  162. {
  163. CmdSendVelocityAndAngular(currentVelocity, currentAngularVelocity);
  164. previousValue.velocity = currentVelocity;
  165. previousValue.angularVelocity = currentAngularVelocity;
  166. }
  167. else if (velocityChanged)
  168. {
  169. CmdSendVelocity(currentVelocity);
  170. previousValue.velocity = currentVelocity;
  171. }
  172. // only update syncTime if either has changed
  173. if (angularVelocityChanged || velocityChanged)
  174. previousValue.nextSyncTime = now + syncInterval;
  175. }
  176. [Client]
  177. void SendRigidBodySettings()
  178. {
  179. // These shouldn't change often so it is ok to send in their own Command
  180. if (previousValue.isKinematic != target.isKinematic)
  181. {
  182. CmdSendIsKinematic(target.isKinematic);
  183. previousValue.isKinematic = target.isKinematic;
  184. }
  185. if (previousValue.useGravity != target.useGravity)
  186. {
  187. CmdSendUseGravity(target.useGravity);
  188. previousValue.useGravity = target.useGravity;
  189. }
  190. if (previousValue.drag != target.drag)
  191. {
  192. CmdSendDrag(target.drag);
  193. previousValue.drag = target.drag;
  194. }
  195. if (previousValue.angularDrag != target.angularDrag)
  196. {
  197. CmdSendAngularDrag(target.angularDrag);
  198. previousValue.angularDrag = target.angularDrag;
  199. }
  200. }
  201. /// <summary>
  202. /// Called when only Velocity has changed on the client
  203. /// </summary>
  204. [Command]
  205. void CmdSendVelocity(Vector3 velocity)
  206. {
  207. // Ignore messages from client if not in client authority mode
  208. if (!clientAuthority)
  209. return;
  210. this.velocity = velocity;
  211. target.velocity = velocity;
  212. }
  213. /// <summary>
  214. /// Called when angularVelocity has changed on the client
  215. /// </summary>
  216. [Command]
  217. void CmdSendVelocityAndAngular(Vector3 velocity, Vector3 angularVelocity)
  218. {
  219. // Ignore messages from client if not in client authority mode
  220. if (!clientAuthority)
  221. return;
  222. if (syncVelocity)
  223. {
  224. this.velocity = velocity;
  225. target.velocity = velocity;
  226. }
  227. this.angularVelocity = angularVelocity;
  228. target.angularVelocity = angularVelocity;
  229. }
  230. [Command]
  231. void CmdSendIsKinematic(bool isKinematic)
  232. {
  233. // Ignore messages from client if not in client authority mode
  234. if (!clientAuthority)
  235. return;
  236. this.isKinematic = isKinematic;
  237. target.isKinematic = isKinematic;
  238. }
  239. [Command]
  240. void CmdSendUseGravity(bool useGravity)
  241. {
  242. // Ignore messages from client if not in client authority mode
  243. if (!clientAuthority)
  244. return;
  245. this.useGravity = useGravity;
  246. target.useGravity = useGravity;
  247. }
  248. [Command]
  249. void CmdSendDrag(float drag)
  250. {
  251. // Ignore messages from client if not in client authority mode
  252. if (!clientAuthority)
  253. return;
  254. this.drag = drag;
  255. target.drag = drag;
  256. }
  257. [Command]
  258. void CmdSendAngularDrag(float angularDrag)
  259. {
  260. // Ignore messages from client if not in client authority mode
  261. if (!clientAuthority)
  262. return;
  263. this.angularDrag = angularDrag;
  264. target.angularDrag = angularDrag;
  265. }
  266. /// <summary>
  267. /// holds previously synced values
  268. /// </summary>
  269. public class ClientSyncState
  270. {
  271. /// <summary>
  272. /// Next sync time that velocity will be synced, based on syncInterval.
  273. /// </summary>
  274. public double nextSyncTime;
  275. public Vector3 velocity;
  276. public Vector3 angularVelocity;
  277. public bool isKinematic;
  278. public bool useGravity;
  279. public float drag;
  280. public float angularDrag;
  281. }
  282. }
  283. }