2017-02-19 51 views
1

我正在嘗試瞭解Unet,但儘管如此,我仍然遇到了麻煩,儘管後面有幾個教程。Unity2D無法在所有客戶端同步我的精靈(多人遊戲)

我正在製作一款2D遊戲,我目前所堅持的是更新我的角色精靈旋轉的方式(左或右)。

Problem shown here

我的球員預製有:雪碧渲染,rigidbody2D,網絡身份(設置爲本地玩家的權威)和網絡轉換。

這是連接到每個球員的代碼:

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; 

    } 
} 

我知道這是最有可能的東西超級簡單,我做錯了,這只是增加了更多的痛苦來問這個,但任何幫助是極大的讚賞!謝謝

編輯:我的遊戲設置的方式,其中一個玩家既是主持人又是客戶端,我不知道這是否會讓遊戲變得更難,但這只是團結的方式, 。

+0

一個好的提示是把你的客戶端和服務器代碼放到*完全不同的文件*中。您經常會看到示例代碼,它在同一個文件中被鎖定。如果你只是使用兩個單獨的文件,它對於初學者來說實際上更簡單和更合理。 – Fattie

+0

謝謝,但在我的問題上嘗試的答案會更有幫助;) – Fross

+0

fross - 夠公平的,但我只是不能被打擾。我知道你會想出來:) – Fattie

回答

0

我想通了閱讀上的谷歌前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必須調用函數無論是在本地還是在服務器上!