質問編集履歴
3
Intに変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,14 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
|
12
|
+
|
13
|
+

|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
修正コード
|
18
|
+
|
11
19
|
```ここに言語を入力
|
12
20
|
|
13
21
|
using System.Collections;
|
@@ -92,12 +100,178 @@
|
|
92
100
|
|
93
101
|
|
94
102
|
|
103
|
+
if (Input.GetKey("down"))
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
SetAnimationParam(0);
|
108
|
+
|
109
|
+
target = transform.position - MOVEY;
|
110
|
+
|
111
|
+
return;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
if (Input.GetKey("up"))
|
116
|
+
|
117
|
+
{
|
118
|
+
|
119
|
+
SetAnimationParam(1);
|
120
|
+
|
121
|
+
target = transform.position + MOVEY;
|
122
|
+
|
123
|
+
return;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
95
127
|
if (Input.GetKey("right"))
|
96
128
|
|
97
129
|
{
|
98
130
|
|
131
|
+
SetAnimationParam(2);
|
132
|
+
|
99
133
|
target = transform.position + MOVEX;
|
100
134
|
|
135
|
+
return;
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
if (Input.GetKey("left"))
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
SetAnimationParam(3);
|
144
|
+
|
145
|
+
target = transform.position - MOVEX;
|
146
|
+
|
147
|
+
return;
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
// WalkParam 0;下移動 1;上移動 2:右移動 3:左移動
|
156
|
+
|
157
|
+
void SetAnimationParam(int param)
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
this.animator.SetInteger("Move",param);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
// ③ 目的地へ移動する
|
168
|
+
|
169
|
+
void Move()
|
170
|
+
|
171
|
+
{
|
172
|
+
|
173
|
+
transform.position = Vector3.MoveTowards(transform.position, target, step * Time.deltaTime);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```ここに言語を入力
|
186
|
+
|
187
|
+
using System.Collections;
|
188
|
+
|
189
|
+
using System.Collections.Generic;
|
190
|
+
|
191
|
+
using UnityEngine;
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
public class PlayerController : MonoBehaviour{
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
Vector3 MOVEX = new Vector3(0.32f, 0, 0); // x軸方向に1マス移動するときの距離
|
200
|
+
|
201
|
+
Vector3 MOVEY = new Vector3(0, 0.32f, 0); // y軸方向に1マス移動するときの距離
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
float step = 2f; // 移動速度
|
206
|
+
|
207
|
+
Vector3 target; // 入力受付時、移動後の位置を算出して保存
|
208
|
+
|
209
|
+
Vector3 prevPos; // 何らかの理由で移動できなかった場合、元の位置に戻すため移動前の位置を保存
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
Animator animator; // アニメーション
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
// Use this for initialization
|
220
|
+
|
221
|
+
void Start()
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
target = transform.position;
|
226
|
+
|
227
|
+
animator = GetComponent<Animator>();
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
// Update is called once per frame
|
234
|
+
|
235
|
+
void Update()
|
236
|
+
|
237
|
+
{
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
// ① 移動中かどうかの判定。移動中でなければ入力を受付
|
242
|
+
|
243
|
+
if (transform.position == target)
|
244
|
+
|
245
|
+
{
|
246
|
+
|
247
|
+
SetTargetPosition();
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
Move();
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// ② 入力に応じて移動後の位置を算出
|
258
|
+
|
259
|
+
void SetTargetPosition()
|
260
|
+
|
261
|
+
{
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
prevPos = target;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
if (Input.GetKey("right"))
|
270
|
+
|
271
|
+
{
|
272
|
+
|
273
|
+
target = transform.position + MOVEX;
|
274
|
+
|
101
275
|
SetAnimationParam(1);
|
102
276
|
|
103
277
|
return;
|
@@ -204,8 +378,6 @@
|
|
204
378
|
|
205
379
|
```
|
206
380
|
|
207
|
-
|
208
|
-
|
209
381
|

|
210
382
|
|
211
383
|

|
2
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -207,3 +207,7 @@
|
|
207
207
|
|
208
208
|
|
209
209
|

|
210
|
+
|
211
|
+

|
212
|
+
|
213
|
+

|
1
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -198,4 +198,12 @@
|
|
198
198
|
|
199
199
|
|
200
200
|
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
201
205
|
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+

|