PhotonRigidbodyView.cs 3.9 KB

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