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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

2250閲覧

unityでオブジェクトの移動が正しく行われない

Hayato555

総合スコア17

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

0クリップ

投稿2017/10/23 07:23

###前提・実現したいこと
unityでテトリスをつくっているunityおよびC#の初心者です
テトリスのSRSを実装するために まずはTミノを https://tetrisch.github.io/main/srs.html ここを参考にしながら作っていたのですが、右回転したらTミノが右に向く状態になる右回転の部分で、おもうとおりに動きませんでした。
詳しく書きますと コードの482行目にあたるのですが思っているとおりに動いたならば
図(空白を白、ミノを黒、Tミノを紫で表します)

白紫白
紫紫紫
白黒黒
となっている状態から右回転をさせると、従来通り動くなら、
紫白白
紫紫白
紫黒黒
となるはずなのですが、下記のコードでは
白白白
白紫白
紫紫紫
白黒黒
の状態から
白紫白
白紫紫
白紫白
白黒黒
という変な動きをします。
何度もコードを見直してみたのですが、原因がわかりません。 
初心者なので、コードもみにくいかもしれませんが、どなたか原因がわかる方はいませんでしょうか?

###該当のソースコード

C#

1 2 3 4 public void rotateRight() 5 { 6 Grid.tSpin = false; 7 transform.Rotate(0, 0, -90); 8 foreach (Transform child in transform) 9 { 10 child.transform.Rotate(0, 0,90); 11 } 12 if (isValidGridPos()) 13 // It's valid. Update grid. 14 { 15 updateGrid(); 16 deleteGrid(); 17 } 18 else 19 { 20 if (transform.rotation == new Quaternion(0, 0, 90, 0)) 21 { 22 transform.position += new Vector3(1, 0, 0); 23 // See if valid 24 if (isValidGridPos()) 25 // It's valid. Update grid. 26 { 27 updateGrid(); 28 deleteGrid(); 29 } 30 else 31 { 32 transform.position += new Vector3(0, 1, 0); 33 // See if valid 34 if (isValidGridPos()) 35 // It's valid. Update grid. 36 { 37 updateGrid(); 38 deleteGrid(); 39 } 40 else 41 { 42 transform.position += new Vector3(-1, -3, 0); 43 // See if valid 44 if (isValidGridPos()) 45 // It's valid. Update grid. 46 { 47 updateGrid(); 48 deleteGrid(); 49 } 50 else 51 { 52 transform.position += new Vector3(1, 0, 0); 53 // See if valid 54 if (isValidGridPos()) 55 // It's valid. Update grid. 56 { 57 updateGrid(); 58 deleteGrid(); 59 } 60 else 61 { 62 transform.Rotate(0, 0, 90); 63 foreach (Transform child in transform) 64 { 65 child.transform.Rotate(0, 0, -90); 66 } 67 transform.position += new Vector3(-1, 2, 0); 68 69 } 70 } 71 } 72 } 73 } 74 else if (transform.rotation == new Quaternion(0, 0, -180, 0)) 75 { 76 transform.position += new Vector3(1, 0, 0); 77 if (isValidGridPos()) 78 // It's valid. Update grid. 79 { 80 updateGrid(); 81 deleteGrid(); 82 } 83 else 84 { 85 transform.position += new Vector3(0, -1, 0); 86 if (isValidGridPos()) 87 // It's valid. Update grid. 88 { 89 updateGrid(); 90 deleteGrid(); 91 } 92 else 93 { 94 transform.position += new Vector3(-1, 3, 0); 95 if (isValidGridPos()) 96 // It's valid. Update grid. 97 { 98 updateGrid(); 99 deleteGrid(); 100 } 101 else 102 { 103 transform.position += new Vector3(1, 0, 0); 104 if (isValidGridPos()) 105 { 106 updateGrid(); 107 deleteGrid(); 108 } 109 else 110 { 111 transform.Rotate(0, 0, 90); 112 foreach (Transform child in transform) 113 { 114 child.transform.Rotate(0, 0, -90); 115 } 116 transform.position += new Vector3(-1, -2, 0); 117 } 118 } 119 } 120 } 121 } 122 else if (transform.rotation == new Quaternion(0,0,-90,0)) 123 { 124 transform.position += new Vector3(-1, 0, 0); 125 if (isValidGridPos()) 126 // It's valid. Update grid. 127 { 128 updateGrid(); 129 deleteGrid(); 130 } 131 else 132 { 133 transform.position += new Vector3(0, 1, 0); 134 if (isValidGridPos()) 135 // It's valid. Update grid. 136 { 137 updateGrid(); 138 deleteGrid(); 139 } 140 else 141 { 142 transform.position += new Vector3(1, -3, 0); 143 if (isValidGridPos()) 144 // It's valid. Update grid. 145 { 146 updateGrid(); 147 deleteGrid(); 148 } 149 else 150 { 151 transform.position += new Vector3(-1, 0, 0); 152 if (isValidGridPos()) 153 // It's valid. Update grid. 154 { 155 updateGrid(); 156 deleteGrid(); 157 } 158 else 159 { 160 transform.Rotate(0, 0, 90); 161 foreach (Transform child in transform) 162 { 163 child.transform.Rotate(0, 0, -90); 164 } 165 transform.position += new Vector3(1, 2, 0); 166 } 167 } 168 } 169 } 170 } 171 else 172 { 173 transform.position += new Vector3(-1, 0, 0); 174 if (isValidGridPos()) 175 // It's valid. Update grid. 176 { 177 updateGrid(); 178 deleteGrid(); 179 } 180 else 181 { 182 transform.position += new Vector3(0, -1, 0); 183 if (isValidGridPos()) 184 // It's valid. Update grid. 185 { 186 updateGrid(); 187 deleteGrid(); 188 } 189 else 190 { 191 transform.position += new Vector3(1, 3, 0); 192 if (isValidGridPos()) 193 // It's valid. Update grid. 194 { 195 updateGrid(); 196 deleteGrid(); 197 } 198 else 199 { 200 transform.position += new Vector3(-1, 0, 0); 201 if (isValidGridPos()) 202 // It's valid. Update grid. 203 { 204 updateGrid(); 205 deleteGrid(); 206 } 207 else 208 { 209 transform.Rotate(0, 0, 90); 210 foreach (Transform child in transform) 211 { 212 child.transform.Rotate(0, 0, -90); 213 } 214 transform.position += new Vector3(1, -2, 0); 215 } 216 } 217 } 218 } 219 220 221 } 222 tSpinCheck(); 223 } 224 225 226 227 228 229 230 231 232 } 233 234 235 236

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

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

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

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

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

guest

回答1

0

ベストアンサー

コードを十分追っていないのですが、一つ気になる点として、ミノの姿勢をif (transform.rotation == new Quaternion(0, 0, 90, 0))という風に判定しようとしているのは、意図通りにはいかないのではないかな...?と感じました。

おそらく、Z軸周りの回転が90°か、-180°か、-90°か、それ以外(無回転)かで場合分けしたいのかと推察しますが、new Quaternion(0, 0, 90, 0)で生成されるのは「z成分が90のクォータニオン」のはずで、これは「Z軸周り90°回転を表現するクォータニオン」とは異なるかと思います(ややこしい話ですみませんが、クォータニオンのx、y、z、wはオイラー角がそのまま入っているわけではないのです...Quaternionによる3次元の回転変換 - Qiitaなどがご参考になるでしょうか?)。

オイラー角からのクォータニオン生成はEulerを使うのが便利かと思います。ひとまず3カ所の姿勢判定部分をif (transform.rotation == Quaternion.Euler(0, 0, 90))の形にしてみて、どうなるか確認されるのがよいのではないでしょうか。
あるいは、逆にtransform.rotationからオイラー角を抽出する形にして、if (Mathf.Approximately(transform.rotation.eulerAngles.z, 90))if (Mathf.Approximately(transform.rotation.eulerAngles.z, 180))if (Mathf.Approximately(transform.rotation.eulerAngles.z, 270))でもいいかもしれません(ついでに==で比較する代わりにApproximatelyを使って、もし小数の微妙な誤差が発生しても許容するようにしてみました)。

※ちなみに、Quaternion.Euler(0, 0, 90)で作ったクォータニオンの各成分を見てみると、xが0、yが0、zが0.7071068、wが0.7071068になっていました。回転軸が(0, 0, 1)で、sin(90° / 2) = cos(90° / 2) = 0.7071068...ですので、確かに先に挙げたサイトの解説と合致しておりました。

投稿2017/10/23 11:34

編集2017/10/23 22:41
Bongo

総合スコア10807

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問