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

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

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

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

Unity

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

Q&A

1回答

1024閲覧

Unity 2Dゲームの流れてくる障害物がプレイヤーに押されてしまう

0623_top

総合スコア8

C#

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

Unity

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

0グッド

0クリップ

投稿2020/01/09 01:19

編集2020/01/09 02:57

問題点

イメージ説明
上の画像のように、流れてきた障害物がプレイヤーに当たるとプレイヤーの速度に押されて障害物の速度が遅くなってしまいます。

動画
↑問題となっている現状の動画になります(解答欄で現状がわかりづらいとご指摘いただきました。追記です。)

現状のワークスペース(参考までに)

イメージ説明
現状このような感じですが、どこが間違っているかがわからないため、とりあえずこの画像を貼らせていただきますが、もしどの辺が怪しい!などがありましたら、細かい画像などを追加致しますので、ご教示いただければ幸いです。

スクリプトファイル

全て載せきれないので、怪しそうなファイルをいくつか載せようと思います。

Playerファイル

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 [SerializeField] 14 int itemScore = 500; 15 //インスペクタには表示しない変数 16 bool jumped = false; 17 bool grounded = false; 18 bool groundedPrev = false; 19 bool inCamera = false; 20 Animator animator; 21 Rigidbody2D rigidBody2D; 22 GameManager gameManager; 23 AudioSource[] audioSources; 24 //初期化 25 void Start() 26 { 27 //コンポーネントを取得 28 animator = GetComponent<Animator>(); 29 rigidBody2D = GetComponent<Rigidbody2D>(); 30 audioSources = GetComponents<AudioSource>(); 31 //GameManagerを検索し、コンポーネントを取得 32 gameManager = GameObject.Find("GameManager").GetComponent<GameManager>(); 33 } 34 //毎フレームの処理(一般) 35 void Update() 36 { 37 //接地チェック 38 //GroundCheckオブジェクトに床などが重なってるならtrue 39 grounded = (Physics2D.OverlapPoint(groundCheck.position) != null) ? true : false; 40 //接地してるかどうかの分岐 41 if (grounded) 42 { 43 //接地しているならジャンプを許可 44 if (Input.GetMouseButtonDown(0)) 45 { 46 Jump(); 47 } 48 } 49 //ジャンプしてるかどうかの分岐 50 if (jumped) 51 { 52 animator.SetTrigger("Jump"); 53 //ジャンプ終了(前フレームで接地してなくて、今のフレームで接地したとき) 54 if (!groundedPrev & grounded) 55 { 56 jumped = false; 57 } 58 } 59 else 60 { 61 animator.SetTrigger("Dash"); 62 } 63 //このフレームの接地状態を保存 64 groundedPrev = grounded ? true : false; 65 } 66 //毎フレームの処理(物理演算) 67 void FixedUpdate() 68 { 69 if (grounded) 70 { 71 rigidBody2D.velocity = new Vector2(speed, rigidBody2D.velocity.y); 72 } 73 } 74 //ジャンプ 75 void Jump() 76 { 77 jumped = true; 78 rigidBody2D.velocity = Vector2.up * jumpPower; 79 //ジャンプ音 80 audioSources[0].PlayOneShot(audioSources[0].clip); 81 } 82 //死亡処理 83 void Dead() 84 { 85 gameManager.GameOver(); 86 rigidBody2D.Sleep(); 87 Destroy(gameObject); 88 } 89 //アイテムと衝突した時の処理 90 void OnCollisionEnter2D(Collision2D other) 91 { 92 if (other.gameObject.tag == "Item") 93 { 94 //スコア加算 95 gameManager.score += itemScore; 96 97 //取ったアイテムを消す 98 Destroy(other.gameObject); 99 //コイン取得音 100 audioSources[1].PlayOneShot(audioSources[1].clip); 101 } 102 } 103 //カメラ内外の判定処理 104 void OnBecameVisible() 105 { 106 inCamera = true; 107 } 108 void OnBecameInvisible() 109 { 110 if (inCamera) 111 { 112 Dead(); 113 } 114 } 115}

Wallファイル

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}

Emitterファイル

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class Emitter : MonoBehaviour 5{ 6 [SerializeField] 7 float emitWaitTimeMin = 0.6f; 8 [SerializeField] 9 float emitWaitTimeMax = 1.0f; 10 [SerializeField] 11 float intervalCofficient = 0.98f; //レベルアップしたとき、生成間隔を何倍するかの係数 12 [SerializeField] 13 float wallSpawnX = 13.0f; 14 [SerializeField] 15 float wallSpawnY = -2.5f; 16 [SerializeField] 17 float coinSpawnX = 11.0f; 18 [SerializeField] 19 float coinSpawnY = 5.0f; 20 [SerializeField] 21 GameObject wall; 22 [SerializeField] 23 GameObject coin; 24 [SerializeField] 25 GameManager gameManager; 26 bool emitterEnabled = true; 27 int cullentLevel; 28 float emitWaitTimeMinNow; 29 float emitWaitTimeMaxNow; 30 //毎フレームの処理 31 void Update() 32 { 33 //タイトルまたはゲームオーバー画面 34 if (!gameManager.playing) 35 { 36 //初期化 37 cullentLevel = gameManager.level; 38 emitWaitTimeMinNow = emitWaitTimeMin; 39 emitWaitTimeMaxNow = emitWaitTimeMax; 40 } 41 //プレイ中の処理 42 if (emitterEnabled && gameManager.playing) 43 { 44 //生成処理 45 Instantiate(wall, new Vector3(wallSpawnX, wallSpawnY, transform.position.z), Quaternion.identity); 46 Instantiate(coin, new Vector3(coinSpawnX, coinSpawnY, transform.position.z), Quaternion.identity); 47 StartCoroutine(EmitWait()); 48 //レベルアップ時に生成速度Up 49 if (gameManager.level != cullentLevel) 50 { 51 emitWaitTimeMinNow *= intervalCofficient; 52 emitWaitTimeMaxNow *= intervalCofficient; 53 cullentLevel = gameManager.level; 54 } 55 } 56 } 57 //生成を待つ処理 58 IEnumerator EmitWait() 59 { 60 emitterEnabled = false; 61 float emitWaitTime = Random.Range(emitWaitTimeMinNow, emitWaitTimeMaxNow); 62 yield return new WaitForSeconds(emitWaitTime); 63 emitterEnabled = true; 64 } 65}

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

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

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

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

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

guest

回答1

0

このように速度が遅くなる、といわれても、静止画では伝わりませんよ

RigidBodyを持ってしまうと、物理挙動を持つオブジェクトとなってしまうため
プレイヤーによって押されてしまいます

・障害物の質量を大きくする
・RigidBodyをつけない

の方法があると思います

投稿2020/01/09 02:47

izmktr

総合スコア2856

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

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

0623_top

2020/01/09 03:01

アドバイスありがとうございます。 RigidBody周りと、障害物の質量の部分を色々いじってみます。 また、ご指摘いただきました、静止画では伝わりにくいというところですが、新たに動画リンクを貼らせていただきましたので、一度見ていただけるとありがたく思います。 未熟者ですが、よろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問