回答編集履歴
2
OnDragEndの機能
answer
CHANGED
@@ -43,14 +43,23 @@
|
|
43
43
|
}
|
44
44
|
```
|
45
45
|
Updateを以下のようにしてください
|
46
|
-
```
|
46
|
+
```C#
|
47
47
|
public void Update(PointerEventData eventData) {
|
48
|
+
bool isInsideBoundary;
|
48
49
|
Touch[] touches = Input.touches;
|
49
50
|
for (int i = 0; i < Input.touchCount; i++) {
|
50
51
|
Touch touch=touches[i];
|
51
52
|
if(touch.phase==TouchPhase.Began||touch.ohase==TouchPhase.Moved){
|
52
|
-
|
53
|
+
isInsideBoundary=OnDrag(touch.position);
|
54
|
+
if(isInsideBoundary)break;
|
53
55
|
}
|
54
56
|
}
|
57
|
+
if(!isInsideBoundary){
|
58
|
+
if (_shouldResetPosition)
|
59
|
+
{
|
60
|
+
//スティックを中心に戻す
|
61
|
+
_stickPosition = Vector3.zero;
|
62
|
+
}
|
63
|
+
}
|
55
64
|
}
|
56
65
|
```
|
1
コードをはじめとする追加
answer
CHANGED
@@ -1,3 +1,56 @@
|
|
1
1
|
大まかな方針だけになりますが
|
2
2
|
Input.touchesを使うと複数の指の座標がもとまるのでforで一本ずつ処理すると良いと思います
|
3
|
-
イメージとしては、Input.touches[指のinxex]の座標がJoyStickの範囲内にあるときはJoystickの位置を更新、ボタンの範囲内にあるときはボタンを更新という感じになると思います。UI座標からスクリーンに変換するところは[stackoverflow](https://answers.unity.com/questions/826851/how-to-get-screen-position-of-a-recttransform-when.html)のやり方で出来ると思います
|
3
|
+
イメージとしては、Input.touches[指のinxex]の座標がJoyStickの範囲内にあるときはJoystickの位置を更新、ボタンの範囲内にあるときはボタンを更新という感じになると思います。UI座標からスクリーンに変換するところは[stackoverflow](https://answers.unity.com/questions/826851/how-to-get-screen-position-of-a-recttransform-when.html)のやり方で出来ると思います
|
4
|
+
|
5
|
+
追記
|
6
|
+
クラスの継承はMonoBehaviorだけにして
|
7
|
+
(Joystick:MonoBehavior)
|
8
|
+
イベント処理の関数は消してください
|
9
|
+
OnDragを以下のように、
|
10
|
+
```C# OnDrag
|
11
|
+
float _radius2=150;//ジョイスティックの最大半径
|
12
|
+
public bool OnDrag(Vector2 touch)
|
13
|
+
{
|
14
|
+
//タップ位置を画面内の座標に変換し、スティックを移動
|
15
|
+
Vector2 screenPos = Vector2.zero;
|
16
|
+
RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(),
|
17
|
+
touch,
|
18
|
+
null,
|
19
|
+
out screenPos
|
20
|
+
);
|
21
|
+
|
22
|
+
_stickPosition = screenPos;
|
23
|
+
|
24
|
+
//移動場所が設定した半径を超えてる場合は制限内に抑える
|
25
|
+
float currentRadius = Vector3.Distance(Vector3.zero, _stick.transform.localPosition);
|
26
|
+
if (currentRadius > _radius2)
|
27
|
+
{return false;}
|
28
|
+
|
29
|
+
if (currentRadius > _radius)
|
30
|
+
{
|
31
|
+
|
32
|
+
//角度計算
|
33
|
+
float radian = Mathf.Atan2(_stick.transform.localPosition.y, _stick.transform.localPosition.x);
|
34
|
+
|
35
|
+
//円上にXとYを設定
|
36
|
+
Vector3 limitedPosition = Vector3.zero;
|
37
|
+
limitedPosition.x = _radius * Mathf.Cos(radian);
|
38
|
+
limitedPosition.y = _radius * Mathf.Sin(radian);
|
39
|
+
|
40
|
+
_stickPosition = limitedPosition;
|
41
|
+
}
|
42
|
+
return true;
|
43
|
+
}
|
44
|
+
```
|
45
|
+
Updateを以下のようにしてください
|
46
|
+
```
|
47
|
+
public void Update(PointerEventData eventData) {
|
48
|
+
Touch[] touches = Input.touches;
|
49
|
+
for (int i = 0; i < Input.touchCount; i++) {
|
50
|
+
Touch touch=touches[i];
|
51
|
+
if(touch.phase==TouchPhase.Began||touch.ohase==TouchPhase.Moved){
|
52
|
+
if(OnDrag(touch.position))break;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
```
|