Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Objects not parenting or translating correctly across ntwrk

Discussion in 'Connected Games' started by amherst, Jun 30, 2009.

  1. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    I am working on a Multiplayer Networked architectural tool. The function I am building is supposed to work like this:
    The player can create a cube in front of them. They can add a geometric shape to the face of the cube. The shape is parented to the cube so that if the player resizes, translates or deletes the cube, the shape does the same along with the cube. The player that creates the cube is the only one who can manipulate the cube, but all other players should be able to view the cube and any changes made to it.

    How I've written the code is that the shape is instantiated at the same global position as the cube, then it is translated in position so that it lies on the surface of the cube. The shape is then parented to the cube.

    The problem I'm having is this:
    If I use RPCMode.all when I call the function from the server to add a geometric shape there are actually two instances made of the shape, one which is where it's supposed to be on the surface of the cube and parented to the cube, and one which is at the global position of the cube (i.e. inside the cube) and is not parented to the cube. It shows up like this on both the client and the server.

    If I use RPCMode.others when I call the function from the server to add the geometric shape, there is only one instance made of the shape on both the client and the server. However, on the client it appears on the face of the cube and parented to the cube (even though the calling function is coming from the server), and on the server the shape appears at the global position of the cube and not parented to the cube.

    Here is my calling function. The var "normalInfo" just tells Unity which direction in relation to the camera I need to translate the shape.
    Code (csharp):
    1.  
    2. networkView.RPC("addShapeToSurface", RPCMode.All, normalInfo, transform.position);
    3.  
    And here is the function that is supposed to instantiate, place and parent the shape. It is in a script attached to the cube object which has a NetworkView pointing to the script. State Synchronization is set to off on this NetworkView. The var "shapeObject" is the prefab geometric shape that is supposed to appear on the surface of the cube. That prefab has a NetworkView attached to it which points to the prefab's transform. It also has StateSynchronization set to off.
    Code (csharp):
    1.  
    2. @RPC
    3. function addShapeToSurface(theNormalInfo : Vector3, thePosition : Vector3) {
    4.     var newAddedObject = Network.Instantiate(shapeObject, thePosition, transform.rotation, 0);
    5.     newAddedObject.transform.parent = theCubeObject.transform;
    6.     newAddedObject.transform.Translate(theNormalInfo*2);
    7. }
    8.  
    Any advice would be greatly appreciated!
    Thanks!
     
  2. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Hey there!
    Firstly I would like to point out that with an RPC function you wouldn't use network.instantiate

    var newAddedObject = Network.Instantiate(shapeObject, thePosition, transform.rotation, 0);

    Instead, you would only run Network.instantiate from the client that it producing the cube. This will send out the rpc to all clients anyway as network.instantiate automatically creates the RPC :)

    Hope that helps!