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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

5092閲覧

3Dモードでのline描画について

sakuramoti

総合スコア20

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

0クリップ

投稿2018/08/19 13:20

編集2021/03/04 14:24

###実現したいこと 
Unity3dでマウスをドラッグした時にその軌道上に線を描画するということをしたいです。

###試したこと
linerendererを用いてcanvas上に描いてみました。

###該当のソースコード
canvasのRenderModeはScreenSpace-cameraでRenderCameraにMainCameraをアタッチしています。
PlaneDestanceは5です。(手前に表示したいため)
MainCameraのTransformは
Position(0, 5, -10);
Rotation(20, 0, 0);
Scale(0, 0, 0);
ProjectionはPerspective
Field of View が 60です。

以下がソースコードです。

lang

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class lineController : MonoBehaviour { 6 private LineRenderer lineRenderer; 7 private int index; 8 private Camera mainCamera; 9 // Use this for initialization 10 void Start () { 11 lineRenderer = GetComponent<LineRenderer> (); 12 index = 0; 13 mainCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera> (); 14 } 15 16 // Updateis called once per frame 17 void Update () { 18//ボタンを押した時最初の点を決める。 19 if (Input.GetMouseButtonDown (0)) { 20 var pos = Input.mousePosition; 21 pos = mainCamera.ScreenToWorldPoint(Input.mousePosition + mainCamera.transform.forward*10); 22//調整のためcanvasのscaleのそれぞれの座標で割ったり、canvasのポジションを引いたりしている。(うまくいかない) 23 pos.x = (float)(pos.x / 0.00641500); 24 pos.y = (float)((pos.y-3.289912f) / 0.00641500); 25 pos.z = 0.0f; 26 index++; 27 lineRenderer.positionCount = index; 28 lineRenderer.SetPosition (index - 1, pos); 29 } 30//ボタンを押している間線を引く。 31 if (Input.GetMouseButton (0)) { 32 var posSecond = Input.mousePosition; 33 posSecond = mainCamera.ScreenToWorldPoint (posSecond + mainCamera.transform.forward*10); 34//調整のためcanvasのscaleのそれぞれの座標で割ったり、canvasのポジションを引いたりしている。(うまくいかない) 35 posSecond.x = (float)(posSecond.x / 0.00641500); 36 posSecond.y = (float)((posSecond.y-3.289912f) / 0.00641500); 37 posSecond.z = 0.0f; 38 index++; 39 lineRenderer.positionCount = index; 40 lineRenderer.SetPosition (index - 1, posSecond); 41 } 42 if (!(Input.GetMouseButton (0))) { 43 index = 0; 44 } 45 } 46}

###問題点
マウスの軌道上にぴったり合わせることができません。

ぴったり合わせる方法を教えていただけないでしょうか?(足りない情報があれば申し訳ありませんがが教えてくださいませ。追記いたします。)

###追記
大変申し訳ありません。こちらの情報不足でした。
マウスの座標に線を引くことができました! しかしカメラが動くとlineのz座標が変ってしまいます。
これをうまく固定して表示することはできないでしょうか。

以下がカメラにアタッチしているscriptです。

lang

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class CameraController : MonoBehaviour { 6 public GameObject unityChan; 7 // Use this for initialization 8 void Start () { 9 unityChan = GameObject.FindGameObjectWithTag ("Player"); 10 transform.position = new Vector3(unityChan.transform.position.x, unityChan.transform.position.y + 5, unityChan.transform.position.z - 7); 11 12 } 13 14 // Update is called once per frame 15//カメラがキャラクターを追従する 16 void Update () { 17 transform.position = new Vector3(unityChan.transform.position.x, unityChan.transform.position.y + 5, unityChan.transform.position.z - 7); 18 } 19}

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

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

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

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

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

guest

回答1

0

ベストアンサー

ScreenToWorldPointに与える座標が不適切なのではないかと思います。
下記のようにしてみてはいかがでしょうか?

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class lineController : MonoBehaviour 6{ 7 private LineRenderer lineRenderer; 8 private int index; 9 private Camera mainCamera; 10 // Use this for initialization 11 void Start() 12 { 13 lineRenderer = GetComponent<LineRenderer>(); 14 index = 0; 15 mainCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>(); 16 } 17 18 // Updateis called once per frame 19 void Update() 20 { 21 //ボタンを押した時最初の点を決める。 22 if (Input.GetMouseButtonDown(0)) 23 { 24 // ScreenToWorldPointが引数として求めるのは「X、Yにスクリーン座標、Zにカメラからの距離」なので 25 // 「マウスのスクリーン座標」+「ワールド座標系でのカメラforward×10」を与えるのは奇妙だと思います 26 27 // こうではなく... 28 /* 29 var pos = Input.mousePosition; 30 pos = mainCamera.ScreenToWorldPoint(Input.mousePosition + mainCamera.transform.forward*10); 31 */ 32 33 // これでどうでしょうか? 34 var pos = Input.mousePosition; 35 pos.z = 10; 36 pos = mainCamera.ScreenToWorldPoint(pos); 37 38 // あるいは、こう書いてもよさそうです 39 // var pos = mainCamera.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10); 40 41 // LineRendererのポジションはuseWorldSpaceがtrueの場合ワールド座標系で指定するので、たとえLineRendererをアタッチしたオブジェクトが 42 // Canvas階層下に入っていたとしても、Canvasの位置・スケールによるポジション調整は不要なはず 43 /* 44 //調整のためcanvasのscaleのそれぞれの座標で割ったり、canvasのポジションを引いたりしている。(うまくいかない) 45 pos.x = (float)(pos.x / 0.00641500); 46 pos.y = (float)((pos.y - 3.289912f) / 0.00641500); 47 pos.z = 0.0f; 48 */ 49 50 index++; 51 lineRenderer.positionCount = index; 52 lineRenderer.SetPosition(index - 1, pos); 53 } 54 //ボタンを押している間線を引く。 55 if (Input.GetMouseButton(0)) 56 { 57 // こちらも上のパートと同様に書き換えました 58 // GetMouseButtonDownのパートもGetMouseButtonのパートもやっていることに違いはなく、 59 // GetMouseButtonDownのパートは丸ごと削除してしまっても問題ないかもしれませんね 60 61 /* 62 var posSecond = Input.mousePosition; 63 posSecond = mainCamera.ScreenToWorldPoint (posSecond + mainCamera.transform.forward*10); 64 */ 65 66 var posSecond = Input.mousePosition; 67 posSecond.z = 10.0f; 68 posSecond = mainCamera.ScreenToWorldPoint(posSecond); 69 70 /* 71 //調整のためcanvasのscaleのそれぞれの座標で割ったり、canvasのポジションを引いたりしている。(うまくいかない) 72 posSecond.x = (float)(posSecond.x / 0.00641500); 73 posSecond.y = (float)((posSecond.y - 3.289912f) / 0.00641500); 74 posSecond.z = 0.0f; 75 */ 76 77 index++; 78 lineRenderer.positionCount = index; 79 lineRenderer.SetPosition(index - 1, posSecond); 80 } 81 if (!(Input.GetMouseButton(0))) 82 { 83 index = 0; 84 } 85 } 86}

こうするとラインとUIの位置関係は下図のようになり、カメラから5mの位置にUIが、10mの位置にラインが描画されるかと思います。

Scene

#コメントを受けて追記

それでは、このlineControllerをアタッチしているオブジェクトを、カメラと連動して動くようにした上で、useWorldSpacefalseにしてみてはいかがでしょう。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class lineController : MonoBehaviour 6{ 7 private LineRenderer lineRenderer; 8 private int index; 9 private Camera mainCamera; 10 // Use this for initialization 11 void Start() 12 { 13 lineRenderer = GetComponent<LineRenderer>(); 14 15 // ラインの座標指定を、このラインオブジェクトのローカル座標系を基準にするよう設定を変更 16 // この状態でラインオブジェクトを移動・回転させると、描かれたラインもワールド空間に 17 // 取り残されることなく、一緒に移動・回転するはず 18 lineRenderer.useWorldSpace = false; 19 20 index = 0; 21 mainCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>(); 22 } 23 24 // Updateis called once per frame 25 void Update() 26 { 27 // このラインオブジェクトを、位置はカメラ前方10m、回転はカメラと同じになるようキープさせる 28 // スクリプトによる追従ではなく、ラインオブジェクトをカメラの子オブジェクトにする方式でもいいと思います 29 // その方がオブジェクトの位置をシーンビュー上で視覚的に調整できて好都合かもしれませんね 30 transform.position = mainCamera.transform.position + mainCamera.transform.forward * 10; 31 transform.rotation = mainCamera.transform.rotation; 32 33 // 簡略化して、「ボタンを押している間線を引く」パートのみにしました 34 //ボタンを押している間線を引く。 35 if (Input.GetMouseButton(0)) 36 { 37 // 座標指定の設定をローカル座標系にしたため、与える座標にも手を加える 38 // まず先ほどと同じようにマウススクリーン座標をワールド座標に直し... 39 var pos = Input.mousePosition; 40 pos.z = 10.0f; 41 pos = mainCamera.ScreenToWorldPoint(pos); 42 43 // さらにそれをラインオブジェクトにおけるローカル座標に直し... 44 pos = transform.InverseTransformPoint(pos); 45 46 // 得られたローカル座標をラインレンダラーに追加する 47 index++; 48 lineRenderer.positionCount = index; 49 lineRenderer.SetPosition(index - 1, pos); 50 } 51 if (!(Input.GetMouseButton(0))) 52 { 53 index = 0; 54 } 55 } 56}

これで動かしてみたところ、マウスで線を描いた後にカメラを回転させても、画面内の描いた線の位置は一定になりました。

Scene

投稿2018/08/19 21:24

編集2018/08/20 09:29
Bongo

総合スコア10807

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

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

sakuramoti

2018/08/19 22:31

Bongo様 回答大変ありがとうございます。 マウスの軌道上にラインを表示することができました! しかしこちらの記載不足でやりたかったことがうまく伝えられていませんでした。 その情報追記いたしました。 もしよろしければご回答いただけると助かります。
Bongo

2018/08/20 09:30

お返事遅れてすみません、画面内の線の位置を固定する案を検討してみました。ご参考になりますでしょうか?
sakuramoti

2018/08/20 10:56

はい! やりたかったことができました! 大変ありがとうございます。 以前にもご回答していただいたことがあったのですが、 丁寧にコメント付きでとても参考になりました。 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問