NetCodeTransform.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Unity.Netcode;
  2. using UnityEngine;
  3. public delegate Vector3 PositionFunc(Vector3 pos);
  4. public delegate Quaternion RotationFunc(Quaternion pos);
  5. public class NetCodeTransform : NetworkBehaviour
  6. {
  7. NetworkVariable<Vector3> syncPostion;
  8. NetworkVariable<Quaternion> syncRotation;
  9. NetworkVariable<Vector3> syncScale;
  10. public float deltaTime;
  11. private float endTime;
  12. public float interpolationTime = 0.1f;
  13. private PositionFunc posHandler;
  14. private RotationFunc rotHandler;
  15. void Awake()
  16. {
  17. syncPostion = new NetworkVariable<Vector3>();
  18. syncRotation = new NetworkVariable<Quaternion>();
  19. syncScale = new NetworkVariable<Vector3>();
  20. }
  21. void Update()
  22. {
  23. ServerSync();
  24. }
  25. public override void OnNetworkSpawn()
  26. {
  27. base.OnNetworkSpawn();
  28. //if(NetworkManager.Singleton.IsConnectedClient)
  29. //{
  30. // syncPostion.OnValueChanged += OnPosValueChange;
  31. // syncScale.OnValueChanged += OnScaleValueChange;
  32. // syncRotation.OnValueChanged += OnRotationValueChange;
  33. //}
  34. }
  35. public override void OnNetworkDespawn()
  36. {
  37. base.OnNetworkDespawn();
  38. //if(NetworkManager.Singleton.IsConnectedClient)
  39. //{
  40. // syncPostion.OnValueChanged -= OnPosValueChange;
  41. // syncScale.OnValueChanged -= OnScaleValueChange;
  42. // syncRotation.OnValueChanged -= OnRotationValueChange;
  43. //}
  44. }
  45. private void ServerSync()
  46. {
  47. if(IsServer)
  48. {
  49. if(CheckDeltaTime())
  50. {
  51. //服务端
  52. syncPostion.Value = transform.position;
  53. syncRotation.Value = transform.rotation;
  54. syncScale.Value = transform.localScale;
  55. }
  56. }
  57. else if(NetworkManager.Singleton.IsConnectedClient)
  58. {
  59. if(posHandler != null && rotHandler != null)
  60. {
  61. //客户端
  62. transform.position = Vector3.Lerp(transform.position, posHandler(syncPostion.Value), interpolationTime);
  63. transform.localScale = Vector3.Lerp(transform.localScale, syncScale.Value, interpolationTime);
  64. transform.rotation = Quaternion.Slerp(transform.rotation, rotHandler(syncRotation.Value), interpolationTime);
  65. }
  66. }
  67. }
  68. bool CheckDeltaTime()
  69. {
  70. if(Time.time >= endTime)
  71. {
  72. endTime = Time.time + deltaTime;
  73. return true;
  74. }
  75. return false;
  76. }
  77. private void OnPosValueChange(Vector3 oldPos, Vector3 newPos)
  78. {
  79. if(NetworkManager.Singleton.IsConnectedClient)
  80. {
  81. //客户端
  82. transform.position = Vector3.Lerp(transform.position, newPos, interpolationTime);
  83. //transform.position = Vector3.SmoothDamp(transform.position, newPos, ref curVelocity, interpolationTime);
  84. }
  85. }
  86. private Vector3 curVelocity;
  87. private void OnScaleValueChange(Vector3 oldScale, Vector3 newScale)
  88. {
  89. if(NetworkManager.Singleton.IsConnectedClient)
  90. {
  91. //客户端
  92. transform.localScale = Vector3.Lerp(transform.localScale, newScale, interpolationTime);
  93. //transform.localScale = Vector3.SmoothDamp(transform.localScale, newScale, ref curVelocity,interpolationTime);
  94. }
  95. }
  96. private void OnRotationValueChange(Quaternion oldRot, Quaternion newRot)
  97. {
  98. if(NetworkManager.Singleton.IsConnectedClient)
  99. {
  100. //客户端
  101. transform.rotation = Quaternion.Slerp(transform.rotation, newRot, interpolationTime);
  102. }
  103. }
  104. }