UNET Multi-Player game, having both players interact with gameObject, changes synchronized on host and client

梦想的初衷 提交于 2019-12-09 23:16:22

问题


I want to create a simple demo. I want there to be a cube, in the middle of a plane, that changes colors when you click it. (This was easy to implement) I want there to be two players, who take turns clicking the cube. The cube will only change colors if it is your turn. If the cube changes colors, the change will reflect on both the players' screens. I've been looking at the examples for UNET, http://forum.unity3d.com/threads/unet-sample-projects.331978/, and most of them have a networked character who you control with your keyboard, and this aspect is throwing me off. Do I still need to create 2 players, but just have them be invisible and have no control scripts? Should my block be a prefab? Here's my script for my block:

void Update()
{
      if (Input.GetKeyDown(KeyCode.Space))  
      {
          // Command function is called from the client, but invoked on the server
           CmdChangeColor();
       }
 }

[Command]
void CmdChangeColor()
{
      if (cubeColor == Color.green) cubeColor = Color.magenta;
      else if (cubeColor == Color.magenta) cubeColor = Color.blue;
      else if (cubeColor == Color.blue) cubeColor = Color.yellow;
      else if (cubeColor == Color.yellow) cubeColor = Color.red;
      else cubeColor = Color.green;

      GetComponent<Renderer>().material.color = cubeColor;
 }

Also I'll note that my Block isn't currently a prefab. I have the Network Identity component enabled, as well as the network transform->Sync transform. When I start the server host, I'm able to change the color of the block, but the client can't view these changes. When the client clicks the block, nothing happens, except the error message: Trying to send command to the object without authority.

Any help would be appreciated! Thank you http://docs.unity3d.com/Manual/UNetSetup.html


回答1:


So I finally was able to get this to work, thanks to this StackOverflow post.

Here's my script, I attached it to the Player Object, not the non-Game Object that I wanted to be synchronized over the network.

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

public class OnTouchEvent : NetworkBehaviour
{
    //this will get called when you click on the gameObject
    [SyncVar]
    public Color cubeColor;
    [SyncVar]
    private GameObject objectID;
    private NetworkIdentity objNetId;


    void Update()
    {
        if (isLocalPlayer)
        {
            CheckIfClicked();
        }
    }

    void CheckIfClicked()
    {
        if (isLocalPlayer && Input.GetMouseButtonDown(0))
        {
            objectID = GameObject.FindGameObjectsWithTag("Tower")[0];                         //get the tower                                   
            cubeColor = new Color(Random.value, Random.value, Random.value, Random.value);    // I select the color here before doing anything else
            CmdChangeColor(objectID, cubeColor);
        }
    }



    [Command]
    void CmdChangeColor(GameObject go, Color c)
    {
        objNetId = go.GetComponent<NetworkIdentity>();        // get the object's network ID
        objNetId.AssignClientAuthority(connectionToClient);    // assign authority to the player who is changing the color
        RpcUpdateCube(go, c);
        // use a Client RPC function to "paint" the object on all clients
        objNetId.RemoveClientAuthority(connectionToClient);    // remove the authority from the player who changed the color
    }

    [ClientRpc]
    void RpcUpdateCube(GameObject go, Color c)
    {
        go.GetComponent<Renderer>().material.color = c;
    }

}


来源:https://stackoverflow.com/questions/34191207/unet-multi-player-game-having-both-players-interact-with-gameobject-changes-sy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!