客户端将带有标签的对象移动

时间:2018-10-31 08:36:01

标签: c# unity3d multiplayer unity3d-unet

我正在尝试制作俄罗斯方块游戏,其中主机移动他的积木,而客户端移动他的积木。

客户端在左边,主机在右边。

Client on the left, host on the right.

因此,主机支持使用“ playerObject”标签移动对象。 客户端支持使用“ enemeyObject”标签移动对象。

似乎客户端没有使用“ enemyObject”标签,而是在移动playerObject对象。

在对象中生成的代码:

    [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();
}

移动对象的代码:

  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);
}

0 个答案:

没有答案