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

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

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

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

Unity

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

Q&A

解決済

3回答

8952閲覧

スクリプトがif文ばかりになる

lkiuxc

総合スコア29

C#

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

Unity

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

4グッド

8クリップ

投稿2018/10/04 11:24

こんにちは。まだラムダ式などが良く解らず勉強中なのですが、趣味でUnityを使いゲームを作っています。

早速本題に。スクリプトを使いプレイヤーを動かしたりしているのですが、if文がとても多いんです。例えば

using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyTestRoutine : MonoBehaviour { [SerializeField] GameObject enemyBullet; [SerializeField] GameObject player; [SerializeField] Rigidbody rb; private float shotTime = 0.05f; private bool shot = true; private float specialShotTime = 40; private bool specialShot = true; private float power = 80; private float speed = 30; Vector3 playerPoint; private float move = 10; int movePoint; private Vector3 escapeSky; private float downLimit = 5; private Vector3 down; private float capriciousDown = 3; private Vector3 capricious; private int Life = 10; // Use this for initialization void Start () { specialShot = false; move = 10; movePoint = Random.Range(0, 4); escapeSky = Vector3.zero; capricious = Vector3.zero; } Vector3 EnemyMove(int move) { switch (move) { case 0: return rb.velocity = transform.right * speed; case 1: return rb.velocity = -transform.right * speed; case 2: return rb.velocity = (-transform.forward / 2 + transform.right * 2) / 2 * speed; case 3: return rb.velocity = (-transform.forward / 2 + -transform.right * 2) / 2 * speed; } Debug.Log("EnemyMoveError"); return rb.velocity = Vector3.zero; } void SpecialShot() { Quaternion nin = Quaternion.LookRotation(player.transform.position - transform.position); for (int red = -40; red <= 40; red += 6) { Quaternion r = nin * Quaternion.Euler(0, red, 0); GameObject bullets = Instantiate(enemyBullet, gameObject.transform.position, r); bullets.GetComponent<Rigidbody>().AddForce(bullets.transform.forward * power, ForceMode.Impulse); } } void Shot() { Quaternion nin = Quaternion.LookRotation(player.transform.position - transform.position); GameObject bullets = Instantiate(enemyBullet, gameObject.transform.position, nin); bullets.GetComponent<Rigidbody>().AddForce(transform.forward * power, ForceMode.Impulse); } private void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Player1Bullet")) { Debug.Log("hit"); } } // Update is called once per frame void Update () { transform.LookAt(player.transform.position); if (GameStart.gameStart == 1) { shotTime -= Time.deltaTime; if (shotTime <= 0) { shot = true; } if (shot) { Shot(); shot = false; shotTime = 0.05f; } specialShotTime -= Time.deltaTime; if (specialShotTime <= 0) { specialShot = true; } if (specialShot) { SpecialShot(); specialShot = false; specialShotTime = 1; } move -= Time.deltaTime; if (move <= 0) { movePoint = Random.Range(0, 4); move = 5; } if (gameObject.transform.position.y >= 30) { escapeSky = -transform.up * speed; } else { escapeSky = Vector3.zero; } if (gameObject.transform.position.y >= 29) { downLimit -= Time.deltaTime; if (downLimit <= 0) { down = -transform.up * speed; } } if (gameObject.transform.position.y <= 0) { Debug.Log("a"); down = Vector3.zero; downLimit = 5; } capriciousDown -= Time.deltaTime; if (capriciousDown <= 0) { switch (Random.Range(0, 15)) { case 0: capricious = -transform.up * speed; break; case 1: capricious = transform.up * speed; break; default: capricious = Vector3.zero; break; } capriciousDown = 1; } if (Life <= 0) { Destroy(gameObject); } } } private void FixedUpdate() { if (GameStart.gameStart == 1) { rb.velocity = EnemyMove(movePoint) + escapeSky + down + capricious; } } }

このように。これは良い事なのでしょうか。何かとても悪い事のように考えてしまって、色々調べてみたのですが「ネストが深いのは良くない」とか、「スパゲティ死すべし」等書いてあっただけでして・・・・善悪だけでも構いません、教えていただけないでしょうか。

よろしくお願いします。

gemstone, luma, MMashiro, set0gut1👍を押しています

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

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

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

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

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

guest

回答3

0

ベストアンサー

無駄なフラグが多い

csharp

1 2 if (shotTime <= 0) 3 { 4 shot = true; 5 } 6 if (shot) 7 { 8 Shot(); 9 shot = false; 10 shotTime = 0.05f; 11 }

↓ やっていることはこれと同じ

csharp

1 if (shotTime <= 0) 2 { 3 Shot(); 4 shot = false; 5 shotTime = 0.05f; 6 }

これも同様。

csharp

1 2 if (shotTime <= 0) 3 { 4 shot = true; 5 } 6 if (shot) 7 { 8 Shot(); 9 shot = false; 10 shotTime = 0.05f; 11 }

csharp

1 if (GameStart.gameStart == 1) 2 { 3 // 処理① 4 }

↓早期リターンする

csharp

1 if (GameStart.gameStart != 1) 2 { 3 return; 4 } 5 6 // 処理①

これだけでも、入れ子も浅くなるし、余計な条件式もいらなくなりますね。

投稿2018/10/04 12:16

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

一つのクラスに対していろんな機能をもたせてそれをすべてUpdate内でやろうとするとif文が必然的に多くなってしまいます

一つの機能に対して一つのクラス(いわゆる単一責任の原則)をやってみるといいかもしれません。
特にUnityはコンポーネント指向なのでいろんなコンポーネント(クラス)を作ってGameObjectにアタッチしまくって一つの動作・動きになるっていう形が望ましいです

投稿2018/10/04 12:08

MMashiro

総合スコア2378

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

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

0

一つのところにいろいろ集中しすぎて、責任の分割とかがやれていない感じですね。
最近読んだ記事で、これが良かったです。
https://qiita.com/_-_-_-_-_/items/1f604f6205b8bfd8a823

あと、早期リターンをやりましょう。
https://blog.isyumi.net/entry/2017/10/11/134948

投稿2018/10/04 12:00

kiichi54321

総合スコア1984

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問