質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

274閲覧

UnityNetwork アニメーションの同期について

Kei0206

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2018/03/14 08:41

編集2018/03/15 07:22

今Unityで2dのマルチプレイヤーゲームを作っています
そこで質問です
isLocalPlayerでDashのアニメーションを止めているんですがその時に一緒に書いてある画像の反転(左向きに進んでいる時はlocalScale.xを-1にし画像を左向きに、右向きに進んでいる時は1にし画像を右向きにするもの)までが止まってしまいます
どうにかして画像を反転させる方法はありませんか?

c#

1void FixedUpdate () 2 { 3 4 5 6 if (!gameClear) { 7 if (!isLocalPlayer) { 8 return; 9 } 10 float x = Input.GetAxisRaw ("Horizontal"); 11 if (x != 0) { 12 rigidbody2D.velocity = new Vector2 (x * speed, rigidbody2D.velocity.y); 13 Vector2 temp = transform.localScale; 14 temp.x = x; 15 transform.localScale = temp; 16 17 anim.SetBool ("Dash", true); 18 if (transform.position.x > mainCamera.transform.position.x - 4) { 19 Vector3 cameraPos = mainCamera.transform.position; 20 cameraPos.x = transform.position.x + 4; 21 mainCamera.transform.position = cameraPos; 22 } 23 Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0)); 24 Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1)); 25 Vector2 pos = transform.position; 26 pos.x = Mathf.Clamp (pos.x, min.x + 0.5f, max.x); 27 transform.position = pos; 28 } else { 29 rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y); 30 anim.SetBool ("Dash", false); 31 32 }
一応全てのコードを載せときます

c#

