0
\$\begingroup\$

I'm trying out the new Unity 5.1 networking for a Bomberman style game and i can't seem to get the bombs to instantiate on both the client and all those connected, what am i missing?

The code below instantiates a prefab at the players current position, that prefab has a Network Identity and a Network Transform same as the players, i thought if the object is spawned using "NetworkServer.Spawned(Object)" then that would be instantiated on both clients?

    // Update is called once per frame
void Update()
{
    if (isLocalPlayer)
    {
        this.transform.Translate(new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0.0f));

        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject item = Instantiate(prefab, this.transform.position, Quaternion.identity) as GameObject;
            NetworkServer.Spawn(item);
        }
    }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ forum.unity3d.com/threads/… \$\endgroup\$
    – House
    Commented Jun 14, 2015 at 14:51
  • \$\begingroup\$ Thank you, i didn't find that whilst trawling Google.. I've resolved the problem \$\endgroup\$
    – Syphonx
    Commented Jun 14, 2015 at 14:59

1 Answer 1

2
\$\begingroup\$

It seems i misread the docs for [Command], the objects are now instantiated on both clients

// Update is called once per frame
void Update()
{
    if (isLocalPlayer)
    {
        this.transform.Translate(new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0.0f));

        if (Input.GetKeyDown(KeyCode.Space))
        {
            CmdSpawnBomb(this.transform.position);
        }
    }
}

[Command]
void CmdSpawnBomb(Vector3 pos)
{
    GameObject item = Instantiate(prefab, pos, Quaternion.identity) as GameObject;
    NetworkServer.Spawn(item);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .