Search Unity

Synchronizing the Island Demo

Discussion in 'Multiplayer' started by Hypnos, Jun 13, 2009.

  1. Hypnos

    Hypnos

    Joined:
    May 6, 2009
    Posts:
    18
    I am begining to code network games in Unity.
    I tried to code the Island Demo game to play in network based on Network example that is in the Unity site. But I had problems to synchronizing the positions of the network players.

    Code (csharp):
    1.  
    2.  
    3. //NetworkRigidbody.cs NEW!
    4.  
    5. Vector3 pos;
    6. Quaternion rot;
    7.  
    8. function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
    9.    if (stream.isWriting) {
    10.       // Sending
    11.       Vector3 posNet = transform.position;
    12.       Quaternion rotNet = transform.rotation;
    13.  
    14.       stream.Serialize(ref posNet);
    15.       strem.Serialize(ref rotNet);
    16.    } else {
    17.  
    18.       // Receiving
    19.       Vector3 posNet = Vector3.Zero;
    20.       Quaternion rotNet = Quaternion.identity;
    21.  
    22.       stream.Serialize(ref posNet);
    23.       strem.Serialize(ref rotNet);
    24.  
    25.       pos = posNet;
    26.       rot = rotNet;
    27.    }
    28. }
    29.  
    30. function Update()  {
    31.    transform.position = pos;
    32.    transform.rotation = rot;
    33. }
    The "First Person Controller Prefab" had a camera, Graphics and a bunker to quickly visualize the prefab. I put a network view on this prefab to view the NetworkRigidbody.cs that is in it too. But when I tried to move the player all players move too.

    I didn't understand very well how the synchronizing works in Unity. Can somebody help me? How should I change the code to work correctly?

    Thx!
     
  2. Hypnos

    Hypnos

    Joined:
    May 6, 2009
    Posts:
    18
    Any ideas?
     
  3. Veli

    Veli

    Joined:
    Mar 10, 2009
    Posts:
    301
    Well just an idea, but have you tried addin networkviews to player objects and make the observe the transform componet of that object? Seems like youre over complaicating a bit.
     
  4. Hypnos

    Hypnos

    Joined:
    May 6, 2009
    Posts:
    18
    I added networkviews to the player objects and I make them to observe the script above like the Network Example.

    I just don't understand how do the Update function works in this code. How do the code recognize that the transform component, specified in the function, belongs to a client? How it works?

    The player prefab calls "First Person Controller Prefab" like Island Demo project. The prefab contains FPSWalker, MouseLook, NetworkRigidbody and CarNetworkInit (without the camera line code) scripts and its children are a camera, a graphics and a simple bunker to visualize the player.

    I tried a lot but it doesn't work =/
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the script is assigned to the corresponding object it wants to alter (that means that transform is the transform of the object the component is assigned to). This linking is done on the prefab thats then beeing instantiated for all remote players.
     
  6. Hypnos

    Hypnos

    Joined:
    May 6, 2009
    Posts:
    18
    So I must to make the networkview observes the transform component or the script?
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you update it through script then the script.
     
  8. Hypnos

    Hypnos

    Joined:
    May 6, 2009
    Posts:
    18
    Thx for the help!
    Now it's clear!