Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ultimate Unity Networking project - Add multiplayer to your game today!

Discussion in 'Multiplayer' started by MikeHergaarden, Jan 25, 2011.

  1. sijj

    sijj

    Joined:
    May 3, 2011
    Posts:
    12
    Hi, Thanks for the tutorial but i am having problem with samples. when i click on play it says "All compiler errors should be fixed". I am getting unknown identifier "FPSChat" and "GameSettings".

    would you please help me to fix them i am using unity 3.3.0f4 version.

    thanks
     
  2. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    You'll need to solve all compile errors first. Try reimporting the package, your import seems to be missing at least two files. Import it fresh from the asset store.
     
  3. sijj

    sijj

    Joined:
    May 3, 2011
    Posts:
    12
    Does your tutorial covers the state synchronization issue aswell? i am working on a racing game of bike for iPhone and i am using translation instead of physics . Following problems i am facing in my app.

    1) by playing on two phones i am facing state synchronization issue like on phones i can see there is a lot of distance between bikes. on one phone i have less distance between bikes and on the other phone its more then that.

    2) by using Unity,s Master Server I am able to have communication up and running. However, I believe the provided code only works for full cone NATs or restricted NATs but if there are port restricted NATs on each end, it does not work. Am I right? if it is then does your sample provide the solution for this?


    does you samples provides the solution for these issue? if not then do you have any idea or hint how can i overcome these issue?

    thanks
     
  4. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    1: How are you synchronizing?
    2: Some combinations will indeed not work. We can not apply any workarounds as its an limitation of Unity's raknet implementation. One option is to use a proxyserver for all the traffic, you would need to host this proxyserver. For better/garuanteed connectivity you would need to look at a hosted solution (photon,smartfox, etc).
    Edit (Little angel): I mean a different networking solution, by using dedicated server(s). Connectivity can be guaranteed here because you can control this server where you cannot predict your players network connections (their NAT type when acting as a server).
     
    Last edited: Sep 13, 2011
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Leepo: As you can host your own master server, are you suggesting that for better/guaranteed connectivity one should avoid Unity Networking?

    I'm unclear what you are advising here...
     
  6. sijj

    sijj

    Joined:
    May 3, 2011
    Posts:
    12
    I am using the NetworkRigidbody.cs Script and sending and receiving the positions on both ends, also using Interpolation and extrapolation is same script but still i am facing that issue of state synchronization.

    Here is my code for NetworkRigidbody.

    Code (csharp):
    1. public class NetworkRigidbody : MonoBehaviour {
    2.    
    3.     public double m_InterpolationBackTime = 0.1;
    4.     public double m_ExtrapolationLimit = 0.5;
    5.    
    6.     private StreamWriter sendPos;
    7.     //private String  fileName = "Send Positions.txt";
    8.    
    9.     internal struct  State
    10.     {
    11.         internal double timestamp;
    12.         internal Vector3 pos;
    13.     }
    14.    
    15.     // We store twenty states with "playback" information
    16.     State[] m_BufferedState = new State[20];
    17.     // Keep track of what slots are used
    18.     int m_TimestampCount;
    19.    
    20.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
    21.         // Send data to server
    22.         if (stream.isWriting)
    23.         {
    24.             Vector3 pos = rigidbody.position;
    25.             stream.Serialize(ref pos);            
    26.         }
    27.         // Read data from remote client
    28.         else
    29.         {
    30.             Vector3 pos = Vector3.zero;
    31.             stream.Serialize(ref pos);
    32.            
    33.             // Shift the buffer sideways, deleting state 20
    34.             for (int i=m_BufferedState.Length-1;i>=1;i--)
    35.             {
    36.                 m_BufferedState[i] = m_BufferedState[i-1];
    37.             }
    38.            
    39.             // Record current state in slot 0
    40.             State state;
    41.             state.timestamp = info.timestamp;
    42.             state.pos = pos;
    43.             m_BufferedState[0] = state;
    44.            
    45.             // Update used slot count, however never exceed the buffer size
    46.             // Slots aren't actually freed so this just makes sure the buffer is
    47.             // filled up and that uninitalized slots aren't used.
    48.             m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
    49.  
    50.             // Check if states are in order, if it is inconsistent you could reshuffel or
    51.             // drop the out-of-order state. Nothing is done here
    52.             for (int i=0;i<m_TimestampCount-1;i++)
    53.             {
    54.                 if (m_BufferedState[i].timestamp < m_BufferedState[i+1].timestamp)
    55.                     Debug.Log("State inconsistent");
    56.             }    
    57.         }
    58.     }
    59.    
    60.     // We have a window of interpolationBackTime where we basically play
    61.     // By having interpolationBackTime the average ping, you will usually use interpolation.
    62.     // And only if no more data arrives we will use extra polation
    63.     void Update () {
    64.         // This is the target playback time of the rigid body
    65.         double interpolationTime = Network.time - m_InterpolationBackTime;
    66.        
    67.         // Use interpolation if the target playback time is present in the buffer
    68.         if (m_BufferedState[0].timestamp > interpolationTime)
    69.         {
    70.             // Go through buffer and find correct state to play back
    71.             for (int i=0;i<m_TimestampCount;i++)
    72.             {
    73.                 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount-1)
    74.                 {
    75.                     // The state one slot newer (<100ms) than the best playback state
    76.                     State rhs = m_BufferedState[Mathf.Max(i-1, 0)];
    77.                     // The best playback state (closest to 100 ms old (default time))
    78.                     State lhs = m_BufferedState[i];
    79.                    
    80.                     // Use the time between the two slots to determine if interpolation is necessary
    81.                     double length = rhs.timestamp - lhs.timestamp;
    82.                     float t = 0.0F;
    83.                     // As the time difference gets closer to 100 ms t gets closer to 1 in
    84.                     // which case rhs is only used
    85.                     // Example:
    86.                     // Time is 10.000, so sampleTime is 9.900
    87.                     // lhs.time is 9.910 rhs.time is 9.980 length is 0.070
    88.                     // t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs
    89.                     if (length > 0.0001)
    90.                         t = (float)((interpolationTime - lhs.timestamp) / length);
    91.                    
    92.                     // if t=0 => lhs is used directly0
    93.                     transform.position = Vector3.Lerp(lhs.pos, rhs.pos, t);
    94.                    
    95.                     return;
    96.                 }
    97.             }
    98.         }
    99.         // Use extrapolation
    100.         else
    101.         {
    102.             State latest = m_BufferedState[0];
    103.             float extrapolationLength = (float)(interpolationTime - latest.timestamp);
    104.            
    105.             //calculate average
    106.             float average = 0.0F;
    107.             float[] difference = new float[19];
    108.            
    109.             for(int i=0; i<m_BufferedState.Length-1;i++)
    110.             {
    111.                 difference[i] = m_BufferedState[i].pos.z - m_BufferedState[i+1].pos.z;
    112.             }
    113.            
    114.             for(int i=0;i<difference.Length;i++)
    115.                 average += difference[i];
    116.             average /= difference.Length;
    117.            
    118.            
    119.             // Don't extrapolation for more than 500 ms, you would need to do that carefully
    120.             if (extrapolationLength < m_ExtrapolationLimit)
    121.             {                
    122.                 transform.position = Vector3.Lerp(transform.position, transform.position +  new Vector3(0, 0, average), 0.5F);
    123.             }            
    124.         }
    125.     }
    126. }
    thanks
     
    Last edited: Sep 13, 2011
  7. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    I've had a server running "Example 2" of the library for a week now and the server/host is not behind any NAT at all. My client doesn't ever find a server so either there is still a problem with Unity's Masterserver (for over a week) or maybe the Masterserver code isn't working in Unity 3.4 anymore - I've been unable to confirm why it's not working yet.
     
  8. sijj

    sijj

    Joined:
    May 3, 2011
    Posts:
    12
    I am running the default Master Server and it running fine. i am having no issues with that except the problem i have mentioned above. debug the code or check your firewall,s and router,s settings.
     
  9. sijj

    sijj

    Joined:
    May 3, 2011
    Posts:
    12
    I am looking at your "Example 5" in which you have implemented Auto Matchmaking but i cannot find where you are sending and receiving the positions of both the players. can you point me out where you are doing this specific work?

    In documentation you have not mentioned how to implement these examples in our application.

    thanks
     
  10. vivalarosa

    vivalarosa

    Joined:
    Sep 15, 2011
    Posts:
    26
    Hey,

    I bought this networking guide yesterday, and know little about networking.

    I uploaded the project to my website to test whether the lobby's and multiplayer from different routers was working with no luck.

    I uploaded it to my website: http://deadpixelinc.com/scrapped

    I have 3 main problems:

    1) When entering the lobby example (or fps example), and starting a lobby, that lobby cannot be picked up by another browser on this computer, another instance of the website on a different computer in the same house or by a computer at a friends house.

    However on the same computer if i use the default values and direct connect, it will join the lobby.

    2) When a friend uses my IP to try and connect to one of the tutorial files it does not work. I supplied him with my I.p, 124.185.***.***, he cannot connect using the default port, or at all. This is the same if i try to connect to him. Once again this works fine using a different web instance on the same computer using the default values (127.0.0.1).

    3) In the auto matchmaking example it does not connect the 2 players, the first to start the server will get the chat window popup, the second will get the error "Failed to initialize network interface. Is the listen port already in use?", i assume this version is also trying to start a server, but shouldn't it find already that a server is created and attempt to connect?

    I read the little information given about each example but nothing details any steps to take to get this to work.

    Someone please help, i feel that i've wasted $85 on a product that doesn't work and is incomplete.

    Thanks in advance for reading and any help, It's really appreciated.

    James
     
  11. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    The networkview is observing the transform, this takes care of synch. automaticlly. But its not very efficient as even scale etc is send(which is not required).

    1) possibly a masterserver problem. This seems to be happening quite a lot lately? maybe try hosting your own masterserver.

    2) Make sure you open your routers port OR he should connect via the masterserver, only then NAT paunchtrough is used, if available.

    3) this problem occurs if you try to start two servers on the same port on the same computer. Your second server should use a different port.
    However, in the matchmaking case this means that the 2nd client doesnt see(or cannot connect to) your first client and start a server too.
    This error will never appear in real situations as theres never two application on the same machine. The connection error is the real error to solve here...the masterserver -should- automatically connect you :/.

    Please note that these errors all seem to be originating from the masterserver and Unitys networking implementation.
     
  12. vivalarosa

    vivalarosa

    Joined:
    Sep 15, 2011
    Posts:
    26
    Thanks for the reply Leepo,

    Just to get this straight, my friend (on a different computer), should only have to put my IP address into the IP section and click connect as client, to connect to me?

    We both play a lot of multiplayer games and have had no problems previous, my average user (casual gamer) shouldn't have to open ports just to play the games. There must be a way to fix the setup to allow a direct connection. Can we use a different port? Edit the code in any way?

    Otherwise I just paid $85 for a LAN networking set up?

    You can test all these errors for yourself at http://deadpixelinc.com/scrapped ( a fresh install of all the files ), if you have any different results please tell me.

    Thanks Leepo,

    James.
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Just for what it's worth with the folks that are having connectivity problems: I have been testing to Unity's default master server with my Unity Networking setup (which as very little to do with these tutorials, but shares the same concepts - see networking docs) and I'm connecting fine. The current test is unfortunately within a project that is not available to the public. If I can get a public test going, I'll let you know...

    But: Bottom line is, the Unity Master Server is working fine for me.
     
  14. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    No, per default your router will simply block connections on all ports. Only in lan it can work right away, assuming you allow your program to connect via your own local firewall.

    Very true, this is what the masterserver is for. This will help in setting up a connection by using NAT punchtrough, just like other games. See example 2. However even NAT punchtrough does not always work, in the worst case you will have to open up a port (I still have to do so for quite a few AAA games)
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    One thing is sure, I'm unclear how the connection tester actually works and what Unity is doing with it. (TBC: this is a Unity thing not an M2H thing. The connection tester in this code used the code pretty much straight from the docs: http://unity3d.com/support/documentation/ScriptReference/Network.TestConnection.html) I can run this a few times in succession, usually on a webplayer, and get different results each time. All related, and all successful, but sometimes different - and I'm not sure why. Can you expose the connection tester status and let us know what you are receiving as a response to connection testing? IIRC, this should be found in an M2H toolbox script... "MultiplayerTools"? "MultiplayerMethods"? where the connection testing takes place, and throw the details onto the screen into a GUILayout.Label? It would be good to know what your instance is saying about the connection.

    FWIW: I can connect between two webpages with my project, but I can't connect between two of your instances of the tutorial.
     
  16. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    vivalarosa: FWIW - I dl'd the most recent version of the Ultimate Networking Package and created a standalone player, a web player, and ran an instance in the editor.

    I often was getting a timeout error when trying to test the connection, but even when I did successfully test the connection and did successfully register the host:

    Code (csharp):
    1. 13.33925 OnMasterEevent: RegistrationSucceeded
    2. UnityEngine.Debug:Log(Object)
    3. MultiplayerFunctions:OnMasterServerEvent(MasterServerEvent) (at Assets/M2HNetworking/NetworkingPlugins/MultiplayerFunctions.js:276)
    ... and I couldn't see the hosted game from another instance of the project. The host list came up empty. Not sure why.

    This shouldn't be a master server issue on Unity's part, as I'm currently testing a networked project using Unity's master server on iOS, Standalone, Web and Editor - and I get a good connection to the master server.
     
  17. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    This is the same behavior I still have with Unity 3.4 (only tested it in 3.4) and I bought the package about 1-2 weeks ago. I also get the "Registration Succeeded", and not only that I capture traffic on the client when refreshing the list and I get a response from the master server and in that response traffic I see the IP address of the server... So the server is being registered and the client fins out about it - but it's not presented in the list for whatever reason.
     
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Tho' I imagine Leepo will get to this when he can...

    But could you dig through the code and look to see if there is a mis-match somewhere? Could it be that the game name, or port, or something is not the same between what's being sent and what's being queried? Could you be registering "MyGame" and asking for "OtherGame"? Also (and this WAS in the code from this package) the host needs to regularly re-register the game, as the master server has a short timeout on registered games... some people recommend re-registering every 60 seconds (but this should be in the code...) ... ... dunno. I have to pick my boy up from school and then go to after school activities, or I'd look now that I've d/l'd it again.
     
  19. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    It looks like everyone is having trouble with the masterserver in the project lately, I'll try to look at this today. (The masterserver does seem to work for other projects indeed)
     
  20. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Yes, I verified that the same game name is used (I thought that might have been the issue as well) - thanks for having a look at it later Leepo. I'll spend time with client side prediction meanwhile =)
     
  21. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    I got it. The javascript MultiplayerFunctions.js class has a bug where it never calls the delegate that is used when a new hostlist is ready.
    I have submitted an update to the assetstore. I'm not sure how soon it's live.

    You can fix this manually with the following patch:
    //+- line 310
    //+- line 259:
     
  22. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Thanks Leepo!

    I can confirm that after the modification it worked instantly for me (I'm behind Port Restricted NAT.)
     
  23. strtok

    strtok

    Joined:
    Nov 6, 2010
    Posts:
    7
    I've gone through the tutorial now, and I'm a bit confused about some conflicting ideas from Tutorial 3 and Tutorial 4.

    Tutorial 3 has the server spawning the player objects, and then sending an RPC message "SetPlayer" to let the player know they have control over the object. This means that the server is the owner of the network view and correctly serializes out changes to that object.

    Tutorial 4 introduces manually creating network view IDs, but it also changes the spawn model so that players spawn themselves.

    The reason for this change is not quite clear? Is this the recommended model? If so, I would think the GameManager script also needs to set the NetworkView's owner to the server so that the server is responsible for serializing.

    Thanks!
    --strtok
     
  24. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    I have just reached the same conclusion and I agree. I've got my prototype game running in Authoritative server (based on Tutorial 3) - and I'm about to take the next step making a game manager. I feel that tutorial 4 should have been based on authoritative servers as a natural progression from Tutorial 3. I will change the GameManager so that the server spawns the player again (but I'll be interested to hear the "official" reply to your question =)
     
  25. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Yeah its completely different, never really thought about the changes from 3 -> 4 as 4 was later. I would recommend manual allocation of view IDS over using Network.Instantiate. The main reason used to be that Network.Instantiate had quite a few bugs, but that seems to be solved now. Still, manually controlling your allocated viewIDs gives you some more control. E.g. if you die in a FPS game, you can transfer the same viewID to a newly spawned playerobject and keep using the same viewID for the same player.
     
  26. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    I am having troubles getting the master server examples to work over 3G on mobile devices. Connection works great between pc <-> pc and pc <-> mobile when the mobile device is using wifi, but as soon as I switch to 3G the mobile device stops being able to host or connect properly.
     
  27. strtok

    strtok

    Joined:
    Nov 6, 2010
    Posts:
    7
    I went through and adapted this example so that the server sends out AddPlayer and Spawn to RPC.OthersBuffered (i.e. the server is responsible for spawning). This means the server also created the network view ID's for the players, which gave the server proper ownership over the network views (so that the server is the writer on serialization).

    This created another problem. Now when a player leaves, there's no easy way to remove the buffered RPC related to their 'AddPlayer' and 'Spawn, because it's not owned by the NetworkPlayer being destroyed, but by the server.

    One easy solution to this is to not used buffered RPC for the AddPlayer and Spawn messages, but to send RPC to each player manually on join.

    Thoughts?
     
  28. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Seems like we are going side by side on this - exactly the same issue for me. I am using the PlayerInfo class to store more information about the players and I plan to add the ownership information into the class so the server knows what objects to destroy when the player disconnects.

    I have also opted to go back to Network.Instantiate for the time being. Since I use the NetworkRigidbody.cs observer it's too easy to manage that I'm not prepared to let Network.Instantiate go unless there is a major reason to move to manual allocation. I still use a GameManager and one of the reasons was to force you to write a GameManager. As Leepo said above, it apparently used to be buggy with Network.Instantiate but if that has been resolved I'm OK with it. The only thing I've noticed is that a respawn generates a new NetworkID as I'm not reusing them with a manual approach. But I'm not sure how many IDs are available... is it an Int?
     
  29. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    I bought the Ultimate Unity Networking package in the asset store about 2-3 weeks ago and reading the tutorial gave me a great insight how to approach and implement multiplayer in Unity. I'll post a proper review in the Asset Store later on, but I just thought I'd share a little preview of what I have achieved based on this network package during the course of two weeks (not just the networking aspect but from "New Project..."):

    YouTube version (looks choppy due to YT reducing video to 30 frames per second)
    60 frames per second MP4 video - http://www.astrofighter.net/video/astrofighter-net-first-preview.mp4

    The video shows 3 players connected but latency is low at 30ms and the video is from the server side. I'll have to optimize the networking code for low bandwidth, but it should perform quite well with high latency I hope (something I will definitely test.)

    I'm very impressed with how networking works in Unity, and making games in Unity at all for that matter. I have a huge amount to learn as I've only scratched on the surface of Unity. I wouldn't have dreamed of achieving such a result in such a short time using my past tools for developing games.
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Very impressed imphy. First time I've heard your music as well - tho' I've seen it 'round.

    For speed of development and getting off the ground fast, Unity is great! Like all things, polish will take a long time, but it's amazing how quick you can get a game that plays...

    I assume that you are running on the Unity Master Server for this test.
     
  31. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Thanks. Yes, I remember from past experience when I thought I was about 90% done with a game it was more like...45% if that =)

    I'm just using IP connect during the early development stage - but I'll use the Unity Master Server (or a dedicated master server) for the final release.

    I see in your footer now that you're the author(?) of EZGui. Nice, I have to pick a GUI tool for my menu interfaces and I like what I see in the preview video you've got.
     
  32. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    No, not the Author of EZGui - that's Brady Wright. I was one of the first users, so I had some tools quick out of the gate, but now I'm a little stale with all of the developments. It was a full time job keeping up with all of the new features, and now I'm onto networking and other domains... so MUCH to learn... I have a message managing system and a number of editor tools for EZGui. When 3.5 comes out, I'll port the system to Unity's GUI.
     
  33. strtok

    strtok

    Joined:
    Nov 6, 2010
    Posts:
    7
    Do you have an authoritative server? If so, are you calling Network.Instantiate on the client and then setting the owner of the NetworkView to the server so that it is the writer on serialization?
     
  34. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    I have authoritative server yes, but I am calling Network.Instantiate on the server to spawn players which I think makes it authoritative for the serialization =) I do it pretty much as described in tutorial 3 of the package when it comes to spawning, but with the addition of introducing a "GameManager"

    This leads me to another question. Is the best practice to have one script (e.g. GameManager.cs) to handle all communication? Currently I have spawning functions etc. in the GameManager script, but I call RPC for ships and weapon changes to other scripts and it seems to be a bit confusing... Should I always call RPC for GameManager which in turn calls functions locally further down in the hierarchy in a non-RPC fashion?
     
  35. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I love Network.Instantiate, and have had few problems with it.

    About the ID's... why do you need to destroy/respawn? Why not deactivate/hide renderer, reposition and reactivate? Then you don't need to worry about it.
     
  36. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I've never bothered at all with authoritative setups... but if I did I would imagine myself Network.Instantiating 2 objects for every connecting player... the server would spawn the actual player prefab and the player would spawn a "PlayerController" that just sent input messages to the server to move his prefab?
     
  37. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Why didn't I think of that? That's what I'll do of course :) Thanks!

    The two object spawn is an interesting idea as well - I'll add that to my list of considerations now during tidying up the code and structure. I definitely want authoritative because it would be tempting to cheat in a game like the one I'm making. Player stats will be recorded and published so it would be an incentive to cheat and without authoritative server one could just apply infinite armor or something like that... I suspect there will be hacked servers anyway so there will have to be validation of uploaded data and I just want to make good enough defense mechanisms and I'm aware it's impossible to make anything fool proof.
     
  38. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    also.... whats going to keep hosts from cheating? ;)
     
  39. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    I know - that's a problem. Only solution would be to have dedicated hosts but I couldn't afford hosting a large amount of servers =) At least clan matches could be hosted by a third party server to reduce foul play.
     
  40. strtok

    strtok

    Joined:
    Nov 6, 2010
    Posts:
    7
    How do the buffered RPC messages for the Network.Instantiate get destroyed if they're owned by the server? Is unity smart enough to know that if you're destroying the object associated with a Network.Instantiate then it should destroy the buffered RPC associated with its creation too?

    That alone might be one reason to use Network.Instantiate over trying to manually handle everything.
     
  41. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    You could just have all clients verify all the "important" commands like loosing health, max speed, et , to detect cheating. You don't even need to verify every command, but something like every X, or verify them all in a batch.
     
  42. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    You could always put all server's Network.Instantiates in a different group for each player, and use RemoveRPCs() and specify the group. You'd just need to keep a lookup table somewhere to remember what group is associated with what player.
     
  43. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Is there a way I can check if a NetworkViewID exists?

    When two bullets are fired at a non-player-object (in my case a rigidbody asteroid) the first bullet may destroy the object where the server executes Network.RemoveRPCs(networkViewID) and Network.Destroy(networkViewID).

    The second bullet that hits a split second later generates two error messages:
    Code (csharp):
    1. View ID SceneID: 8 Level Prefix: 0 not found during lookup. Strange behaviour may occur
    2. UnityEngine.NetworkView:Find(NetworkViewID)
    and
    Code (csharp):
    1. NullReferenceException
    2. script_GameManager.ApplyDamageToObject (Single damage, NetworkViewID target_NetworkViewID, NetworkPlayer inflictor) (at Assets/Scripts/script_GameManager.cs:111)
    3. script_Bullet.Update () (at Assets/Scripts/script_Bullet.cs:102)
    The first error I can't seem to do much about - I believe Unity may have cached network traffic to the object and it's natural for this error to occur? Can anyone confirm this?

    The second error I could eliminate by checking if the NetworkViewID exists - but I haven't found a way to do this. I tried
    Code (csharp):
    1. if (networkViewID != null) // doesn't work
    2. if (networkViewID == NetworkViewID.unassigned) // doesn't work
    3.  
    Any suggestions on this?

    UPDATE:
    I solved the second error, I had to use if (target_NetworkView != null) before I tried to get the transform of networkViewID.

    The first error remains though and I think this is standard Unity behavior if two bullets register a hit in the same Update() and the first bullet has enough power to destroy the object.
     
    Last edited: Sep 23, 2011
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    imphy: Huh...

    http://unity3d.com/support/documentation/ScriptReference/GameObject-networkView.html

    implies that your solution should work when it says:
    The NetworkView attached to this GameObject (Read Only). (null if there is none attached)

    I think that the problem is, looking at this page, by saying:
    Code (csharp):
    1. if (networkViewID != null)
    ... you are checking if the network view exists on this object.

    Looking at the sample code:
    Code (csharp):
    1. public class example : MonoBehaviour {
    2.     public GameObject other;
    3.     void Awake() {
    4.         other.networkView.RPC("MyFunction", RPCMode.All, "someValue");
    5.     }
    6. }
    ... you need:

    Code (csharp):
    1. other.networkView...
    Now you may have tried this in your actual code, but from reading what you've posted this seems to be a possible issue.
     
  45. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    I'm still not able to get networking through a master server to work on mobile devices using 3G. Mobile devices work just fine on wifi, but once I switch to 3G they continue to show up in the server list but cannot be connected to.

    Any insight would be appreciated.
     
  46. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I bought this tutorial a few months ago in hope to getting around to it and I am getting this error-

    Connect.js(40,33): BCW0012: WARNING: 'UnityEngine.Network.InitializeServer(int, int)' is obsolete. Use the IntializeServer(connections, listenPort, useNat) function instead

    Not sure what needs to be done to fix this????
     
  47. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    All_American: You can probably download an updated version of the tutorial files if you have purchased it before. Go to the Unity Asset Store and click on the "downloads" button at the top (next to the house icon)
     
  48. diegomazala

    diegomazala

    Joined:
    Mar 15, 2010
    Posts:
    21
    Hi
    I bought your package and I have to say congratulations. Your job was great.
    However, I am having a problem and I would like to know if you can help me.
    My game is going to run only in a LAN with no internet connection.
    In my case, the server computer has to run the server application and the unity Master Server (according to what I understood about unity network)
    The problem is: if I have the Master Server running, when I try to run the server game unity returns a error message:
    Did anybody have this problem already? How can I fix it?

    Thanks a lot
     
  49. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    This can only really be the case when the same port is already in use on the same pc. Make sure that you're not running two unity servers on this same PC (e.g. when editor and a build both ran .InitializeServer with the same port)
     
  50. diegomazala

    diegomazala

    Joined:
    Mar 15, 2010
    Posts:
    21
    Hi Leepo. Thanks for your attention

    In my case, I have only the Master Server app (downloaded from unity website) running and after that I run Unity Editor.
    My pipeline is as follow:

    1) Start Master Server

    2) Start Unity Editor -> Example2_menu

    3) Edit MultiplayerFunction script at method SetupMasterServer

    4) Play app

    5) Error message popup


    Again, any help is welcome