2017-02-19 13 views
1

私はUnetについて学んでいますが、いくつかのチュートリアルにもかかわらず、問題が発生しています。Unity2Dは、すべてのクライアント(マルチプレイヤー)にスプライトを同期できません。

私は2dゲームを作っていますが、現在私が固執しているのは、私のキャラクタースプライトが左右に回転している様子を更新しています。

Problem shown here

マイプレイヤーのプレハブがあります:スプライトレンダラ、rigidbody2D、(ローカルプレイヤー権限に設定)ネットワークIDとネットワークがトランスフォーム。

これは、各プレイヤーに添付コード:

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

public class CharController : NetworkBehaviour { 

    public float maxSpeed = 1f; 
    [SyncVar] 
    bool facingRight = true; 
    Animator anim; 


    // Use this for initialization 
    void Start() { 
     anim = GetComponent<Animator>(); 
    } 

    // Update is called once per frame 
    void Update() { 

    } 
    void Moving() 
    { 


      float move = Input.GetAxis("Horizontal"); 

      GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y); 

      if(move > 0 && !facingRight) 
      { 
       RpcFlip(); 
      } 
      else if(move < 0 && facingRight) 
      { 
       RpcFlip(); 
      } 
      anim.SetFloat("MovingSpeed", Mathf.Abs(move)); 
    } 

    private void FixedUpdate() 
    { 
     if (!isLocalPlayer) 
     { 
      return; 
     } 
     // handle input here... 
     Moving(); 







    } 
    [ClientRpc] 
    void RpcFlip() 
    { 
     if (isLocalPlayer) 
     { 
      //facingRight = !facingRight; 
      //currentSprite.flipX = !currentSprite.flipX; 


     } 

     facingRight = !facingRight; 
      Vector3 theScale = transform.localScale; 
      theScale.x *= -1; 
      transform.localScale = theScale; 

    } 
} 

私はそれだけでこれを尋ねるに多くの痛みを追加し、私が間違ってやっているスーパーシンプル最も可能性が高い何か、知っているが、任意のヘルプは大歓迎です!ありがとう

編集:私のゲームがセットアップされる方法、選手の1人はホストとクライアントの両方ですが、これがより難しくなるかどうかはわかりませんが、それはローカルでプレーするとき。

+0

良いヒントは、クライアントとサーバーのコードを*完全に別のファイル*に入れることです。あなたは、同じファイルに括弧で囲まれているサンプルコードをよく見ます。実際には2つの別々のファイルを使用するだけで、初心者にとってはもっと簡単で論理的です。 – Fattie

+0

ありがとうございましたが、私の質問で答えが出されなかったのはもっと助かりました;) – Fross

+0

fross - fair十分ですが、私は気にしませんでした。私はあなたがそれを理解するだろうと知っていた:) – Fattie

答えて

0

私はGoogleで最初の5ページを読んだ後、それを考え出した、とここで私の最終的なコードで、同じ問題を持つ他の誰がここでつまずく場合:

public float maxSpeed = 1f; 
    Animator anim; 

    [SyncVar(hook = "FacingCallback")] //Everytime the bool netFacingRight is called, the method FacingCallback is called aswell. 
             //Since the bool only changes when CmdFlipSprite is called, it makes sure that CmdFlipSprite calls 
             //the change on our server, and FacingCallback calls it locally. 
    public bool netFacingRight = true; 

    // Use this for initialization 
    void Start() { 
     anim = GetComponent<Animator>(); 
    } 


    [Command] 
    public void CmdFlipSprite(bool facing) 
    { 
     netFacingRight = facing; 
     if (netFacingRight) 
     { 
      Vector3 SpriteScale = transform.localScale; 
      SpriteScale.x = 1; 
      transform.localScale = SpriteScale; 
     } 
     else 
     { 
      Vector3 SpriteScale = transform.localScale; 
      SpriteScale.x = -1; 
      transform.localScale = SpriteScale; 
     } 
    } 

    void FacingCallback(bool facing) 
    { 
     netFacingRight = facing; 
     if (netFacingRight) 
     { 
      Vector3 SpriteScale = transform.localScale; 
      SpriteScale.x = 1; 
      transform.localScale = SpriteScale; 
     } 
     else 
     { 
      Vector3 SpriteScale = transform.localScale; 
      SpriteScale.x = -1; 
      transform.localScale = SpriteScale; 
     } 
    } 

    //////// 




    // Update is called once per frame 
    void Update() { 

     if (!isLocalPlayer) 
     { 
      return; 
     } 
     float move = Input.GetAxis("Horizontal"); 

     GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y); 

     if ((move > 0 && !netFacingRight) || (move < 0 && netFacingRight)) 
     { 
      netFacingRight = !netFacingRight; 
      CmdFlipSprite(netFacingRight); 
      anim.SetFloat("MovingSpeed", Mathf.Abs(move)); 
     } 
    } 

あなたはaparently機能を呼び出す必要がありますローカルとサーバーの両方で!

関連する問題