###実現したいこと
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}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/19 22:31
2018/08/20 09:30
2018/08/20 10:56