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

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

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

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

Q&A

解決済

1回答

684閲覧

障害物が動いてくれない・・

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2019/08/10 00:37

私は今このサイトのように悪路王という2Dゲームを作っています。
ですが途中で実行したら障害物(スクショでいうWallです)が流れてきてくれませんでした。(説明が難しいので・・詳しくは上の悪路王というところをクリックしてください。)
スクリプトもインスペクターの値なども多分間違えているところはないと思います。一応、全てのスクリーンショットを載せておきます。

イメージ説明

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class Player : MonoBehaviour 5{ 6 //インスペクタに表示する変数 7 [SerializeField] 8 float speed = 0.0f; 9 [SerializeField] 10 float jumpPower = 30.0f; 11 [SerializeField] 12 Transform groundCheck; 13 //インスペクタには表示しない変数 14 bool jumped = false; 15 bool grounded = false; 16 bool groundedPrev = false; 17 Animator animator; 18 Rigidbody2D rigidBody2D; 19 //初期化 20 void Start() 21 { 22 //コンポーネントを取得 23 animator = GetComponent<Animator>(); 24 rigidBody2D = GetComponent<Rigidbody2D>(); 25 } 26 //毎フレームの処理(一般) 27 void Update() 28 { 29 //接地チェック 30 //GroundCheckオブジェクトに床などが重なってるならtrue 31 grounded = (Physics2D.OverlapPoint(groundCheck.position) != null) ? true : false; 32 //接地してるかどうかの分岐 33 if (grounded) 34 { 35 //接地しているならジャンプを許可 36 if (Input.GetMouseButtonDown(0)) 37 { 38 Jump(); 39 } 40 } 41 //ジャンプしてるかどうかの分岐 42 if (jumped) 43 { 44 animator.SetTrigger("Jump"); 45 //ジャンプ終了(前フレームで接地してなくて、今のフレームで接地したとき) 46 if (!groundedPrev & grounded) 47 { 48 jumped = false; 49 } 50 } 51 else 52 { 53 animator.SetTrigger("Dash"); 54 } 55 //このフレームの接地状態を保存 56 groundedPrev = grounded ? true : false; 57 } 58 //毎フレームの処理(物理演算) 59 void FixedUpdate() 60 { 61 if (grounded) 62 { 63 rigidBody2D.velocity = new Vector2(speed, rigidBody2D.velocity.y); 64 } 65 } 66 //ジャンプ 67 void Jump() 68 { 69 jumped = true; 70 rigidBody2D.velocity = Vector2.up * jumpPower; 71 } 72}

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5public class GameManager : MonoBehaviour 6{ 7 //インスペクタに表示しないpublic変数 8 [System.NonSerialized] 9 public int level = 1; 10 [System.NonSerialized] 11 public int score = 0; 12 [System.NonSerialized] 13 public bool playing = false; 14 //インスペクタに表示するprivate変数 15 [SerializeField] 16 GameObject player; 17 [SerializeField] 18 int levelUpTime = 6; //レベルアップするまでの時間 19 [SerializeField] 20 int maxLevel = 10; 21 [SerializeField] 22 float addScoreInterval = 6; //スコア加算間隔 23 [SerializeField] 24 int addScoreCofficient = 1; //スコア加算係数 25 [SerializeField] 26 float playerSpawnX = -7.0f; 27 [SerializeField] 28 float playerSpawnY = 3.0f; 29 //インスペクタに表示しない変数 30 int highScore = 0; 31 string highScoreKey = "highScore"; 32 //UI取得用 33 [SerializeField] 34 Image titleLogoImg; 35 [SerializeField] 36 Text clickStartTxt; 37 [SerializeField] 38 Text gameOverTxt; 39 [SerializeField] 40 Text tryAgainTxt; 41 [SerializeField] 42 Text scoreLabelTxt; 43 [SerializeField] 44 Text scoreNumTxt; 45 [SerializeField] 46 Text highLabelTxt; 47 [SerializeField] 48 Text highNumTxt; 49 //初期化 50 void Start() 51 { 52 53 } 54 55 //毎フレームの処理 56 void Update() 57 { 58 //ゲーム中の処理 59 if (playing) 60 { 61 if (highScore < score) 62 { 63 highScore = score; 64 } 65 //スコアを表示 66 scoreNumTxt.text = score.ToString("d6"); 67 highNumTxt.text = highScore.ToString("d6"); 68 } 69 //タイトルまたはゲームオーバー 70 else 71 { 72 //クリックされたらゲーム開始 73 if (Input.GetMouseButtonDown(0)) 74 { 75 StartGame(); 76 } 77 } 78 } 79 //ゲーム開始処理 80 void StartGame() 81 { 82 //ガベージコレクション実行 83 System.GC.Collect(); 84 //初期化 85 score = 0; 86 level = 1; 87 //ハイスコア読み込み 88 highScore = PlayerPrefs.GetInt(highScoreKey, 0); 89 //タイトルまたはゲームオーバー画面非表示 90 titleLogoImg.enabled = false; 91 clickStartTxt.enabled = false; 92 gameOverTxt.enabled = false; 93 tryAgainTxt.enabled = false; 94 //スコアを表示 95 scoreLabelTxt.enabled = true; 96 scoreNumTxt.enabled = true; 97 highLabelTxt.enabled = true; 98 highNumTxt.enabled = true; 99 playing = true; 100 //もし地形やコインが残ってたら消去 101 DeleteObjects("Wall"); 102 DeleteObjects("Item"); 103 //プレイヤー生成 104 Instantiate(player, new Vector3(playerSpawnX, playerSpawnY, 0.0f), Quaternion.identity); 105 //ゲーム開始後、一定時間ごとの処理 106 InvokeRepeating("LevelUp", levelUpTime, levelUpTime); 107 InvokeRepeating("AddScoreByTime", addScoreInterval, addScoreInterval); 108 } 109 //ゲームオーバー処理 110 public void GameOver() 111 { 112 CancelInvoke(); 113 playing = false; 114 // ハイスコアを保存 115 PlayerPrefs.SetInt(highScoreKey, highScore); 116 PlayerPrefs.Save(); 117 //ゲームオーバー画面表示 118 gameOverTxt.enabled = true; 119 tryAgainTxt.enabled = true; 120 } 121 //スコア加算 122 void AddScoreByTime() 123 { 124 score += addScoreCofficient * (10 + level - 1); 125 Debug.Log(score); 126 } 127 //レベルアップ 128 void LevelUp() 129 { 130 if (level < maxLevel) 131 { 132 level++; 133 Debug.Log(level); 134 } 135 } 136 //ゲームオブジェクトの一括削除 137 void DeleteObjects(string tag) 138 { 139 GameObject[] deleteObjects = GameObject.FindGameObjectsWithTag(tag); 140 if (deleteObjects.Length > 0) 141 { 142 foreach (GameObject obj in deleteObjects) 143 { 144 Destroy(obj); 145 } 146 } 147 } 148} 149

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class Wall : MonoBehaviour 5{ 6 [SerializeField] 7 float speed = 10.0f; 8 [SerializeField] 9 float minScaleX = 4.0f; 10 [SerializeField] 11 float minScaleY = 3.0f; 12 [SerializeField] 13 float maxScaleX = 6.0f; 14 [SerializeField] 15 float maxScaleY = 7.0f; 16 bool inCamera = false; 17 Rigidbody2D rigidBody2D; 18 //初期化 19 void Start() 20 { 21 //コンポーネントを取得 22 rigidBody2D = GetComponent<Rigidbody2D>(); 23 //壁の長さをランダムに変更 24 float scaleX = Random.Range(minScaleX, maxScaleX); 25 float scaleY = Random.Range(minScaleY, maxScaleY); 26 transform.localScale = new Vector3(scaleX, scaleY, transform.localScale.z); 27 } 28 //物理演算 29 void FixedUpdate() 30 { 31 rigidBody2D.velocity = Vector2.left * speed; 32 } 33 //消去処理 34 public void DestroyBlock() 35 { 36 Destroy(gameObject); 37 } 38 //カメラ内外の判定処理 39 void OnBecameVisible() 40 { 41 inCamera = true; 42 } 43 void OnBecameInvisible() 44 { 45 //一度画面内に入って、また出たときに消去する 46 if (inCamera) 47 { 48 DestroyBlock(); 49 } 50 } 51}

このコードも間違えてないと思うますが・・ここは違うんじゃないか?と思う方はご教授お願いします。
また、ほかのスクリーンショットが必要な場合はお知らせください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

単純にWallプレハブにWallスクリプトが割り付けられていないからですね。
今のWallプレハブはただの絵を表示するだけのオブジェクトなだけで、動作を指示するスクリプトが割り付けられていなければ、当然Wallオブジェクトも動きません。

投稿2019/08/10 01:30

vo3

総合スコア321

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

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

退会済みユーザー

退会済みユーザー

2019/08/10 01:35

あっ・・・そうですね。ありがとうございます!私の確認ミスでした!ありがとうございました(^^)/~~~
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問