NetworkRigidbodyUnreliable2D.cs 3.8 KB

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