2016-11-17 7 views
0

私はUnity Networkingで自分の味方を得るために簡単なLANゲームを作っています。プレイヤーがグリッド内の四角形をクリックすると、青色に変わります。私の問題は、LANホストが四角形をクリックしたときにローカルでのみ更新され、クライアントを更新しないときです。クライアントが正方形をクリックすると、ローカルで更新され、LANホストは更新されますが、他のクライアントは更新されません。私のグリッドのすべてには、ネットワークIDが付いていますシンプルなUnity5 LANゲーム、サーバー更新クライアントはありません

アイデアはありますか?

相続コード:

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

    public class Player_Paint : NetworkBehaviour { 

     [SyncVar]GameObject syncGridPiece; 
     GameObject gridPiece; 

     void Update() { 
      Paint(); 
      TransmitGridColours(); 
     } 

     void Paint(){ 
      if(isLocalPlayer && Input.GetMouseButtonDown(0)){ 
       RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); 
       if(hit.collider != null){ 
        print(GameObject.Find (hit.transform.name)); 
        gridPiece = hit.collider.transform.gameObject; 
        gridPiece.GetComponent<SpriteRenderer>().color = Color.blue; 
       } 
      } 
     } 
     [Command] 
     void CmdProvideGridColourToServer(GameObject gridPiece){ 
      if(gridPiece){ 
       syncGridPiece = gridPiece; 
       syncGridPiece.GetComponent<SpriteRenderer>().color = Color.blue; 
      } 

     } 
     [Client] 
     void TransmitGridColours(){ 
      if(isLocalPlayer){ 
       CmdProvideGridColourToServer(gridPiece); 
      } 
     } 
    } 
+0

"if(gridPiece){"?それはGameObject ...ちょうど同期変数を更新する必要があります、すべてのプロパティ/値/ etcは、ローカルのプレーヤー(すなわち、色を変更し、GOを押し上げる)によってサーバーにプッシュされました –

+0

"if( gridPiece){"GOが空であるとゲームが始まるときでした。クリックされたときにのみ読み込まれます。ちょうどそれが私に参照エラーを投げるのを止めるためにそこに置いてください。私はあなたが何を意味するか完全に理解していません。とき私は "syncGridPiece = gridPiece;"変数を更新していませんか? – justinC19

+0

[Command] == [SyncVar]タグ付きメンバーのヴァースを使用して、サーバーにデータを送信するためにローカルプレーヤーで呼び出します。 [Client ** Connect **] == [Command]プレフィックス –

答えて

2

あなたのsyncGridPieceのsyncvarにフックを追加する必要があります。 フック機能では、コマンドのように、色を青に設定します。 フックはリモートプレーヤーで呼び出されます。

また、私はあなたがsyncvarとしてGameOjectを使用することはできないと思います。 IDを送信するだけです。

[SyncVar(hook=UpdateGridPiece)] int syncGridPiece 

多分、RPCはあなたがしたいことに適応しているでしょう。 syncGridPieceは正方形なので、同じメッセージ内のすべての更新を渡すべきではありません。 XプレイヤーがRPCでXの正方形をクリックしたことを送信してください。被害の例で

ルック:https://docs.unity3d.com/Manual/UNetActions.html

+0

ありがとうございます。私はClientRpcを完全に理解していませんが、それは実際に動作するために必要なものです。 – justinC19

+0

コマンドは、ローカルプレーヤーオブジェクトからサーバー(同じ)プレイヤーオブジェクトに関数を呼び出します。 ClientRPCは、サーバープレーヤーからすべてのクライアント上の(同じ)プレーヤーに関数を呼び出します。 – FLX

0

はそれを考え出しました。 ClientRpcを使用しなければならなかった。以下のコード。

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

public class Player_Paint : NetworkBehaviour { 
    GameObject gridPiece; 

    void Start() { 

    } 

    // Update is called once per frame 
    void Update() { 
     Paint(); 
    } 

    void Paint(){ 
     if(isLocalPlayer && Input.GetMouseButtonDown(0)){ 
      RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); 
      if(hit.collider != null){ 
       print(GameObject.Find (hit.transform.name)); 
       gridPiece = hit.collider.transform.gameObject; 
       print("SettingColor"); 
       CmdProvideGridColourToServer(gridPiece,Color.blue); 
      } 
     } 
    } 

    [Command] 
    void CmdProvideGridColourToServer(GameObject gridPiece,Color color){ 
     RpcTransmitGridColours(gridPiece,color); 

    } 
    [ClientRpc] 
    void RpcTransmitGridColours(GameObject gridPiece, Color color){ 
     gridPiece.GetComponent<SpriteRenderer>().color = color; 
    } 
} 
関連する問題