UNET 5.4 - Spawn an object from client...and move it around from client....?

I have tried [Commands] to [ClientRpc]s…and I have also tried NetworkServer.SpawnWithClientAuthority. The most success I have had is spawn an object on the server side…and it also spawns double on client side. I am assuming because the spawn is sent twice by the same player on both client and server. The client is still unable to move the object though.

Does anybody have a code example of how to spawn an object from a client, have it appear on both server and all clients properly?

I am trying to make it so my CoOp players can build towers on the same level…and so that the players can see a preview of the tower moving around on the map before it is placed.

it works perfectly fine locally…just cant get this to work over Unet.

Here is some of my code:

    if (spawn && building == false)
    {
        RaycastSpawnPoint();
    }

    //placing tower
    if (building)
    {
        PositonTower();
        build = true;
    }

}

void RaycastSpawnPoint()
{
    Ray spawnRay = playerCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    RaycastHit spawnRayHit;

    if (Physics.Raycast(spawnRay, out spawnRayHit))
    {
       CmdSpawnTower(towerSpawn);
    }
}


[Command]
void CmdSpawnTower(GameObject obj)
{
      //Do I need an isServer here?
      //how do I send authority back to client that spawns this?
     // Do I need an Rpc? I have Tried this but I still get no authority.
        GameObject tempObj = (GameObject)Instantiate(obj);
        NetworkServer.SpawnWithClientAuthority(tempObj, connectionToClient);
    
        spawnedTower = tempObj;
        childEmpty = (GameObject)Instantiate(gridHelper);
        Debug.Log("Spawning " + childEmpty.name);

        Debug.Log("Spawning " + spawnedTower.name);
        Collider spawnCol = spawnedTower.GetComponent<BoxCollider>();
        spawnCol.enabled = false;

        building = true;
        
}

All of my prefabs are in the network manager spawnables and they all have networkIdentity attached set to “local authority”.

any help is appreciated!

I figured it out

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class PlayerBuild : NetworkBehaviour {

[SerializeField]
private TowerManager towerManager;
[SerializeField]
private float gridSnap = 1;
[SerializeField]
private GameObject gridHelper;
[SerializeField]
private LayerMask buildMask;
[SerializeField]
private Camera playerCamera;

public bool build = false;
private bool spawn = false;

private bool building;

private GameObject spawnedTower;
private GameObject childEmpty;

private Vector3 childPos;
private Vector3 gridScale;

private GameObject towerSpawn;
private TowerItem towerItem;
private bool towerSwitch = false;

public NetworkHash128 assetId { get; set; }

public delegate GameObject SpawnDelegate(Vector3 position, NetworkHash128 assetId);
public delegate void UnSpawnDelegate(GameObject spawned);

// Use this for initialization
void Start () {

    ClientScene.RegisterSpawnHandler(assetId, Spawn, UnSpawn);
    
    //Assign First Tower to tower spawn
    towerSpawn = towerManager.GetTower(1).GetPrefab();
    towerManager.SetTowerUIChosenTower(1);

}

// Update is called once per frame
void Update () {

    if (!isLocalPlayer)
    {
        return;
    }

    //loop through all quick commands
    for (int i = 1; i <=8 ; i++)
    {
        if (Input.GetButtonDown("Quick" + i))
        {
            
            CmdSwitchTower(i);
            towerSwitch = true;
            towerManager.SetTowerUIChosenTower(i);
        }
    }
    
    //spawn tower
    if (Input.GetButtonDown("Build"))
    {
        spawn = !spawn;
        towerSwitch = false;

        if (spawn == false)
        {
            CmdUnspawnCurTower();
            build = false;
            building = false;
        }
    }

    if (spawn && building == false)
    {
        RaycastSpawnPoint();
    }

    //placing tower
    if (building && childEmpty != null)
    {
        PositonTower();
        build = true;
    }

}

[Command]
void CmdSwitchTower(int i)
{
    towerItem = towerManager.GetTower(i);
    towerSpawn = towerItem.GetPrefab();
    
}

[Command]
void CmdUnspawnCurTower()
{
    if (spawnedTower != null && childEmpty != null)
    {
        Destroy(childEmpty);
        Debug.Log(childEmpty.name + " was destroyed");

        UnSpawn(spawnedTower);
        NetworkServer.UnSpawn(spawnedTower);
       
        Debug.Log(spawnedTower.name + " was destroyed");
    }
    else
    {
        Debug.Log("could not unspawn tower");
    }

}

GameObject Spawn(Vector3 pos, NetworkHash128 assetId)
{
    return (GameObject)Instantiate(towerSpawn, pos, Quaternion.identity);
}

void UnSpawn(GameObject spawned)
{
    Destroy(spawned);
}

void RaycastSpawnPoint()
{
    Ray spawnRay = playerCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    RaycastHit spawnRayHit;

    if (Physics.Raycast(spawnRay, out spawnRayHit))
    {
        assetId = NetworkHash128.Parse("e2656f");
        CmdSpawnTower(spawnRayHit.point);
        building = true;
    }
}

[Command]
void CmdSpawnTower(Vector3 pos)
{
    
        spawnedTower = Spawn(pos, assetId);
        NetworkServer.SpawnWithClientAuthority(spawnedTower, connectionToClient);
        Debug.Log("Spawning " + spawnedTower.name);

        Collider spawnCol = spawnedTower.GetComponent<BoxCollider>();
        spawnCol.enabled = false;

    RpcSetSpawnValues(spawnedTower);
          
}
[ClientRpc]
void RpcSetSpawnValues(GameObject spawned)
{
    spawnedTower = spawned;

    childEmpty = (GameObject)Instantiate(gridHelper);
    Debug.Log("Spawning " + childEmpty.name);
}

void PositonTower()
{

    Ray buildRay = playerCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    RaycastHit buildRayHit;

    if (Physics.Raycast(buildRay, out buildRayHit, Mathf.Infinity, buildMask))
    {
        Collider buildHitCol = buildRayHit.collider;
        childEmpty.transform.SetParent(buildRayHit.collider.gameObject.transform);
        childPos = buildHitCol.transform.InverseTransformPoint(buildRayHit.point);

        //get tower floor scale
        gridScale = buildHitCol.transform.lossyScale;

        // snap to 1 meter (or your choosing) based on floor size
        float gridSnapX = gridSnap / gridScale.x;
        float gridSnapZ = gridSnap / gridScale.z;

        // snap to 1 meter using Rounding
        float snapX = Mathf.Round(childPos.x / gridSnapX) * gridSnapX;
        float snapZ = Mathf.Round(childPos.z / gridSnapZ) * gridSnapZ;

        childEmpty.transform.localPosition = new Vector3(snapX, 0, snapZ);

        //tower properties
        spawnedTower.transform.rotation = Quaternion.FromToRotation(Vector3.up, buildRayHit.normal);
        spawnedTower.transform.position = childEmpty.transform.position;

    }

    if (towerSwitch)
    {
        CmdUnspawnCurTower();
        RaycastSpawnPoint();
        towerSwitch = false;
    }

}

}