PhotonTransformView.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonTransformView.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Component to synchronize Transforms via PUN PhotonView.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. namespace Photon.Pun
  11. {
  12. using UnityEngine;
  13. [AddComponentMenu("Photon Networking/Photon Transform View")]
  14. [HelpURL("https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state")]
  15. public class PhotonTransformView : MonoBehaviourPun, IPunObservable
  16. {
  17. private float m_Distance;
  18. private float m_Angle;
  19. private Vector3 m_Direction;
  20. private Vector3 m_NetworkPosition;
  21. private Vector3 m_StoredPosition;
  22. private Quaternion m_NetworkRotation;
  23. public bool m_SynchronizePosition = true;
  24. public bool m_SynchronizeRotation = true;
  25. public bool m_SynchronizeScale = false;
  26. [Tooltip("Indicates if localPosition and localRotation should be used. Scale ignores this setting, and always uses localScale to avoid issues with lossyScale.")]
  27. public bool m_UseLocal;
  28. bool m_firstTake = false;
  29. public void Awake()
  30. {
  31. m_StoredPosition = transform.localPosition;
  32. m_NetworkPosition = Vector3.zero;
  33. m_NetworkRotation = Quaternion.identity;
  34. }
  35. private void Reset()
  36. {
  37. // Only default to true with new instances. useLocal will remain false for old projects that are updating PUN.
  38. m_UseLocal = true;
  39. }
  40. void OnEnable()
  41. {
  42. m_firstTake = true;
  43. }
  44. public void Update()
  45. {
  46. var tr = transform;
  47. if (!this.photonView.IsMine)
  48. {
  49. if (m_UseLocal)
  50. {
  51. tr.localPosition = Vector3.MoveTowards(tr.localPosition, this.m_NetworkPosition, this.m_Distance * Time.deltaTime * PhotonNetwork.SerializationRate);
  52. tr.localRotation = Quaternion.RotateTowards(tr.localRotation, this.m_NetworkRotation, this.m_Angle * Time.deltaTime * PhotonNetwork.SerializationRate);
  53. }
  54. else
  55. {
  56. tr.position = Vector3.MoveTowards(tr.position, this.m_NetworkPosition, this.m_Distance * Time.deltaTime * PhotonNetwork.SerializationRate);
  57. tr.rotation = Quaternion.RotateTowards(tr.rotation, this.m_NetworkRotation, this.m_Angle * Time.deltaTime * PhotonNetwork.SerializationRate);
  58. }
  59. }
  60. }
  61. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  62. {
  63. var tr = transform;
  64. // Write
  65. if (stream.IsWriting)
  66. {
  67. if (this.m_SynchronizePosition)
  68. {
  69. if (m_UseLocal)
  70. {
  71. this.m_Direction = tr.localPosition - this.m_StoredPosition;
  72. this.m_StoredPosition = tr.localPosition;
  73. stream.SendNext(tr.localPosition);
  74. stream.SendNext(this.m_Direction);
  75. }
  76. else
  77. {
  78. this.m_Direction = tr.position - this.m_StoredPosition;
  79. this.m_StoredPosition = tr.position;
  80. stream.SendNext(tr.position);
  81. stream.SendNext(this.m_Direction);
  82. }
  83. }
  84. if (this.m_SynchronizeRotation)
  85. {
  86. if (m_UseLocal)
  87. {
  88. stream.SendNext(tr.localRotation);
  89. }
  90. else
  91. {
  92. stream.SendNext(tr.rotation);
  93. }
  94. }
  95. if (this.m_SynchronizeScale)
  96. {
  97. stream.SendNext(tr.localScale);
  98. }
  99. }
  100. // Read
  101. else
  102. {
  103. if (this.m_SynchronizePosition)
  104. {
  105. this.m_NetworkPosition = (Vector3)stream.ReceiveNext();
  106. this.m_Direction = (Vector3)stream.ReceiveNext();
  107. if (m_firstTake)
  108. {
  109. if (m_UseLocal)
  110. tr.localPosition = this.m_NetworkPosition;
  111. else
  112. tr.position = this.m_NetworkPosition;
  113. this.m_Distance = 0f;
  114. }
  115. else
  116. {
  117. float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
  118. this.m_NetworkPosition += this.m_Direction * lag;
  119. if (m_UseLocal)
  120. {
  121. this.m_Distance = Vector3.Distance(tr.localPosition, this.m_NetworkPosition);
  122. }
  123. else
  124. {
  125. this.m_Distance = Vector3.Distance(tr.position, this.m_NetworkPosition);
  126. }
  127. }
  128. }
  129. if (this.m_SynchronizeRotation)
  130. {
  131. this.m_NetworkRotation = (Quaternion)stream.ReceiveNext();
  132. if (m_firstTake)
  133. {
  134. this.m_Angle = 0f;
  135. if (m_UseLocal)
  136. {
  137. tr.localRotation = this.m_NetworkRotation;
  138. }
  139. else
  140. {
  141. tr.rotation = this.m_NetworkRotation;
  142. }
  143. }
  144. else
  145. {
  146. if (m_UseLocal)
  147. {
  148. this.m_Angle = Quaternion.Angle(tr.localRotation, this.m_NetworkRotation);
  149. }
  150. else
  151. {
  152. this.m_Angle = Quaternion.Angle(tr.rotation, this.m_NetworkRotation);
  153. }
  154. }
  155. }
  156. if (this.m_SynchronizeScale)
  157. {
  158. tr.localScale = (Vector3)stream.ReceiveNext();
  159. }
  160. if (m_firstTake)
  161. {
  162. m_firstTake = false;
  163. }
  164. }
  165. }
  166. }
  167. }