質問編集履歴
2
書式の改善
title
CHANGED
File without changes
|
body
CHANGED
@@ -235,8 +235,9 @@
|
|
235
235
|
|
236
236
|
### 試したこと
|
237
237
|
|
238
|
-

|
238
|
+

|
239
239
|
|
240
240
|
【回答を追記・編集しました】
|
241
241
|
ユニットはランダムに移動しているのですが、ユニットの「不満値」というパラメータが100になると、他のユニットの隣接に移動して、攻撃する、という実装にしようと思っていました。
|
242
|
-
ユニットが移動するスクリプトはUnit_RandomWalk.csというスクリプトです。すべてのユニットにこのスクリプトがアタッチされています。
|
242
|
+
ユニットが移動するスクリプトはUnit_RandomWalk.csというスクリプトです。すべてのユニットにこのスクリプトがアタッチされています。
|
243
|
+
目的のユニットが移動するたびに経路が再計算されるため、上記の図のように、ぐらぐらと揺れながら移動してしまっています。
|
1
スクリプトの追加・説明の編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,19 +8,20 @@
|
|
8
8
|
using System.Collections;
|
9
9
|
using System.Collections.Generic;
|
10
10
|
using UnityEngine;
|
11
|
-
using Module_of_OverWorld;
|
12
|
-
using
|
11
|
+
using System.Linq;
|
13
12
|
|
14
13
|
namespace UtilityModule
|
15
14
|
{
|
16
15
|
public class Moving_from_PositionA_to_PositionB : MonoBehaviour
|
17
16
|
{
|
18
|
-
private int map_Width =
|
17
|
+
private int map_Width = 100;
|
19
|
-
private int map_Height =
|
18
|
+
private int map_Height = 100;
|
20
19
|
private ChessboardPathFinder pathFinder;
|
21
20
|
public List<Vector2> moveList = new List<Vector2>();
|
22
|
-
private float speed =
|
21
|
+
private float speed = 2.5f;
|
23
22
|
public bool stoped_Movement;
|
23
|
+
//始動したコルーチンを覚えておくためのフィールドを追加し...
|
24
|
+
private Coroutine movementAction;
|
24
25
|
|
25
26
|
void Awake()
|
26
27
|
{
|
@@ -36,17 +37,34 @@
|
|
36
37
|
|
37
38
|
public void Start_MovementAction()
|
38
39
|
{
|
40
|
+
//コルーチン始動時にそれを保存
|
39
|
-
StartCoroutine(Move());
|
41
|
+
movementAction = StartCoroutine(Move());
|
40
42
|
}
|
41
43
|
|
44
|
+
//そして覚えておいたコルーチンを停止するためのメソッドを用意
|
45
|
+
public void Stop_MovementAction()
|
46
|
+
{
|
47
|
+
if (movementAction != null)
|
48
|
+
{
|
49
|
+
StopCoroutine(movementAction);
|
50
|
+
moveList.Clear();
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
42
54
|
IEnumerator Move()
|
43
55
|
{
|
44
|
-
foreach (Vector2Int position in pathFinder.Path)
|
56
|
+
foreach (Vector2Int position in pathFinder.Path.Skip(1))
|
45
57
|
{
|
46
58
|
Vector2 pos = new Vector3(position.x, position.y);
|
47
59
|
moveList.Add(pos);
|
48
60
|
}
|
49
61
|
|
62
|
+
Vector2 unitPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
|
63
|
+
if(moveList.Contains(unitPos))
|
64
|
+
{
|
65
|
+
moveList.Remove(unitPos);
|
66
|
+
}
|
67
|
+
|
50
68
|
int count = 0;
|
51
69
|
|
52
70
|
while (true)
|
@@ -73,7 +91,6 @@
|
|
73
91
|
}
|
74
92
|
}
|
75
93
|
}
|
76
|
-
|
77
94
|
```
|
78
95
|
```C#
|
79
96
|
using System.Collections;
|
@@ -89,13 +106,12 @@
|
|
89
106
|
private Unit_RandomWalk unit_RandomWalk;
|
90
107
|
private Unit gameObjectUnit;
|
91
108
|
private Moving_from_PositionA_to_PositionB moving_From_PositionA_To_PositionB;
|
92
|
-
|
93
109
|
private Unit_RandomWalk unit_RandomWalk_SelectedUnit;
|
94
110
|
private GameObject selectedUnit;
|
95
|
-
|
96
111
|
private bool get_Distance;
|
97
|
-
|
98
112
|
private bool switched_Berserkermode;
|
113
|
+
//前回のUpdateで調べたターゲットの位置を保存するためのフィールドを追加
|
114
|
+
private Vector2Int latestTargetPos;
|
99
115
|
|
100
116
|
private void Awake()
|
101
117
|
{
|
@@ -107,7 +123,7 @@
|
|
107
123
|
public void Action()
|
108
124
|
{
|
109
125
|
//不満値が100%になったら、ランダム移動を停止する
|
110
|
-
unit_RandomWalk.
|
126
|
+
unit_RandomWalk.Stop_RandomWalk();
|
111
127
|
|
112
128
|
Invoke("Go", 1);
|
113
129
|
}
|
@@ -133,7 +149,7 @@
|
|
133
149
|
float distanse = Vector2.Distance(gameObject.transform.position, selectedUnit.transform.position);
|
134
150
|
if (distanse <= 3)
|
135
151
|
{
|
136
|
-
unit_RandomWalk_SelectedUnit.
|
152
|
+
unit_RandomWalk_SelectedUnit.Stop_RandomWalk();
|
137
153
|
int x = Mathf.RoundToInt(unit_RandomWalk_SelectedUnit.transform.position.x);
|
138
154
|
int y = Mathf.RoundToInt(unit_RandomWalk_SelectedUnit.transform.position.y);
|
139
155
|
unit_RandomWalk_SelectedUnit.transform.position = new Vector3(x, y, 0);
|
@@ -141,15 +157,37 @@
|
|
141
157
|
}
|
142
158
|
|
143
159
|
//対象のユニットが存在していた座標に到達したら、また移動する
|
160
|
+
/*
|
144
161
|
if (switched_Berserkermode && gameObject.transform.position == (Vector3)moving_From_PositionA_To_PositionB.moveList[moving_From_PositionA_To_PositionB.moveList.Count - 1])
|
145
162
|
{
|
146
163
|
Go_To_TargetUnit();
|
147
164
|
}
|
165
|
+
*/
|
166
|
+
|
167
|
+
if (switched_Berserkermode)
|
168
|
+
{
|
169
|
+
//現在のターゲットの位置を調べ...
|
170
|
+
Vector2Int targetPos = new Vector2Int((int)selectedUnit.transform.position.x, (int)selectedUnit.transform.position.y);
|
171
|
+
|
172
|
+
//敵の位置に変化があったら...
|
173
|
+
if (targetPos != latestTargetPos)
|
174
|
+
{
|
175
|
+
//現在動作中の移動コルーチンは中止し、目的地への経路を再計算
|
176
|
+
latestTargetPos = targetPos;
|
177
|
+
Stop_Movement();
|
178
|
+
Go_To_TargetUnit();
|
179
|
+
}
|
180
|
+
}
|
148
181
|
}
|
149
182
|
|
183
|
+
void Stop_Movement()
|
184
|
+
{
|
185
|
+
moving_From_PositionA_To_PositionB.Stop_MovementAction();
|
186
|
+
}
|
187
|
+
|
150
188
|
void Go_To_TargetUnit()
|
151
189
|
{
|
152
|
-
moving_From_PositionA_To_PositionB.moveList.Clear();
|
190
|
+
//moving_From_PositionA_To_PositionB.moveList.Clear();
|
153
191
|
|
154
192
|
Vector2Int startPos = new Vector2Int((int)gameObject.transform.position.x, (int)gameObject.transform.position.y);
|
155
193
|
Vector2Int goalPos = new Vector2Int((int)selectedUnit.transform.position.x, (int)selectedUnit.transform.position.y);
|
@@ -193,11 +231,12 @@
|
|
193
231
|
}
|
194
232
|
}
|
195
233
|
}
|
196
|
-
|
197
234
|
```
|
198
235
|
|
199
|
-
|
200
236
|
### 試したこと
|
201
237
|
|
202
|
-
[
|
238
|
+
](6cd8d382d3f3084a1fd6e86ede1bce7c.png)
|
239
|
+
|
240
|
+
【回答を追記・編集しました】
|
203
|
-
|
241
|
+
ユニットはランダムに移動しているのですが、ユニットの「不満値」というパラメータが100になると、他のユニットの隣接に移動して、攻撃する、という実装にしようと思っていました。
|
242
|
+
ユニットが移動するスクリプトはUnit_RandomWalk.csというスクリプトです。すべてのユニットにこのスクリプトがアタッチされています。
|