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

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

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

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

Unity

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

Q&A

解決済

1回答

733閲覧

【Unity】子のオブジェクトにOnDrawGizmosで一括でギズモを表示したい

Y0241-N

総合スコア1066

C#

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

Unity

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

0グッド

0クリップ

投稿2019/09/19 03:01

実現したいこと

OnDrawGizmosメソッドでDrawRayを使ってギズモを表示することはできたのですが、
現状はこのスクリプトがアタッチされているオブジェクトにギズモが表示される状態なので、

これを親オブジェクトにスクリプトをアタッチし、子のオブジェクトにはスクリプトを
アタッチしなくてもギズモが表示されるようにしたいと思っています。

しかし、どのようにすれば子にギズモを表示する命令を渡すことが出来るのかが
分からなかったため、質問させていただきました。

該当のソースコード

ギズモ表示と子のオブジェクトの情報取得&変更スクリプト

cs

1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5public class GizomLine : MonoBehaviour 6{ 7 8#if UNITY_EDITOR 9 void Reset() 10 { 11 Update_Path(); 12 } 13#endif 14 15 void Awake() 16 { 17 Update_Path(); 18 } 19 20 public void Update_Path() 21 { 22 Transform[] children = new Transform[transform.childCount]; 23 Vector3[] points = new Vector3[children.Length]; 24 Vector3[] ups = new Vector3[children.Length]; 25 26 for(int i=0; i< transform.childCount; i++){ 27 children[i] = transform.GetChild(i); 28 children[i].gameObject.name = "point " + i; 29 30 points[i] = children[i].localPosition; 31 ups[i] = transform.InverseTransformDirection(children[i].up); 32 } 33 } 34 35 void OnDrawGizmos() 36 { 37 Gizmos.color = new Color(0, 1, 1, 1); 38 Vector3 direction = transform.TransformDirection(Vector3.forward) * 0.5f; 39 Gizmos.DrawRay(transform.position, direction); 40 41 Gizmos.color = Color.green; 42 Vector3 direction2 = transform.TransformDirection(Vector3.up) * 0.5f; 43 Gizmos.DrawRay(transform.position, direction2); 44 45 Gizmos.color = Color.red; 46 Vector3 direction3 = transform.TransformDirection(Vector3.right) * 0.5f; 47 Gizmos.DrawRay(transform.position, direction3); 48 } 49} 50

試したこと

GetChildで取得した子に対してメソッドを適用する方法を調べたのですが、
参考になるような情報が得られませんでした。

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

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

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

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

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

guest

回答1

0

ベストアンサー

「子オブジェクトのスクリプトのOnDrawGizmosで描画処理する」という事に固執し過ぎではないですか。
単に「親オブジェクトのOnDrawGizmosで子オブジェクトの位置に描画する」ようにすればいいと思います。

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class GizomLine : MonoBehaviour 5{ 6 void OnDrawGizmos() 7 { 8 for (int i = 0; i < transform.childCount; ++i) { 9 Transform child = transform.GetChild(i); 10 11 Gizmos.color = new Color(0, 1, 1, 1); 12 Vector3 direction = child.TransformDirection(Vector3.forward) * 0.5f; 13 Gizmos.DrawRay(child.position, direction); 14 15 Gizmos.color = Color.green; 16 Vector3 direction2 = child.TransformDirection(Vector3.up) * 0.5f; 17 Gizmos.DrawRay(child.position, direction2); 18 19 Gizmos.color = Color.red; 20 Vector3 direction3 = child.TransformDirection(Vector3.right) * 0.5f; 21 Gizmos.DrawRay(child.position, direction3); 22 } 23 } 24}

投稿2019/09/19 11:08

katsuko

総合スコア3469

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問