1using UnityEngine; 2using System.Collections; 3using UnityEngine.Networking; 4 5 6using UnityEngine.UI; 7 8 9 10public class PlayerScript :NetworkBehaviour 11{ 12 13 public float speed = 4f; 14 public float jumpPower = 700; 15 public LayerMask groundLayer; 16 public GameObject mainCamera; 17 public GameObject bullet; 18 public LifeScript lifeScript; 19 20 private Rigidbody2D rigidbody2D; 21 private Animator anim; 22 private NetworkAnimator netAnim; 23 private bool isGrounded; 24 private Renderer renderer; 25 26 private bool gameClear = false; 27 //ゲームクリアーしたら操作を無効にする 28 public Text clearText; 29 //ゲームクリアー時に表示するテキスト 30 31 32 void Start () 33 { 34 anim = GetComponent<Animator> (); 35 netAnim = GetComponent<NetworkAnimator> (); 36 rigidbody2D = GetComponent<Rigidbody2D> (); 37 renderer = GetComponent<Renderer> (); 38 } 39 40 void Update () 41 { 42 if (!isLocalPlayer) { 43 return; 44 } 45 46 isGrounded = Physics2D.Linecast ( 47 transform.position + transform.up * 1, 48 transform.position - transform.up * 0.05f, 49 groundLayer); 50 51 //ジャンプさせない 52 if (!gameClear) { 53 54 if (Input.GetKeyDown ("space")) { 55 if (isGrounded) { 56 netAnim.SetTrigger ("Jump"); 57 isGrounded = false; 58 rigidbody2D.AddForce (Vector2.up * jumpPower); 59 } 60 } 61 62 63 } 64 65 66 float velY = rigidbody2D.velocity.y; 67 bool isJumping = velY > 0.1f ? true : false; 68 bool isFalling = velY < -0.1f ? true : false; 69 anim.SetBool ("isJumping", isJumping); 70 anim.SetBool ("isFalling", isFalling); 71 72 //弾を打たせない、ゲームオーバーにさせない 73 if (!gameClear) { 74 75 if (Input.GetKeyDown ("left ctrl")) { 76 netAnim.SetTrigger ("Shot"); 77 Instantiate (bullet, transform.position + new Vector3 (0f, 1.2f, 0f), Quaternion.identity); 78 } 79 80 if (gameObject.transform.position.y < Camera.main.transform.position.y - 8) { 81 lifeScript.GameOver (); 82 } 83 84 } 85 86 } 87 88 void FixedUpdate () 89 { 90 91 92 93 //左右に移動させない MainCameraを動かさない 94 if (!gameClear) { 95 96 if (!isLocalPlayer) { 97 return; 98 } 99 float x = Input.GetAxisRaw ("Horizontal"); 100 if (x != 0) { 101 rigidbody2D.velocity = new Vector2 (x * speed, rigidbody2D.velocity.y); 102 Vector2 temp = transform.localScale; 103 temp.x = x; 104 transform.localScale = temp; 105 106 anim.SetBool ("Dash", true); 107 if (transform.position.x > mainCamera.transform.position.x - 4) { 108 Vector3 cameraPos = mainCamera.transform.position; 109 cameraPos.x = transform.position.x + 4; 110 mainCamera.transform.position = cameraPos; 111 } 112 Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0)); 113 Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1)); 114 Vector2 pos = transform.position; 115 pos.x = Mathf.Clamp (pos.x, min.x + 0.5f, max.x); 116 transform.position = pos; 117 } else { 118 rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y); 119 anim.SetBool ("Dash", false); 120 121 } 122 123 } else { 124 //クリアーテキストを表示 125 clearText.enabled = true; 126 //アニメーションは走り 127 anim.SetBool ("Dash", true); 128 //右に進み続ける 129 rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y); 130 //5秒後にタイトル画面へ戻るCallTitleメソッドを呼び出す 131 Invoke ("CallTitle", 5); 132 } 133 134 } 135 136 void OnCollisionEnter2D (Collision2D col) 137 { 138 if (!gameClear) { 139 if (col.gameObject.tag == "Enemy") { 140 StartCoroutine ("Damage"); 141 } 142 } 143 } 144 145 IEnumerator Damage () 146 { 147 gameObject.layer = LayerMask.NameToLayer ("PlayerDamage"); 148 int count = 10; 149 while (count > 0) { 150 renderer.material.color = new Color (1, 1, 1, 0); 151 yield return new WaitForSeconds (0.05f); 152 renderer.material.color = new Color (1, 1, 1, 1); 153 yield return new WaitForSeconds (0.05f); 154 count--; 155 } 156 gameObject.layer = LayerMask.NameToLayer ("Player"); 157 } 158 159 void OnTriggerEnter2D (Collider2D col) 160 { 161 //タグがClearZoneであるTriggerにぶつかったら 162 if (col.tag == "ClearZone") { 163 //ゲームクリアー 164 gameClear = true; 165 } 166 } 167 168 void CallTitle () 169 { 170 //タイトル画面へ 171 Application.LoadLevel ("Title"); 172 } 173 174}

状況がわかりやすいよう画像を追加しました
イメージ説明
このような感じで右の画面はキャラクターが左に走っていますが左の画面だとキャラクターが左を向いて走らなくなります
つまりクライアント側でホスト側のキャラの向きが反映されないということです(ホスト側でもそうなります)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakura_hana

2018/03/15 05:35

状況がよくわかりません。このソースだけだと「自キャラが移動中は画像反転する、停止中は反転処理は行われない」「相手キャラは何もしない(移動も画像反転もしない)」と挙動します。あとは他のコンポーネントで何のパラメータを伝えているか(通信しているか)により見え方が変わります。この辺りを再確認してみてください。
guest

回答1

0

ベストアンサー

transform.localScaleが同期されていないのだと思います。
(NetworkTransformが付いていても恐らく対象外になっている)

左右が切り替わったタイミングでRPCをコールするのがスマートかと思います。
参考:How To Sync Scale in UNET - Unity Answers

投稿2018/03/15 07:48

sakura_hana

総合スコア11427

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kei0206

2018/03/15 10:59

解決しました、ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問