NetworkRigidbodyUnreliable.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. namespace Mirror
  3. {
  4. // [RequireComponent(typeof(Rigidbody))] <- OnValidate ensures this is on .target
  5. public class NetworkRigidbodyUnreliable : NetworkTransformUnreliable
  6. {
  7. bool clientAuthority => syncDirection == SyncDirection.ClientToServer;
  8. Rigidbody rb;
  9. bool wasKinematic;
  10. // cach Rigidbody and original isKinematic setting
  11. protected override void Awake()
  12. {
  13. // we can't overwrite .target to be a Rigidbody.
  14. // but we can use its Rigidbody component.
  15. rb = target.GetComponent<Rigidbody>();
  16. if (rb == null)
  17. {
  18. Debug.LogError($"{name}'s NetworkRigidbody.target {target.name} is missing a Rigidbody", this);
  19. return;
  20. }
  21. wasKinematic = rb.isKinematic;
  22. base.Awake();
  23. }
  24. // reset forced isKinematic flag to original.
  25. // otherwise the overwritten value would remain between sessions forever.
  26. // for example, a game may run as client, set rigidbody.iskinematic=true,
  27. // then run as server, where .iskinematic isn't touched and remains at
  28. // the overwritten=true, even though the user set it to false originally.
  29. public override void OnStopServer() => rb.isKinematic = wasKinematic;
  30. public override void OnStopClient() => rb.isKinematic = wasKinematic;
  31. // overwriting Construct() and Apply() to set Rigidbody.MovePosition
  32. // would give more jittery movement.
  33. // FixedUpdate for physics
  34. void FixedUpdate()
  35. {
  36. // who ever has authority moves the Rigidbody with physics.
  37. // everyone else simply sets it to kinematic.
  38. // so that only the Transform component is synced.
  39. // host mode
  40. if (isServer && isClient)
  41. {
  42. // in host mode, we own it it if:
  43. // clientAuthority is disabled (hence server / we own it)
  44. // clientAuthority is enabled and we have authority over this object.
  45. bool owned = !clientAuthority || IsClientWithAuthority;
  46. // only set to kinematic if we don't own it
  47. // otherwise don't touch isKinematic.
  48. // the authority owner might use it either way.
  49. if (!owned) rb.isKinematic = true;
  50. }
  51. // client only
  52. else if (isClient)
  53. {
  54. // on the client, we own it only if clientAuthority is enabled,
  55. // and we have authority over this object.
  56. bool owned = IsClientWithAuthority;
  57. // only set to kinematic if we don't own it
  58. // otherwise don't touch isKinematic.
  59. // the authority owner might use it either way.
  60. if (!owned) rb.isKinematic = true;
  61. }
  62. // server only
  63. else if (isServer)
  64. {
  65. // on the server, we always own it if clientAuthority is disabled.
  66. bool owned = !clientAuthority;
  67. // only set to kinematic if we don't own it
  68. // otherwise don't touch isKinematic.
  69. // the authority owner might use it either way.
  70. if (!owned) rb.isKinematic = true;
  71. }
  72. }
  73. protected override void OnValidate()
  74. {
  75. base.OnValidate();
  76. // we can't overwrite .target to be a Rigidbody.
  77. // but we can ensure that .target has a Rigidbody, and use it.
  78. if (target.GetComponent<Rigidbody>() == null)
  79. {
  80. Debug.LogWarning($"{name}'s NetworkRigidbody.target {target.name} is missing a Rigidbody", this);
  81. }
  82. }
  83. }
  84. }