(For new projects, you should use the new networking system introduced in 5.1. This information is for legacy projects using the old networking system.)
You can enable State Synchronization for a given Network View by choosing either Reliable Delta Compressed or Unreliable from the State Synchronization drop-down. You must then choose what kind of data will be synchronized using the Observed property.
Unity can synchronize Transform, Animation, Rigidbody and MonoBehaviour components.
Transforms are serialized by storing position, rotation and scale. Parenting information is not transferred over the network.
Animation serializes each running animation state, that is the time, weight, speed and enabled properties.
Rigidbody serializes position, rotation, velocity and angular velocity.
Scripts (MonoBehaviours) call the function OnSerializeNetworkView().
Network Views currently support two reliability levels Reliable Delta Compressed and Unreliable.
Both have their advantages and disadvantages, and the specific details of the game will determine which is the best to use.
For additional information about minimizing bandwidth, please read the Minimizing Bandwidth page.
Reliable Delta Compressed mode will automatically compare the data that was last received by the client to the current state. If no data has changed since the last update then no data will be sent. However, the data will be compared on a per property basis. For example, if the Transform’s position has changed but its rotation has not then only the position will be sent across the network. Bandwidth is saved by transmitting only the changed data.
Unity will also ensure that every UDP packet arrives reliably by resending it until receipt is determined. This means that if a packet is dropped, any packets sent later will not be applied until the dropped packet is re-sent and received. Until then, all later packets will be kept waiting in a buffer.
In Unreliable mode, Unity will send packets without checking that they have been received. This means that it doesn’t know which information has been received and so it is not safe to send only the changed data - the whole state will be sent with each update.
The Network layer uses UDP, which is an unreliable, unordered protocol but it can used to send ordered packets reliably, just like TCP does. To do this, Unity internally uses ACKs and NACKs to control packet transmission, ensuring no packets are dropped. The downside to using reliable ordered packets is that if a packet is dropped or delayed, everything stops until that packet has arrived safely. This can cause transmission delays where there is significant network lag.
Unreliable transmission is useful when you know that data will change every frame anyway. For example, in a racing game, you can practically rely that the player’s car is always moving, so the effects of a missed packet will soon be fixed by the next one.
In general, you should use Unreliable sending where quick, frequent updates are more important than missed packets. Conversely, when data doesn’t change so frequently, you can use reliable delta compression to save bandwidth.
When the server has full authority over the world state, the clients only change the game state according to updates they receive from the server. One problem with this is that the delay introduced by waiting for the server to respond can affect gameplay. For example, when a player presses a key to move forward, they won’t actually move until the updated state is received from the server. This delay depends on the latency of the connection so the worse the connection the less snappy the control system becomes.
One possible solution to this is Client-side Prediction which means the client predicts the expected movement update from the server by using approximately the same internal model. So the player responds immediately to input but the server sees its position from the last update. When the state update finally arrives from the server, the client will compare what it predicted with what actually happened. This might differ because the server might know more about the environment around the player, the client just knows what it needs to know. Errors in prediction are corrected as they happen and if they are corrected continuously then the result will smoother and less noticeable.
It is possible to apply the basic principle of client-side prediction to the opponents of the player. Extrapolation is the process of storing the last few known values of position, velocity and direction for an opponent and use these to predict where it should be in the immediate future. When the next state update finally arrives with the correct position, the client state will be updated with the exact information, which may make the character jump suddenly if the prediction was bad. In FPS games the behavior of players can be very erratic so this kind of prediction has limited success. If the lag gets high enough the opponent will jump severely as the prediction errors accumulate.
Interpolation can be used when packets get dropped on the way to the client. This would normally cause the NPC’s movement to pause and then jump to the newest position when the new packet finally arrives. By delaying the world state by some set amount of time (like 100 ms) and then interpolating between the last known position and the new one, the movement between these two points, where packets were dropped, will be smooth.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.