Unity3d Client Real-time synchronization technology

Source: Internet
Author: User

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


Playing online games, multiplayer online, multi-team, many to many PK, and so on, these we often can see each other in the mobile, we usually call this for real-time synchronization, sometimes we will see each other suddenly have been pulled back feeling, this title delay operation, That is, the client and server side time is inconsistent or the network is not smooth caused, next I give you how to achieve real-time synchronization in the client.

First we want to know the player around the other players or monsters, how the NPC is painted out, where the implementation of the server, the server will simulate the client's scene, but also in the server's gameserver inside the same size as the client to generate a map, it has a nine screen concept, what is nine screen, Nine screen is the server has a player for the center generated nine lattice, each grid has a certain size, all the players in the nine screen objects will be the server brush out:


Service one of the nine screen as shown, each player has its own nine screen, this nine screen is as the player moves, in the nine screen outside will not be brushed out, the player is not visible, this is why sometimes in the client to set the visual distance is large or not see, this is the truth. The server is still the subject. So we have the following steps when we implement the client:

The first step is to brush the player's own

The second step is to brush the player's nine screen objects

The third step is to always swipe into the player nine screen or away from the player nine screen objects

The implementation code is as follows:


Players in the nine-screen object after the brush out, we are going to achieve real-time synchronization, real-time synchronization There are many ways, I give you the way is now more popular, is based on the player's state of synchronization, what is the state meaning? For example, the player from the state to walk state, the state changes to tell the server, and the server will change this state to tell the player in the nine screen object, the player started to go, if the player has been walking, the client and the server is different letter, and the client is in a certain time interval memory into the stack, At the same time can be sent to the server, but the server is not forwarded, he just in the server simulation. Once the state changes immediately out to the server, the server then mass nine screen messages, in order to make the player appear not abrupt, the client will adopt the difference, the difference is based on the time, the calculation of this time includes the client sent to the server, and the server return time, And the time it takes to walk on the client is calculated. The code is as follows:


The above is the state change when the message is sent, the state does not change the way it is handled as follows:



There is a problem here, do not know the attention of the people? That is, if you encounter a collision body, how to solve?

Here we also have to do special treatment, that is, if we encounter a block, we also think that its state has changed, need to send a message, the code is as follows:



Finally note that the client in order to follow the server time can be synchronized, the client needs to ping the server, depending on the length of the ping difference. The code is as follows:


All of the above client-side synchronization is complete.


Unity3d Client Real-time 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.