2017-11-08 3 views
0

私は小さなオンラインマルチプレイヤーゲームを作成しています。 と私はいくつかのステップを管理しました。私はプレハブプレーヤーを設定し、私は、シーンにそのおかげでオブジェクトをインスタンス化するために管理:ユニティネットワーキングの変更gameオブジェクトのスプライト

[Command] 
void Cmdbars() 
{ 
    GameObject bar = Instantiate(barH, GameObject.Find("pos1").GetComponent<Transform>().transform.position, Quaternion.identity) as GameObject; 

    NetworkServer.Spawn(bar); 
} 

は、今私たちは、このオブジェクトにそのスプライトの変更をクリックした場合、そのたい。 はそのため私は、このメソッドを使用します。

[Command] 
void Cmdclick() 
{ 
    if (Input.GetMouseButtonDown(0)) 
    { 
     Vector2 origin = new Vector2(
         Camera.main.ScreenToWorldPoint(Input.mousePosition).x, 
         Camera.main.ScreenToWorldPoint(Input.mousePosition).y); 

     RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f); 


     if (hit && hit.transform.gameObject.tag.Equals("Untagged")) 
     { 
      hit.transform.gameObject.GetComponent<SpriteRenderer>().sprite = blueBarre.GetComponent<SpriteRenderer>().sprite; 
      hit.transform.gameObject.tag = "ok"; 
     } 
    } 
} 

問題は、ローカルでのみではなく、すべてのプレイヤーでスプライト変化。

答えて

1

すべてのクライアントでコードを実行する場合は、ClientRpc属性の「Rpc」メソッドを使用します。例えば

あなたのケースで、それはこのようにする必要があります

private void OnClick() 
{ 
    Vector2 origin = new Vector2(
        Camera.main.ScreenToWorldPoint(Input.mousePosition).x, 
        Camera.main.ScreenToWorldPoint(Input.mousePosition).y); 

    CmdOnClick(origin.x, origin.y);   
} 

//this code will be executed on host only, but can be called from any client 
[Command(channel = 0)] 
private void CmdClickHandling(float x, float y) 
{ 
    RpcClick(x, y); 
} 

//this code will be executed on all clients 
[ClientRpc(channel = 0)] 
private void RpcClickHandling(float x, float y) 
{ 
    //quit if network behaviour not local for preventing executing code for all network behaviours 
    if (!isLocalPlayer) 
    { 
     return; 
    } 

    Vector2 osrigin = new Vector2(x, y); 
    RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f); 

    if (hit && hit.transform.gameObject.tag.Equals("Untagged")) 
    { 

     hit.transform.gameObject.GetComponent<SpriteRenderer>().sprite = blueBarre.GetComponent<SpriteRenderer>().sprite; 
     hit.transform.gameObject.tag = "ok"; 
    } 
} 
関連する問題