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

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

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

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

Q&A

解決済

1回答

4435閲覧

Unity2D オブジェクト同士の当たり判定を別のオブジェクトのスクリプトでも取得したい

creatorsGame

総合スコア18

Unity

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

0グッド

0クリップ

投稿2019/12/10 11:58

今、Agar.io×スプラトゥーンみたいな2Dゲームを開発しています。ゲームを開始するとランダムにプレイヤーの色が変更され、変更されたプレイヤーの色と同じ色に弾の色を変更し、プレイヤーが発射した弾が2Dサークルコライダーを離れると弾が消去され、消去された座標に同じ色のタイルが生成されるプログラムを書きました。
次は、弾が2Dサークルコライダーを離れてもタイルのコライダーに接触していれば消去されないようにしたいです。自分でコードを書いてみたのですが、上手くできません。なので、タイルと弾のコライダーの当たり判定をMoveスクリプトで取得するか、その他で出来る方法があれば教えてください。 
長文ですみません。  
イメージ説明イメージ説明

Move

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Move : MonoBehaviour 6{ 7 public GameObject Bullet; 8 public GameObject Tile; 9 public float Speed; 10 public float ShotSpeed; 11 private ColorRandom colorComp; 12 13 //追加 14 void Start() 15 { 16 //MoveとColorRandomは同じオブジェクトに付いていることを前提としています 17 colorComp = GetComponent<ColorRandom>(); 18 } 19 20 void Update() 21 { 22 //マウスの位置に移動 23 Vector3 Target = Camera.main.ScreenToWorldPoint(Input.mousePosition); 24 Target.z = transform.position.z; 25 26 transform.position = Vector2.MoveTowards(transform.position, Target, Speed * Time.deltaTime); 27 28 //マウスの方向を向く 29 var pos = Camera.main.WorldToScreenPoint(transform.localPosition); 30 var rotation = Quaternion.LookRotation(Vector3.forward, Input.mousePosition - pos); 31 transform.localRotation = rotation; 32 33 34 if (Input.GetMouseButtonDown(0)) 35 { 36 // 弾(ゲームオブジェクト)の生成 37 GameObject clone = Instantiate(Bullet, transform.position, Quaternion.identity); 38 39 // 色反映 40 clone.GetComponent<Renderer>().material.color = colorComp.playerColor; 41 42 // クリックした座標の取得(スクリーン座標からワールド座標に変換) 43 Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 44 45 // 向きの生成(Z成分の除去と正規化) 46 Vector3 shotForward = Vector3.Scale((mouseWorldPos - transform.position), new Vector3(1, 1, 0)).normalized; 47 48 // 弾に速度を与える 49 clone.GetComponent<Rigidbody2D>().velocity = shotForward * ShotSpeed; 50 } 51 } 52 //弾(ball)がColliderから離れた時の処理 53 void OnTriggerExit2D(Collider2D col) 54 { 55 56 if (col.gameObject.tag == "PaintBullet") 57 { 58 59 GameObject[] Bullets = GameObject.FindGameObjectsWithTag("PaintBullet"); 60 foreach (GameObject Bullet in Bullets) 61 { 62 GameObject Tileclone = Instantiate(Tile, Bullet.transform.position, Quaternion.identity); 63 Tileclone.GetComponent<Renderer>().material.color = colorComp.playerColor; 64 Destroy(Bullet); 65 } 66 67 } 68 } 69}

ColorRandom

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ColorRandom : MonoBehaviour 6{ 7 private Renderer rend; 8 public Color playerColor; //追加 9 10 private void Start() 11 { 12 rend = GetComponent<Renderer>(); 13 playerColor = new Color(Random.value, Random.value, Random.value, 1.0f); 14 rend.material.color = playerColor; 15 16 } 17} 18

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

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

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

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

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

guest

回答1

0

ベストアンサー

消去条件が良くわかりませんが、Tileにtagを設定しておいて
PaintBalletと同じようにしたら良いのでは?

C#

1 2void OnTriggerExit2D(Collider2D col) 3{ 4 //省略 5 6 if (col.gameObject.tag == "Tile") 7 { 8 // 消さない 9 10 } 11}

投稿2019/12/11 00:42

madone99

総合スコア1855

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

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

creatorsGame

2019/12/12 13:23 編集

発射する弾に新規作成スクリプト(PaintBullets)を追加して、弾の方で当たり判定をするようにしました。 ですが、プレイヤーの色か弾の色を取得してタイルの色を同じ色にする方法が分かりません。ColorRandomとMoveで同じような処理をしているのですが、別のオブジェクト間ではできません。どうすればよいでしょうか? PaintBullets using System.Collections; using System.Collections.Generic; using UnityEngine; public class PaintBullets : MonoBehaviour { public GameObject Tile; //弾(ball)がColliderから離れた時の処理 void OnTriggerExit2D(Collider2D col) { if (col.gameObject.tag == "Player") { GameObject Tileclone = Instantiate(Tile, this.transform.position, Quaternion.identity); Destroy(this.gameObject); } } }
madone99

2019/12/13 00:49

シングルトンにするとか、それぞれの取得方法はおまかせしますが Playerと弾にcurrentColorのプロパティを作り、衝突時にお互いをチェックして色を変更。 ↓ タイルは、当たった弾のcurrentColorを取得する という考えでいいのではないでしょうか。
creatorsGame

2019/12/13 07:52

すみません。ColorRandomのPlayerColorを取得したいのですが、//←の所は同じオブジェクトに追加していないとできません。具体的にどう変えれば良いでしょうか? using System.Collections; using System.Collections.Generic; using UnityEngine; public class PaintBullets : MonoBehaviour { public GameObject Tile; public ColorRandom currentColor; //弾(ball)がColliderから離れた時の処理 void OnTriggerExit2D(Collider2D col) { if (col.gameObject.tag == "Player") { currentColor = GetComponent<ColorRandom>(); //← GameObject Tileclone = Instantiate(Tile, this.transform.position, Quaternion.identity); Tileclone.GetComponent<Renderer>().material.color = currentColor.playerColor; Destroy(this.gameObject); } } }
madone99

2019/12/13 08:08 編集

ColorRadomにtagを設定して currentColor = GameObject.FindWithTag("ColorRandom"); で取得するとか、色々考えられます!
creatorsGame

2019/12/13 13:00

ありがとうございます。下のようにしてできました。後はタイルに当たっていたらDelete()を実行しない、もしくは、タイルに当たっていなかったらDelete()を実行するというようにしたいです。自分でやってみたのですが上手くできません。 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PaintBullets : MonoBehaviour { public GameObject Tile; public ColorRandom currentColor; //弾(ball)がColliderから離れた時の処理 void OnTriggerExit2D(Collider2D col) { if (col.gameObject.tag == "Player") { Delete(); } } void Delete() { currentColor = GameObject.FindWithTag("Player").GetComponent<ColorRandom>(); ; GameObject Tileclone = Instantiate(Tile, this.transform.position, Quaternion.identity); Tileclone.GetComponent<Renderer>().material.color = currentColor.playerColor; Destroy(this.gameObject); } }
madone99

2019/12/13 13:27

あとはご自身で考えられた条件をそのままif文にするだけですので 考えてみて下さい。 今あるコードの内容で出来るはずです。
creatorsGame

2019/12/14 01:09

すみません。Paintに接触していなかったらかPlayerに接触していなかったらDelete()したいのですが、Paintに接触していてもDelete()されます。どこを変えればよいのでしょうか? using System.Collections; using System.Collections.Generic; using UnityEngine; public class PaintBullets : MonoBehaviour { public GameObject Tile; public ColorRandom currentColor; private bool Stay; void OnTriggerEnter2d(Collider2D col) { if (col.gameObject.tag == "Paint") { Stay = true; } } //弾(bullet)がColliderから離れた時の処理 void OnTriggerExit2D(Collider2D col) { if (col.gameObject.tag == "Paint") { Stay = false; } if (col.gameObject.tag == "Player") { if (!Stay) { Delete(); } } } void Delete() { currentColor = GameObject.FindWithTag("Player").GetComponent<ColorRandom>(); ; GameObject Tileclone = Instantiate(Tile, this.transform.position, Quaternion.identity); Tileclone.GetComponent<Renderer>().material.color = currentColor.playerColor; Destroy(this.gameObject); } }
creatorsGame

2019/12/14 07:37

できました。ですが、たまに思うように動かなかったりするので、できるだけ自分で調べて分からなかったらまた質問します。ありがとうございました。 修正 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PaintBullets : MonoBehaviour { public GameObject Tile; public ColorRandom currentColor; private bool Stay; void Start() { Stay = true; } //触れるのをやめた時 void OnTriggerExit2D(Collider2D col) { if ((col.gameObject.tag == "Player") || (col.gameObject.tag == "Paint")) { Stay = false; } } //触れている時 void OnTriggerStay2D(Collider2D col) { if (col.gameObject.tag == "Paint") { Stay = true; } } void Update() { if (!Stay) { Delete(); } } void Delete() { currentColor = GameObject.FindWithTag("Player").GetComponent<ColorRandom>(); ; GameObject Tileclone = Instantiate(Tile, this.transform.position, Quaternion.identity); Tileclone.GetComponent<Renderer>().material.color = currentColor.playerColor; Destroy(this.gameObject); } }
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問