今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}
状況がわかりやすいよう画像を追加しました
このような感じで右の画面はキャラクターが左に走っていますが左の画面だとキャラクターが左を向いて走らなくなります
つまりクライアント側でホスト側のキャラの向きが反映されないということです(ホスト側でもそうなります)
状況がよくわかりません。このソースだけだと「自キャラが移動中は画像反転する、停止中は反転処理は行われない」「相手キャラは何もしない(移動も画像反転もしない)」と挙動します。あとは他のコンポーネントで何のパラメータを伝えているか(通信しているか)により見え方が変わります。この辺りを再確認してみてください。
回答1件
あなたの回答
tips
プレビュー