PhotonRigidbody2DView.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonRigidbody2DView.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Component to synchronize 2d rigidbodies via PUN.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. namespace Photon.Pun
  11. {
  12. using UnityEngine;
  13. [RequireComponent(typeof(Rigidbody2D))]
  14. [AddComponentMenu("Photon Networking/Photon Rigidbody 2D View")]
  15. public class PhotonRigidbody2DView : MonoBehaviourPun, IPunObservable
  16. {
  17. private float m_Distance;
  18. private float m_Angle;
  19. private Rigidbody2D m_Body;
  20. private Vector2 m_NetworkPosition;
  21. private float m_NetworkRotation;
  22. [HideInInspector]
  23. public bool m_SynchronizeVelocity = true;
  24. [HideInInspector]
  25. public bool m_SynchronizeAngularVelocity = false;
  26. [HideInInspector]
  27. public bool m_TeleportEnabled = false;
  28. [HideInInspector]
  29. public float m_TeleportIfDistanceGreaterThan = 3.0f;
  30. public void Awake()
  31. {
  32. this.m_Body = GetComponent<Rigidbody2D>();
  33. this.m_NetworkPosition = new Vector2();
  34. }
  35. public void FixedUpdate()
  36. {
  37. if (!this.photonView.IsMine)
  38. {
  39. this.m_Body.position = Vector2.MoveTowards(this.m_Body.position, this.m_NetworkPosition, this.m_Distance * (1.0f / PhotonNetwork.SerializationRate));
  40. this.m_Body.rotation = Mathf.MoveTowards(this.m_Body.rotation, this.m_NetworkRotation, this.m_Angle * (1.0f / PhotonNetwork.SerializationRate));
  41. }
  42. }
  43. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  44. {
  45. if (stream.IsWriting)
  46. {
  47. stream.SendNext(this.m_Body.position);
  48. stream.SendNext(this.m_Body.rotation);
  49. if (this.m_SynchronizeVelocity)
  50. {
  51. stream.SendNext(this.m_Body.velocity);
  52. }
  53. if (this.m_SynchronizeAngularVelocity)
  54. {
  55. stream.SendNext(this.m_Body.angularVelocity);
  56. }
  57. }
  58. else
  59. {
  60. this.m_NetworkPosition = (Vector2)stream.ReceiveNext();
  61. this.m_NetworkRotation = (float)stream.ReceiveNext();
  62. if (this.m_TeleportEnabled)
  63. {
  64. if (Vector3.Distance(this.m_Body.position, this.m_NetworkPosition) > this.m_TeleportIfDistanceGreaterThan)
  65. {
  66. this.m_Body.position = this.m_NetworkPosition;
  67. }
  68. }
  69. if (this.m_SynchronizeVelocity || this.m_SynchronizeAngularVelocity)
  70. {
  71. float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
  72. if (m_SynchronizeVelocity)
  73. {
  74. this.m_Body.velocity = (Vector2)stream.ReceiveNext();
  75. this.m_NetworkPosition += this.m_Body.velocity * lag;
  76. this.m_Distance = Vector2.Distance(this.m_Body.position, this.m_NetworkPosition);
  77. }
  78. if (this.m_SynchronizeAngularVelocity)
  79. {
  80. this.m_Body.angularVelocity = (float)stream.ReceiveNext();
  81. this.m_NetworkRotation += this.m_Body.angularVelocity * lag;
  82. this.m_Angle = Mathf.Abs(this.m_Body.rotation - this.m_NetworkRotation);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }