回答編集履歴

1

追記

2017/12/25 23:41

投稿

fiveHundred
fiveHundred

スコア9803

test CHANGED
@@ -3,3 +3,89 @@
3
3
  (このソースの場合、verticesはif文の中でのみ有効です。もし、外部で使用したい場合はif文の外で定義する必要があります)
4
4
 
5
5
  verticesにすべきところをmouseposにしているからではないでしょうか?
6
+
7
+
8
+
9
+ ---
10
+
11
+
12
+
13
+ コメントを受けて追記:
14
+
15
+
16
+
17
+ あなたと私でこのスクリプトをアタッチしているゲームオブジェクトが違っていたみたいです。
18
+
19
+ 私は(0, 0, 0)の位置に空のゲームオブジェクトを配置して、それにアタッチしていました。
20
+
21
+
22
+
23
+ 以下のようにすることで、アタッチしたオブジェクトに関わらず、私が意図した通りの位置に配置できるようになります。
24
+
25
+
26
+
27
+ ```C#
28
+
29
+ GameObject meshGameObj = null;
30
+
31
+
32
+
33
+ void Update()
34
+
35
+ {
36
+
37
+ mousePressed();
38
+
39
+ if (hitNum > 3 && meshGameObj == null)
40
+
41
+ {//4点以上クリックしていたら
42
+
43
+ Vector3[] vertices = new Vector3[4];
44
+
45
+ for (int i = 0; i < 4; i++)
46
+
47
+ {
48
+
49
+ vertices[i] = Camera.main.ScreenToWorldPoint(mousepos[i]);//頂点座標を絶対座標に変換した
50
+
51
+ }
52
+
53
+
54
+
55
+ int[] triangles = { 0, 1, 2, 0, 2, 3 };
56
+
57
+ Mesh mesh = new Mesh();
58
+
59
+ mesh.vertices = vertices;
60
+
61
+ mesh.triangles = triangles;
62
+
63
+ mesh.RecalculateNormals();
64
+
65
+ meshGameObj = new GameObject();
66
+
67
+ meshGameObj.transform.position = Vector3.zero;
68
+
69
+ meshGameObj.transform.rotation = Quaternion.identity;
70
+
71
+ MeshFilter meshFilter = meshGameObj.GetComponent<MeshFilter>();
72
+
73
+ if (!meshFilter)
74
+
75
+ meshFilter = meshGameObj.AddComponent<MeshFilter>();
76
+
77
+ MeshRenderer meshRenderer = meshGameObj.GetComponent<MeshRenderer>();
78
+
79
+ if (!meshRenderer)
80
+
81
+ meshRenderer = meshGameObj.AddComponent<MeshRenderer>();
82
+
83
+ meshFilter.mesh = mesh;
84
+
85
+ meshRenderer.sharedMaterial = material;
86
+
87
+ }
88
+
89
+ }
90
+
91
+ ```