Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unity Multiplayer [Closed] [UNet] NetworkServer.Spawn - Rotations, Scaling etc

Discussion in 'Connected Games' started by MartinLyne, Jun 28, 2015.

  1. MartinLyne

    MartinLyne

    Joined:
    Apr 25, 2013
    Posts:
    30
    Hi,
    I'm not sure if this was ever a thing, I'm sure I read in one of the docs that when the server spawns an object to the clients it sends the Position/Rotation/Scale. I've been wrestling with another issue for days and now I come back to this basic example and not sure where I stand.

    I use this on a scene object with a network ID to spawn other items. It's a simplified debugging test of course.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class NetSpawner : NetworkBehaviour {
    7.  
    8.     public override void OnStartServer () {
    9.         Debug.Log("Server started callback");
    10.         if (isServer) {
    11.             Spawn();
    12.         }
    13.     }
    14.  
    15.     void Awake () {
    16.         Debug.Log("Awake");
    17.         ClientScene.RegisterPrefab(Resources.Load("TestObject") as GameObject);
    18.     }
    19.  
    20.     void Spawn () {
    21.         Debug.Log("Spawn");
    22.         Object prefab = Resources.Load("TestObject");
    23.         GameObject go = Instantiate(prefab, Vector3.forward, Quaternion.Euler(45f, 45f, 45f)) as GameObject;
    24.         go.transform.localScale = new Vector3(3, 3, 3);
    25.         NetworkServer.Spawn(go);
    26.     }
    27. }
    28.  
    It spawns the item on the server correctly but the client has it without rotations/scaling. Is this a bug or working as intended? Evenif I use a client RPC after the spawn to update the position/rotation that won't work for clients that join later as the ClientRPCs arent buffered. So I'd have to make a local script for each spawnable that has a syncvar for their details.. seems kinda wasteful for objects that after spawning may not do anything (some could be enemies etc, some might just be buildings).

    Thanks for any help.
     
    Last edited: Jun 29, 2015
  2. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    23
    Can you try your Spawn() method outside OnStartServer ()? Bind a keydown and try it see if stills the same. Maybe your indeed going to need the syncvars.
     
  3. MartinLyne

    MartinLyne

    Joined:
    Apr 25, 2013
    Posts:
    30
    So I added a command that just calls spawn on the server linked to a keypress from client.

    Same thing happens, server spawns it right, clients don't get rotation/scale (but they do get position).

    However, on the client I do now get an error:
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. UnityEngine.Networking.NetworkClient.OnCRC (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:626)
    4. UnityEngine.Networking.NetworkConnection.HandleMessage (System.Collections.Generic.Dictionary`2 handler, UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:301)
    5. UnityEngine.Networking.NetworkClient.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:472)
    6. UnityEngine.Networking.NetworkClient.UpdateClients () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:686)
    7. UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:707)
    8.  
    Maybe this is trying to update the client but failing? This is the simplest possible implementation, I don't see what I could be doing wrong.
     
  4. MartinLyne

    MartinLyne

    Joined:
    Apr 25, 2013
    Posts:
    30
    So looks like Spawn does not transmit rotation/scale info (someone on IRC showed me decompiled version of the source, it only does position)

    Had to add this to my objects:
    http://hastebin.com/odimokecal.avrasm
     
    Zullar and darkmask58 like this.
  5. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    I'm having the same issue. Spawn is copying the correct position, but not rotation. Thanks for the post.
     
  6. Mikekan13

    Mikekan13

    Joined:
    May 30, 2015
    Posts:
    72
    How exactly do you get this working? How do you modify NetCorrector?