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
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.