0

Im trying to make a tetris game, where the host moves his blocks, and the client moves his.

Client on the left, host on the right.

Client on the left, host on the right.

So the host is suppost the move the object with the "playerObject" tag. The client is suppost to move the object with the "enemeyObject" tag.

Seems like the client is not using the "enemyObject" tag, but is moving the playerObject object.

The code for spawning in the objects:

    [ClientRpc]
void RpcSetTag()
{
    playerBlock.transform.gameObject.tag = "playerObject";
    enemyBlock.transform.gameObject.tag = "enemyObject";
}
IEnumerator SpawnObjects()
{
    rnd = Random.Range(0, objects.Length);
    playerBlock = Instantiate(objects[rnd], spawnPointPlayer.transform.position, objects[rnd].transform.rotation);
    enemyBlock = Instantiate(objects[rnd], spawnPointEnemy.transform.position, objects[rnd].transform.rotation);
    NetworkServer.Spawn(playerBlock);
    NetworkServer.Spawn(enemyBlock);
    RpcSetTag();
    yield return new WaitForSeconds(6f);
    CmdStartTimer();
}

the code to move the objects:

  void Update()
{
    if (isServer)
    {
        Debug.Log("SERVER");
        Server = true;
    }
    if (!isServer)
    {
        Client = true;
        Debug.Log("CLIENT");
    }
    if (Server == true)
    {
        block = GameObject.FindGameObjectWithTag("playerObject");
        Debug.Log(block);
    }
    if (Client == true)
    {
        block = GameObject.FindGameObjectWithTag("enemyObject");
        Debug.Log(block);
    }
    if (pressingL == true)
    {
        CmdMoveLeft();
        Debug.Log("MOVE LEFT");
    }
    if (pressingR == true)
    {
        CmdMoveRight();
        Debug.Log("MOVE RIGHT");
    }
    if (Input.GetKey(KeyCode.D))
    {
        Debug.Log("D");
        CmdMoveRight();
    }

    if (Input.GetKey(KeyCode.A))
    {
        Debug.Log("A");
        CmdMoveLeft();
    }

    if (Input.GetKeyDown(KeyCode.E))
    {
        CmdRotateRight();
        //background.transform.position = new Vector3(0.01f, 0, 0);
        //background.transform.Rotate(-90, 0, 0);
    }
    if (Input.GetKeyDown(KeyCode.Q))
    {
        CmdRotateLeft();
        //background.transform.position = new Vector3(0.01f, 0, 0);
        //background.transform.Rotate(+90, 0, 0);
    }
}

[Command]
public void CmdRotateLeft()
{
    block.transform.Rotate(-90, 0, 0);
}
[Command]
public void CmdRotateRight()
{
    block.transform.Rotate(+90, 0, 0);
}

[Command]
public void CmdMoveLeft()
{
    block.transform.Translate(Vector3.left * 1f * Time.deltaTime, Space.World);
}

[Command]
public void CmdMoveRight()
{
    block.transform.Translate(Vector3.right * 1f * Time.deltaTime, Space.World);
}
  • 1
    So, the statement `block = GameObject.FindGameObjectWithTag("enemyObject");` returns the playerObject? Is that correct? (Check with debugging) – RUL Oct 31 '18 at 08:44
  • "block" is the spawned tetris, so for example, if you look at the screenshot it would be that white block thats about to land. The one of the left should have the tag "enemyObject" and the right one "playerObject". The block returns nothing on the client, but does on the host. – Jordy Rutjens Oct 31 '18 at 08:48
  • "The block returns nothing on the client, but does on the host" This is actually false, it does return the block too on the client. So NVM that statement. – Jordy Rutjens Oct 31 '18 at 08:57
  • But whick block exactly does it return. Both times the playerBlock? – RUL Oct 31 '18 at 09:00
  • yeah, the client returns the "playerObject" tagged object. The client does get into the code thats suppost to be in: if (Client == true) { block = GameObject.FindGameObjectWithTag("enemyObject"); Debug.Log(block); } – Jordy Rutjens Oct 31 '18 at 09:06
  • I'm guessing right now, but when setting the tags in `RpcSetTag()`, try `playerBlock.tag = "playerObject";` and `enemyBlock.tag = "enemyObject";`. I mean playerBlock should technically be GameObjects and only maybe must be casted first. Because I think, you want the actual blocks later on and not their transforms. – RUL Oct 31 '18 at 09:45
  • I think you rather should have a look at [SpawnWithClientAuthority](https://docs.unity3d.com/ScriptReference/Networking.NetworkServer.SpawnWithClientAuthority.html) – derHugo Oct 31 '18 at 10:14
  • Both of those fixes didnt work ;/ – Jordy Rutjens Oct 31 '18 at 12:32

0 Answers0