Search Unity

NetworkServer.Spawn, rotation on client

Discussion in 'Scripting' started by panicq, Jun 18, 2015.

  1. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Hi,

    I'm trying to Spawn a bullet on all the clients with the NetworkServer.Spawn method of UNet, the bullets are correctly spawning at the right location but it seams that the Quaternion that was send to the command couldn't be used on the clients. So it works on the server, but not on the clients. Here is my script:

    Code (CSharp):
    1. Update:
    2. if (isLocalPlayer) {
    3.             if(Input.GetKeyDown(KeyCode.Space))
    4.             {
    5.                 CmdTellMyShootToTheServer();
    6.             }
    7. }
    8.  
    9.     [Command]
    10.     void CmdTellMyShootToTheServer()
    11.     {
    12.         GameObject bullet = Instantiate (bulletPrefab, myTransform.position, myTransform.rotation) as GameObject;
    13.         NetworkServer.Spawn (bullet);
    14.     }
    The bullet script is simply a forward translation. I attached a network id component on it and i register it in the network manager.

    PS: If I attach a network transform, it works but I don't want that because it takes bandwidth for nothing in order to sync the position.

    Any ideas ?
     
  2. Stinger2121

    Stinger2121

    Joined:
    Aug 17, 2013
    Posts:
    2
    Hello!

    I ran into the same problem just now. here is how I solved it:

    In the documentation here:
    http://docs.unity3d.com/Manual/UNetSpawning.html
    It said that all SyncVars are autoamtically synced when an object spawns, so making the rotation of the object a syncVar and setting it on spawn fixed the problem.

    Why position is automatically synced in the background but rotation is not - I have absolutely no idea.

    In my Bullet class I added this:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5. public class movingForward : NetworkBehaviour
    6. {
    7.     [SyncVar]
    8.     public Quaternion direction;
    9.  
    10.     public float SpeedInUnitsPerSecond;
    11.     void Update ()
    12.     {
    13.        this.transform.rotation = direction;
    14.         transform.Translate(Vector3.forward * SpeedInUnitsPerSecond * Time.deltaTime, Space.Self);
    15.     }
    16. }
    And in the place where I spawn it I added this:
    Code (CSharp):
    1.  
    2.     [Command]
    3.     void CmdShoot(Vector3 position, Quaternion rotation)
    4.     {
    5.         GameObject newProjectile = (GameObject)GameObject.Instantiate(projectile, position, rotation);
    6.         newProjectile.GetComponent<movingForward>().direction = rotation;
    7.  
    8.         NetworkServer.Spawn(newProjectile);
    9.     }
     
    Last edited: Jun 26, 2015
  3. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    247
    I have this issue as well, only for scene objects (buildings)
    I just tried 5.2.0f1 and there it happens as well.
     
  4. bteitler

    bteitler

    Joined:
    May 11, 2014
    Posts:
    52
    This has been plaguing me as well :(
     
  5. HugoZandel

    HugoZandel

    Joined:
    Mar 11, 2014
    Posts:
    52
  6. someusernamewhatever

    someusernamewhatever

    Joined:
    Nov 8, 2014
    Posts:
    2
    Has a dev ever weighed in on whether this is a bug (and if so, when it will be fixed)? It's a bit frustrating to have to remember to sync rotation but not the rest of the transform.
     
  7. robotfido

    robotfido

    Joined:
    Jun 21, 2013
    Posts:
    1
    Bump. Would really like not to have to make an extra rotation syncvar on each object that needs to start pointing the right way when spawned. The workaround Stinger2121 worked for me, but at the very least shouldn't NetworkTransform do this for you provided it has rotation synced in its equivalent of OnStartClient?