Download M2H Unity Networking Tutorial PDF

TitleM2H Unity Networking Tutorial
File Size735.8 KB
Total Pages20
Table of Contents
                            About this tutorial
About the author
How to use this tutorial?
Tutorial 1: Connect & Disconnect
Tutorial 2: Sending messages
	Our very first multiplayer scene..just one player though!
	Tutorial 2A: Server plays, client observes, no instantiating.
	Tutorial 2B: Server and client(s) play, with instantiating.
Tutorial 3: Authoritative servers
Further network subjects explained
Real life examples
	Example 1: Chatscript
	Example 2: Masterserver example
	Example 3: Lobby system
	Example 4: FPS game
Tips
                        
Document Text Contents
Page 11

networkviews. Therefore we now use our own owner variable to detect what network player owns
this object. The owner of the playerscript sends movement input to the server. The server executes
all playerscripts to process the movement input and actually move the players. We now have a fully
authoritative server!

To get back to the subject of lag: in the previous examples the players cube would move right away
when pressing a movement key, but in this authoritative example we need to send our request, wait
for the server to process it and then finally receive the new server position. While we do want the
server to have all authority, we don't want the clients waiting for the servers response too long. We
can quite easily fix this lag by also having the client calculate the movement right away, the server
will always overwrite its calculated movement anyway and is still authoritative. This is quite easy to
add. In “Tutorial_3_Playerscript.js” simply have the client execute “SendMovementInput(HInput,
Vinput);”where you are sending the movement RPC (uncomment line 56) . Then make sure that the
SendMovementInput RPC call actually affects the client by updating the (server) movement code in
the bottom of the Update() function; also run it on the local player too by adding “||
Network.player==owner){" in the IF statement (see line 64). These two edits will now make sure the
clients movement is applied right away, but the servers calculations will still be ultimate in the end.

After applying the client “prediction” the movement will still look a bit laggy, to improve this check
out line 100, here's a snippet to “merge” the clients current position and the servers position, with
the servers position having more weight. You can take this a step further by saving the real server
position in a variable and “lerp”(See Vector3.Lerp) to this position in the Update loop instead of
only Lerping once in the OnSerializeNetworkView function (which is executed far less than
Update).

“Technically” you can make this happen ;).

Do note that you really don't need to make (all) your multiplayer games (totally) authoritative. Take
our “Crashdrive 3D” game for example, it was made non-authoritative. A player could possibly
change it's cars position maliciously; but who would really care? It could possibly affects a players
highscore; but that's being checked for dubious entries already since you can 'hack' your highscore
even more easily. Long story short: Consider whats really vital to make authoritative. Also do not
forget that even an authoritative server itself can still cheat.

M2H unity networking tutorial 11

Page 19

RPC bug?
Don't let this bug kill your time: If you have a game where the authoritative server itself is also a
player you might want to use this code:

networkView.RPC("SendUserInput", RPCMode.Server, horizontalInput, verticalInput);
However, this does not work. Instead, use:

if(Network.isServer){
SendUserInput(horizontalInput, verticalInput);

}else{
networkView.RPC("SendUserInput", RPCMode.Server, horizontalInput,

verticalInput);
}

Run dedicated servers
A downside of the unity networking is that the current dedicated server isn't as dedicated as we'd
possibly want. However; it still works. Running a dedicated server on Mac OS X is possible by
giving the batch mode argument to the executable. A windows equivalent option has been added in
Unity 2.6.

See: http://unity3d.com/support/documentation/Manual/Command%20Line%20Arguments.html

When running a dedicated server, you should use “Application.targetFrameRate”to make sure unity
doesn't try to run your server at 1000+fps, hogging your resources.

Connection issues: How to connect over the internet
Connection wise a multiplayer LAN game is no different then an multiplayer internet game, except
for the fact that LAN speeds/connections are usually better. Once you are able to connect your
game over LAN you'll find out that getting it to work over the internet can be a bit cumbersome.
Therefore I've made a list of things you can check to diagnose the problem.

Connections over the internet do not work:

• Does the game work over LAN?
• Do both computers have a working internet connection?
• Ensure that both PC's have opened up their firewalls for your application/port. You can try

to temporarily disable the firewall(s) to ensure it's not causing problems.
• Try a direct connection. Start a server and have the other PC connect to your external IP

address (“internet IP address”).
• If this does not work; your network router is probably blocking unknown incoming

connections as security measure. There are two options:
1. Use NAT punch trough (See the masterserver example) and hope that your router

supports NAT punch trough.
2. You can open up the port you are using in your router and/or forwarding all the

connections to that port to your internal LAN IP address. This will always work, but not
all your players/users will know how to configure their router.

M2H unity networking tutorial 19

http://unity3d.com/support/documentation/Manual/Command%20Line%20Arguments.html

Similer Documents