回答編集履歴

2

OnDragEndの機能

2019/01/16 10:51

投稿

bochan2
bochan2

スコア2050

test CHANGED
@@ -88,9 +88,11 @@
88
88
 
89
89
  Updateを以下のようにしてください
90
90
 
91
- ```
91
+ ```C#
92
92
 
93
93
  public void Update(PointerEventData eventData) {
94
+
95
+ bool isInsideBoundary;
94
96
 
95
97
  Touch[] touches = Input.touches;
96
98
 
@@ -100,9 +102,25 @@
100
102
 
101
103
  if(touch.phase==TouchPhase.Began||touch.ohase==TouchPhase.Moved){
102
104
 
103
- if(OnDrag(touch.position))break;
105
+ isInsideBoundary=OnDrag(touch.position);
106
+
107
+ if(isInsideBoundary)break;
104
108
 
105
109
  }
110
+
111
+ }
112
+
113
+ if(!isInsideBoundary){
114
+
115
+ if (_shouldResetPosition)
116
+
117
+ {
118
+
119
+ //スティックを中心に戻す
120
+
121
+ _stickPosition = Vector3.zero;
122
+
123
+ }
106
124
 
107
125
  }
108
126
 

1

コードをはじめとする追加

2019/01/16 10:51

投稿

bochan2
bochan2

スコア2050

test CHANGED
@@ -3,3 +3,109 @@
3
3
  Input.touchesを使うと複数の指の座標がもとまるのでforで一本ずつ処理すると良いと思います
4
4
 
5
5
  イメージとしては、Input.touches[指のinxex]の座標がJoyStickの範囲内にあるときはJoystickの位置を更新、ボタンの範囲内にあるときはボタンを更新という感じになると思います。UI座標からスクリーンに変換するところは[stackoverflow](https://answers.unity.com/questions/826851/how-to-get-screen-position-of-a-recttransform-when.html)のやり方で出来ると思います
6
+
7
+
8
+
9
+ 追記
10
+
11
+ クラスの継承はMonoBehaviorだけにして
12
+
13
+ (Joystick:MonoBehavior)
14
+
15
+ イベント処理の関数は消してください
16
+
17
+ OnDragを以下のように、
18
+
19
+ ```C# OnDrag
20
+
21
+ float _radius2=150;//ジョイスティックの最大半径
22
+
23
+ public bool OnDrag(Vector2 touch)
24
+
25
+ {
26
+
27
+ //タップ位置を画面内の座標に変換し、スティックを移動
28
+
29
+ Vector2 screenPos = Vector2.zero;
30
+
31
+ RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(),
32
+
33
+ touch,
34
+
35
+ null,
36
+
37
+ out screenPos
38
+
39
+ );
40
+
41
+
42
+
43
+ _stickPosition = screenPos;
44
+
45
+
46
+
47
+ //移動場所が設定した半径を超えてる場合は制限内に抑える
48
+
49
+ float currentRadius = Vector3.Distance(Vector3.zero, _stick.transform.localPosition);
50
+
51
+ if (currentRadius > _radius2)
52
+
53
+ {return false;}
54
+
55
+
56
+
57
+ if (currentRadius > _radius)
58
+
59
+ {
60
+
61
+
62
+
63
+ //角度計算
64
+
65
+ float radian = Mathf.Atan2(_stick.transform.localPosition.y, _stick.transform.localPosition.x);
66
+
67
+
68
+
69
+ //円上にXとYを設定
70
+
71
+ Vector3 limitedPosition = Vector3.zero;
72
+
73
+ limitedPosition.x = _radius * Mathf.Cos(radian);
74
+
75
+ limitedPosition.y = _radius * Mathf.Sin(radian);
76
+
77
+
78
+
79
+ _stickPosition = limitedPosition;
80
+
81
+ }
82
+
83
+ return true;
84
+
85
+ }
86
+
87
+ ```
88
+
89
+ Updateを以下のようにしてください
90
+
91
+ ```
92
+
93
+ public void Update(PointerEventData eventData) {
94
+
95
+ Touch[] touches = Input.touches;
96
+
97
+ for (int i = 0; i < Input.touchCount; i++) {
98
+
99
+ Touch touch=touches[i];
100
+
101
+ if(touch.phase==TouchPhase.Began||touch.ohase==TouchPhase.Moved){
102
+
103
+ if(OnDrag(touch.position))break;
104
+
105
+ }
106
+
107
+ }
108
+
109
+ }
110
+
111
+ ```