查看: 637|回复: 0
打印 上一主题 下一主题

[其他] Unity3d 网络同步算法

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-9-30 01:44:35 |只看该作者 |倒序浏览
  1. using UnityEngine;

  2. using System.Collections;

  3. using System.Collections.Generic;

  4. using System;

  5. public class NetworkController : MonoBehaviour

  6. {

  7. // Use this for initialization

  8. void Start ()

  9. {

  10. this.rigidbody.useGravity = false;

  11. }

  12. // Update is called once per frame

  13. void Update ()

  14. {

  15. NetworkListen();

  16. }

  17. #region 网络监听

  18. private void NetworkListen()

  19. {

  20. this.rigidbody.useGravity = false;

  21. this.rigidbody.angularVelocity = Vector3.zero;

  22. if( SelfInfo.GamePlayerInfo.ContainsKey(this.name) && SelfInfo.GamePlayerInfo[this.name].que.Count > 0 )

  23. {

  24. long networktime = ( BaseInfo.NetWorkTime + BaseInfo.DelayBackTime + (long)((Time.time - BaseInfo.GameTime)*1000f) );

  25. int i;

  26. for( i = 0 ; i<SelfInfo.GamePlayerInfo[this.name].que.Count ; i++ )

  27. {

  28. if( networktime <= long.Parse( (string)SelfInfo.GamePlayerInfo[this.name].que.data[18] ) )

  29. {

  30. break;

  31. }

  32. }

  33. if( i == SelfInfo.GamePlayerInfo[this.name].que.Count )

  34. i--;

  35. MessageElement message_item = SelfInfo.GamePlayerInfo[this.name].que[ i ];

  36. MessageElement message_item2;

  37. if( i > 0 )

  38. {

  39. message_item2 = SelfInfo.GamePlayerInfo[this.name].que[ i-1 ];

  40. SelfInfo.GamePlayerInfo[this.name].que.RemoveRange(0,i-1);

  41. }

  42. else

  43. {

  44. message_item2 = SelfInfo.GamePlayerInfo[this.name].que[ i ];

  45. }

  46. Vector3 Nowvelocity = new Vector3(float.Parse((string)message_item.data[11]) , float.Parse((string)message_item.data[12]) , float.Parse((string)message_item.data[13]) );

  47. Vector3 Nowvelocity2 = new Vector3(float.Parse((string)message_item2.data[11]) , float.Parse((string)message_item2.data[12]) , float.Parse((string)message_item2.data[13]) );

  48. Vector3 NowVelocityA = new Vector3(float.Parse((string)message_item.data[14]) , float.Parse((string)message_item.data[15]) , float.Parse((string)message_item.data[16]) );

  49. Vector3 NowVelocityA2 = new Vector3(float.Parse((string)message_item2.data[14]) , float.Parse((string)message_item2.data[15]) , float.Parse((string)message_item2.data[16]) );

  50. Quaternion Nowrotation = new Quaternion(float.Parse((string)message_item.data[7]) , float.Parse((string)message_item.data[8]) , float.Parse((string)message_item.data[9]) , float.Parse((string)message_item.data[10]) );

  51. Quaternion Nowrotation2 = new Quaternion(float.Parse((string)message_item2.data[7]) , float.Parse((string)message_item2.data[8]) , float.Parse((string)message_item2.data[9]) , float.Parse((string)message_item2.data[10]) );

  52. Vector3 Nowposition = new Vector3( float.Parse((string)message_item.data[4]) , float.Parse((string)message_item.data[5]) , float.Parse((string)message_item.data[6]) );

  53. Vector3 Nowposition2 = new Vector3( float.Parse((string)message_item2.data[4]) , float.Parse((string)message_item2.data[5]) , float.Parse((string)message_item2.data[6]) );

  54. //~ Debug.Log(networktime - long.Parse((string)message_item.data[18]));

  55. long TimeLimitExceed = 500;

  56. //忽略大于固定延迟时间

  57. if( networktime - long.Parse((string)message_item.data[18]) > TimeLimitExceed )

  58. {

  59. //~ Debug.Log(networktime - long.Parse((string)message_item.data[18]));

  60. return;

  61. }

  62. //求新的位置点

  63. float tmptime = 0f;

  64. float delaytime = 0.1f;

  65. if( Nowvelocity.magnitude != 0 )

  66. tmptime += ( Nowposition - Nowposition2 ).magnitude / Nowvelocity2.magnitude;

  67. //求移动中间点

  68. float AddSpeedTime = (float)( networktime - long.Parse((string)message_item.data[18]) )/1000f;

  69. float DisTime = (float)( long.Parse( (string)message_item.data[18] ) - long.Parse( (string)message_item2.data[18] ) )/1000f;

  70. Vector3 VelocityA = NowVelocityA;

  71. Vector3 NewPosition = Nowposition + Nowvelocity*AddSpeedTime + VelocityA*AddSpeedTime*AddSpeedTime/2f + (Nowvelocity+AddSpeedTime*VelocityA)*delaytime ;

  72. Vector3 NewPosition1 = Nowposition + Nowvelocity*AddSpeedTime + VelocityA*AddSpeedTime*AddSpeedTime/2f;

  73. if( (NewPosition1 - this.transform.position).magnitude > 10f )

  74. {

  75. Debug.Log(“Distance is larger than 10 meters!”);

  76. this.rigidbody.velocity = (Nowvelocity+AddSpeedTime*VelocityA)*delaytime;

  77. this.rigidbody.angularVelocity = Vector3.zero;

  78. this.transform.rotation = Nowrotation;

  79. this.transform.position = NewPosition1;

  80. return;

  81. }

  82. //~ Debug.DrawLine( NewPosition , NewPosition + Vector3.up*20f );

  83. //~ this.transform.position = NewPosition;

  84. //~ return;

  85. //~ if( (NewPosition - this.rigidbody.position).magnitude > 10 )

  86. //~ this.GetComponentInChildren<Collider>().isTrigger = true;

  87. //~ else

  88. //~ this.GetComponentInChildren<Collider>().isTrigger = false;

  89. Vector3 NewVelocity;

  90. //~ if( Vector3.Angle( ( Nowposition - NewPosition ).normalized , ( NewPosition - this.rigidbody.position ).normalized ) > 110f )

  91. //~ {

  92. //~ NewVelocity = ( Nowposition2 - Nowposition ).normalized * Nowvelocity.magnitude;

  93. //~ dislen = ( (float)( networktime - long.Parse((string)message_item2.data[18]) ) + delaytime ) / ( (float)( long.Parse( (string)message_item.data[18] ) - long.Parse( (string)message_item2.data[18] ) ) );

  94. //~ NewPosition = Vector3.Slerp( Nowposition2 , Nowposition , dislen );

  95. //~ NewVelocity = ( NewPosition - this.rigidbody.position ).normalized * ( NewPosition - this.rigidbody.position ).magnitude / tmptime;

  96. //~ Debug.Log(“in back: ”+ Vector3.Angle( ( Nowposition - NewPosition ).normalized , ( NewPosition - this.rigidbody.position ).normalized ) );

  97. //~ }

  98. //~ else

  99. //~ {

  100. //把新速度付给本物体

  101. if( tmptime < 0.00001f )

  102. {

  103. NewVelocity = ( NewPosition - this.rigidbody.position ).normalized * Nowvelocity.magnitude;

  104. }

  105. else

  106. {

  107. NewVelocity = ( NewPosition - this.rigidbody.position ).normalized *( NewPosition - this.rigidbody.position ).magnitude / tmptime;

  108. }

  109. this.rigidbody.velocity = NewVelocity;

  110. //车辆方向改变旋转模拟

  111. float len = 1f;

  112. if( (NewPosition - this.rigidbody.position).magnitude > 0 )

  113. len = NewVelocity.magnitude*Time.deltaTime / (NewPosition - this.rigidbody.position).magnitude;

  114. this.transform.rotation = Quaternion.Slerp( this.rigidbody.rotation  , Nowrotation , len );

  115. }

  116. }

  117. #endregion

  118. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-13 19:39 , Processed in 0.058027 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部