質問編集履歴

2

修正

2019/10/13 04:51

投稿

Ruchi1237
Ruchi1237

スコア8

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,367 @@
10
10
 
11
11
  回答のようにScriptを変更してもエラーが起きてしまい自力で解決することができません。どなたかよろしくお願いします。
12
12
 
13
-
13
+ ```c#
14
+
15
+ using UnityEngine;
16
+
17
+ using UnityEngine.UI;
18
+
19
+ using UnityEngine.EventSystems;
20
+
21
+ using System.Collections;
22
+
23
+ using System.Collections.Generic;
24
+
25
+
26
+
27
+ /// <summary>
28
+
29
+ /// ジョイスティック
30
+
31
+ /// </summary>
32
+
33
+ public class Joystick : MonoBehaviour
34
+
35
+ {
36
+
37
+
38
+
39
+ //実際に動くスティック部分
40
+
41
+ [SerializeField]
42
+
43
+ [Header("実際に動くスティック部分(自動設定)")]
44
+
45
+ private GameObject _stick = null;
46
+
47
+ private const string STICK_NAME = "Stick";
48
+
49
+
50
+
51
+ //スティックが動く範囲の半径
52
+
53
+ [SerializeField]
54
+
55
+ [Header("スティックが動く範囲の半径")]
56
+
57
+ private float _radius = 100;
58
+
59
+
60
+
61
+ //指を離した時にスティックが中心に戻るか
62
+
63
+ [SerializeField]
64
+
65
+ [Header("指を離した時にスティックが中心に戻るか")]
66
+
67
+ private bool _shouldResetPosition = true;
68
+
69
+
70
+
71
+ //現在地(x,y共に値が-1~1の範囲になる)
72
+
73
+ [SerializeField]
74
+
75
+ [Header("現在地(自動更新)")]
76
+
77
+ private Vector2 _position = Vector2.zero;
78
+
79
+ public Vector2 Position { get { return _position; } }
80
+
81
+
82
+
83
+ //スティックの位置(Setter)
84
+
85
+ private Vector3 _stickPosition
86
+
87
+ {
88
+
89
+ set
90
+
91
+ {
92
+
93
+ _stick.transform.localPosition = value;
94
+
95
+ _position = new Vector2(
96
+
97
+ _stick.transform.localPosition.x / _radius,
98
+
99
+ _stick.transform.localPosition.y / _radius
100
+
101
+ );
102
+
103
+ }
104
+
105
+ }
106
+
107
+
108
+
109
+ //=================================================================================
110
+
111
+ //初期化
112
+
113
+ //=================================================================================
114
+
115
+
116
+
117
+ protected void Awake()
118
+
119
+ {
120
+
121
+ Awake();
122
+
123
+ Init();
124
+
125
+ }
126
+
127
+
128
+
129
+ //初期化
130
+
131
+ private void Init()
132
+
133
+ {
134
+
135
+ //スティックを生成する必要があれば生成し、位置を中心に設定
136
+
137
+ CreateStickIfneeded();
138
+
139
+ _stickPosition = Vector3.zero;
140
+
141
+
142
+
143
+ //スティックのImage取得(なければ追加)、タッチ判定を取られないようにraycastTargetをfalseに
144
+
145
+ Image stickImage = _stick.GetComponent<Image>();
146
+
147
+ if (stickImage == null)
148
+
149
+ {
150
+
151
+ stickImage = _stick.AddComponent<Image>();
152
+
153
+ }
154
+
155
+ stickImage.raycastTarget = false;
156
+
157
+ }
158
+
159
+
160
+
161
+ //スティックを生成する必要があれば生成
162
+
163
+ private void CreateStickIfneeded()
164
+
165
+ {
166
+
167
+ //スティックが設定されていれば終了
168
+
169
+ if (_stick != null)
170
+
171
+ {
172
+
173
+ return;
174
+
175
+ }
176
+
177
+
178
+
179
+ //スティックが子にあるか検索、あれば取得し終了
180
+
181
+ if (transform.Find(STICK_NAME) != null)
182
+
183
+ {
184
+
185
+ _stick = transform.Find(STICK_NAME).gameObject;
186
+
187
+ return;
188
+
189
+ }
190
+
191
+
192
+
193
+ //スティック生成
194
+
195
+ _stick = new GameObject(STICK_NAME);
196
+
197
+ _stick.transform.SetParent(gameObject.transform);
198
+
199
+ _stick.transform.localRotation = Quaternion.identity;
200
+
201
+ }
202
+
203
+
204
+
205
+ float _radius2 = 150;//ジョイスティックの最大半径
206
+
207
+ public bool OnDrag(Vector2 touch)
208
+
209
+ {
210
+
211
+ //タップ位置を画面内の座標に変換し、スティックを移動
212
+
213
+ Vector2 screenPos = Vector2.zero;
214
+
215
+ //Debug.Log(Input.mousePosition);
216
+
217
+ RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(),
218
+
219
+ new Vector2(Input.mousePosition.x, Input.mousePosition.y),
220
+
221
+ null,
222
+
223
+ out screenPos
224
+
225
+ );
226
+
227
+
228
+
229
+ _stickPosition = screenPos;
230
+
231
+
232
+
233
+ //移動場所が設定した半径を超えてる場合は制限内に抑える
234
+
235
+ float currentRadius = Vector3.Distance(Vector3.zero, _stick.transform.localPosition);
236
+
237
+
238
+
239
+ if (currentRadius > _radius)
240
+
241
+ {
242
+
243
+ return false;
244
+
245
+ }
246
+
247
+
248
+
249
+ if (currentRadius > _radius)
250
+
251
+ {
252
+
253
+ //角度計算
254
+
255
+ float radian = Mathf.Atan2(_stick.transform.localPosition.y, _stick.transform.localPosition.x);
256
+
257
+
258
+
259
+ //円上にXとYを設定
260
+
261
+ Vector3 limitedPosition = Vector3.zero;
262
+
263
+ limitedPosition.x = _radius * Mathf.Cos(radian);
264
+
265
+ limitedPosition.y = _radius * Mathf.Sin(radian);
266
+
267
+
268
+
269
+ _stickPosition = limitedPosition;
270
+
271
+ }
272
+
273
+ }
274
+
275
+
276
+
277
+ private T GetComponent<T>()
278
+
279
+ {
280
+
281
+ throw new NotImplementedException();
282
+
283
+ }
284
+
285
+ bool isInsideBoundary;
286
+
287
+
288
+
289
+ public void Update(PointerEventData eventData)
290
+
291
+ {
292
+
293
+ Touch[] touches = Input.touches;
294
+
295
+ for (int i = 0; i < Input.touchCount; i++)
296
+
297
+ {
298
+
299
+ Touch touch = touches[i];
300
+
301
+ if (touch.phase == TouchPhase.Began || touch.ohase == TouchPhase.Moved)
302
+
303
+ {
304
+
305
+ isInsideBoundary = OnDrag(touch.position);
306
+
307
+ if (isInsideBoundary) break;
308
+
309
+ }
310
+
311
+ }
312
+
313
+ if (!isInsideBoundary)
314
+
315
+ {
316
+
317
+ if (_shouldResetPosition)
318
+
319
+ {
320
+
321
+ //スティックを中心に戻す
322
+
323
+ _stickPosition = Vector3.zero;
324
+
325
+ }
326
+
327
+ }
328
+
329
+ }
330
+
331
+
332
+
333
+ //=================================================================================
334
+
335
+ //更新
336
+
337
+ //=================================================================================
338
+
339
+
340
+
341
+ #if UNITY_EDITOR
342
+
343
+ //Gizmoを表示する
344
+
345
+ private void OnDrawGizmos()
346
+
347
+ {
348
+
349
+ //スティックが移動できる範囲をScene上に表示
350
+
351
+ UnityEditor.Handles.color = Color.green;
352
+
353
+ UnityEditor.Handles.DrawWireDisc(transform.position, transform.forward, _radius * 0.5f);
354
+
355
+ }
356
+
357
+ #endif
358
+
359
+
360
+
361
+ }
362
+
363
+ ```
364
+
365
+ ###エラー
366
+
367
+ 'Joystick.OnDrag(Vector2)': 値を返さないコード パスがあります。(97行目)
368
+
369
+
370
+
371
+ 型または名前空間の名前 'NotImplementedException' が見つかりませんでした (using ディレクティブまたはアセンブリ参照が指定されていることを確認してください)。(134行目)
372
+
373
+ 140行目、141行目、144行目、146行目ー>定義がありません系のエラー
14
374
 
15
375
  ### 試したこと
16
376
 

1

誤字

2019/10/13 04:51

投稿

Ruchi1237
Ruchi1237

スコア8

test CHANGED
File without changes
test CHANGED
@@ -16,4 +16,4 @@
16
16
 
17
17
 
18
18
 
19
- 回答のようにソースを変更した。
19
+ 回答のようにソースを変更した。