Unity 3D Implementation of frame synchronization technology

Source: Internet
Author: User
Tags local time

The author introduces: Jiang Xiewei, IT company technology partner, it senior lecturer, CSDN Community expert, guest editor, best-selling author, national patent inventor; published books: Teach You architecture 3D game engine, electronic industry press andUnity3d Actual combat core technical details of the electronic industry publishing house.

CSDN Video URL: http://edu.csdn.net/lecturer/144

Now competitive online games compared to the fire, there are many of this type of game competition, mentioned online game can not avoid a problem: synchronization technology, many people in a game scene to siege a monster or say multiplayer team fight and so on. Now in the mobile game due to bandwidth constraints, the general use of real-time synchronization is the state of synchronization, that is, the status of the role changes, will not send messages. As an example:

3D characters General Action State: Idle,walk,run,attack, player operation keyboard or touch screen button, will trigger these actions, a game scene will have multiple characters, each character has its own action state, in order to let the player can see what other players are doing, need to synchronize, The player default state is idle, the player has just appeared when the idle state, this time, the client will play home status, location, direction to the server, other players are the same, the server receives the information, will send this information to other players other than itself, This way we can see the status of the other players. If the player is transferred from the idle state to the walk state, this indicates that the player's action status has changed, which also need to send the information to the server, the server is sent to other players, so that other players can see the role began to walk. Next, if the player continues to walk, the client will not send a message to the server, because the state does not change, and other changes in the status of the message will be sent to the server, and then the server mass message, in this process, the other client will be interpolated by the way the distance between the two states realized, and so on ... This is what is called the state synchronization mode.

The following describes the frame synchronization mode, frame synchronization means that the game client accepts multiple clients from the network operation, if these operations are the same on each client, then the display of multiple clients is the same, which brings "synchronization" effect. Therefore, in this case, the operation of the client must be absolutely consistent, can not rely on such as local time, local random number, and so on "input", but all the network to the operation of data-based.

Generally speaking, most of the game client engine, will call an interface function, this function by the user to fill in the content, to modify and control the game in the various needs to display the content. For example, in Unity called Update (), such functions will usually be called before each frame rendering, when the user modifies the game's role in the location, size, the next frame is displayed. In the frame synchronization game, this update () function is still there, but most of the content, need to move to another similar function, we can call the Updatenet () function--by the network layer of receiving server sent by the "network Frame" packet, Each time such a packet is received, the updatenet () function is called once, so that the game is driven from the update () function of the local CPU to the Updatenet () function based on the network. Obviously, the network sent over the synchronization frame speed will be significantly slower than the local CPU, here on our game logic development put forward higher requirements-how to synchronize, but also to ensure smooth?

The implementation of the Updatenet function content, in fact, is to define a stack for the network sent over the message, through frame monitoring to use its data, because the update function is significantly faster than the updatenet, which requires us to define a time interval for the sending of messages, such as 50 milliseconds or 100 millimeters.

[CSharp]View PlainCopy
  1. Private float accumilatedtime = 0f;
  2. Private float framelength = 0.05f; //50 miliseconds
  3. Called Once per unity frame
  4. Public void Update () {
  5. //basically same logic as fixedupdate, but we can scale it by adjusting framelength
  6. Accumilatedtime = Accumilatedtime + time.deltatime;
  7. //in case The FPS is too slow, we could need to update the game multiple times a frame
  8. While (Accumilatedtime > Framelength) {
  9. Gameframeturn ();
  10. Accumilatedtime = Accumilatedtime-framelength;
  11. }
  12. }

[CSharp]View PlainCopy
  1. Private void Gameframeturn () {
  2. //first frame is used to process actions
  3. if (Gameframe = = 0) {
  4. if (Lockstepturn ()) {
  5. gameframe++;
  6. }
  7. } Else {
  8. //update Game
  9. SceneManager.Manager.TwoDPhysics.Update (Gameframespersecond);
  10. List<ihasgameframe> finished = new list<ihasgameframe> ();
  11. foreach (Ihasgameframe obj in SceneManager.Manager.GameFrameObjects) {
  12. Obj. Gameframeturn (Gameframespersecond);
  13. if (obj. Finished) {
  14. Finished. ADD (obj);
  15. }
  16. }
  17. foreach (Ihasgameframe obj in finished) {
  18. SceneManager.Manager.GameFrameObjects.Remove (obj);
  19. }
  20. gameframe++;
  21. if (gameframe = = Gameframesperlocksetpturn) {
  22. Gameframe = 0;
  23. }
  24. }
  25. }

Frame synchronization game, because the need to "every frame" to broadcast data, so the frequency of the broadcast is very high, which requires that each broadcast of data to be small enough. It is best to have each network frame below an MTU to effectively reduce the latency of the underlying network. For the same reason, in order to improve the real-time, we generally prefer to use UDP instead of the TCP protocol, so that the underlying processing is more efficient. However, this will also bring the possibility of packet loss, chaos. So we often use redundancy--for example, each frame packet--that actually contains the last 2 frames of data, that is, 3 frames of data each time, to fight packet loss. That is, three bags inside as long as there is no loss of a bag, it will not affect the game.

Frame synchronization implementation of the process has a very important point is that the logical layer and the presentation layer must be separated, the presentation layer first, the logic layer, etc. sent to the service side of the instructions to process. The playing frequency between frame and frame is controlled by the server uniformly, but because of the influence of network jitter, the frame frequency is not too stable, in order to avoid playing jitter, the frame number controller needs some smoothing.

The cause of network jitter: In the network game, the operating conditions and environment of each client are often different, some hardware better, some worse, the network situation of the parties is not consistent; Occasionally the player's network will be in the game process, temporary congestion, we call it "network jitter." May cause the client to receive a bunch of network frames in "past time", the client needs to take some time to deal with these stacked network frames, so the client must have the ability to handle these stacked network data.

The most important thing in a real-time sync game is fluency, but there are many factors that affect the game's fluency, limitation of network bandwidth, CPU operation and rendering efficiency. The action of the player-controlled role, including the current client-controlled role, or the behavior data should be obtained from the network frame, because if the player loves to control the role of too many inconsistencies, the whole game scene will be worse. Many of the monster AI in the game are set according to the player's character, so if the player's characters are synchronized, then most monsters will behave the same.

Frame synchronization Game Technology, there is no one can make the game smooth general practice, but need and game specific to do a lot of combination, in reducing the packet, optimize the game fast forward experience, control the speed of the package as far as possible tuning. At the same time also need and game product planning together, balance consistency, real-time, fairness strategy, can truly achieve the purpose of smooth game. Demo: Link: https://pan.baidu.com/s/1kVt77wz Password: TFSA

http://www.woaipu.com/shops/zuzhuan/61406
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117777
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117890
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117994
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=118376

Unity 3D Implementation of frame synchronization technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.