質問編集履歴
3
情報の微追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -458,6 +458,8 @@
|
|
458
458
|
|
459
459
|
### 補足情報(FW/ツールのバージョンなど)
|
460
460
|
|
461
|
+
シーンから弾は取り除き済み
|
462
|
+
|
461
463
|
Waypointのスクリプト等は
|
462
464
|
|
463
465
|
[Unityで3D横スクロールの挙動を考える](http://adarapata.hatenablog.com/entry/2016/12/15/180722)
|
2
説明不足分追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,6 +34,8 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
+
キャラ本体側のスクリプト
|
38
|
+
|
37
39
|
```ここに言語名を入力
|
38
40
|
|
39
41
|
using System.Collections;
|
@@ -62,7 +64,7 @@
|
|
62
64
|
|
63
65
|
private bool isGrounded;
|
64
66
|
|
65
|
-
private float bulletspeed = 500f;
|
67
|
+
private float bulletspeed = 500f;//弾速
|
66
68
|
|
67
69
|
//ジャンプ力
|
68
70
|
|
@@ -80,7 +82,7 @@
|
|
80
82
|
|
81
83
|
private float power = 10f;
|
82
84
|
|
83
|
-
public Transform shotpoint;
|
85
|
+
public Transform shotpoint;//弾丸発射地点
|
84
86
|
|
85
87
|
void Start()
|
86
88
|
|
@@ -220,7 +222,9 @@
|
|
220
222
|
|
221
223
|
|
222
224
|
|
223
|
-
|
225
|
+
Waypointに沿って方向転換するスクリプト
|
226
|
+
|
227
|
+
```
|
224
228
|
|
225
229
|
using UnityEngine;
|
226
230
|
|
@@ -390,7 +394,9 @@
|
|
390
394
|
|
391
395
|
|
392
396
|
|
393
|
-
|
397
|
+
適用すると悪化する弾丸側スクリプト
|
398
|
+
|
399
|
+
```
|
394
400
|
|
395
401
|
using System.Collections;
|
396
402
|
|
1
何故かコードや使用ツールについての情報が全部吹っ飛んでいたので張り直しました。申し訳ございません
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,27 +8,23 @@
|
|
8
8
|
|
9
9
|
逆に、玉本体にプログラムを設定しなかった場合は突っかかる事はありませんが、Waypointに沿う事なくまっすぐ飛んでいきます
|
10
10
|
|
11
|
-
これはあくまで私個人の憶測なのですが、今使っているプログラムがinstanceのオブジェクトには対応していないと
|
11
|
+
これはあくまで私個人の憶測なのですが、今使っているプログラムがinstanceのオブジェクトには対応していないと考えています
|
12
|
-
|
13
|
-
|
12
|
+
|
14
|
-
|
15
|
-
|
13
|
+
今後敵の操作等もプレハブで発生させたものをWaypointで管理したいのでかなり重大な問題です
|
16
|
-
|
17
|
-
|
14
|
+
|
18
|
-
|
19
|
-
|
15
|
+
WaypointをInstanceに適用することはできないのでしょうか
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
16
|
+
|
17
|
+
|
18
|
+
|
25
|
-
### 発生している問題・エラーメッセージ
|
19
|
+
### 発生している問題・エラーメッセージ(弾丸にメソッド適用時のみ)
|
26
20
|
|
27
21
|
|
28
22
|
|
29
23
|
```
|
30
24
|
|
25
|
+
NullReferenceException: Object reference not set to an instance of an object
|
26
|
+
|
31
|
-
|
27
|
+
LineTrace.DirectionController2d.set_direction (LineTrace.Direction value)
|
32
28
|
|
33
29
|
```
|
34
30
|
|
@@ -40,12 +36,408 @@
|
|
40
36
|
|
41
37
|
```ここに言語名を入力
|
42
38
|
|
39
|
+
using System.Collections;
|
40
|
+
|
41
|
+
using System.Collections.Generic;
|
42
|
+
|
43
|
+
using UnityEngine;
|
44
|
+
|
45
|
+
using LineTrace;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
public class nita2dmove : MonoBehaviour
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
public DirectionController2d controller;
|
54
|
+
|
55
|
+
public float speed;
|
56
|
+
|
57
|
+
private Rigidbody rb;
|
58
|
+
|
59
|
+
//キャラクターのコライダ
|
60
|
+
|
61
|
+
private CapsuleCollider myCollider;
|
62
|
+
|
63
|
+
private bool isGrounded;
|
64
|
+
|
65
|
+
private float bulletspeed = 500f;
|
66
|
+
|
67
|
+
//ジャンプ力
|
68
|
+
|
69
|
+
[SerializeField]
|
70
|
+
|
71
|
+
private float jumppower = 5f;
|
72
|
+
|
73
|
+
private Vector3 velocity;
|
74
|
+
|
75
|
+
// Start is called before the first frame update
|
76
|
+
|
77
|
+
[SerializeField]
|
78
|
+
|
79
|
+
private GameObject bulletPrefab;
|
80
|
+
|
81
|
+
private float power = 10f;
|
82
|
+
|
83
|
+
public Transform shotpoint;
|
84
|
+
|
85
|
+
void Start()
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
rb = GetComponent<Rigidbody>();
|
90
|
+
|
91
|
+
myCollider = GetComponent<CapsuleCollider>();
|
92
|
+
|
93
|
+
controller.direction = Direction.front;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// Update is called once per frame
|
100
|
+
|
101
|
+
void Update()
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
CheckGround();
|
106
|
+
|
107
|
+
Jump();
|
108
|
+
|
109
|
+
Move();
|
110
|
+
|
111
|
+
Fire();
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
private void Move()
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
if (Input.GetKey(KeyCode.LeftArrow))
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
// 向きを設定する
|
126
|
+
|
127
|
+
controller.direction = Direction.back;
|
128
|
+
|
129
|
+
rb.MovePosition(transform.position + controller.forward * speed * Time.deltaTime);
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
else if (Input.GetKey(KeyCode.RightArrow))
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
// 向きを設定する
|
138
|
+
|
139
|
+
controller.direction = Direction.front;
|
140
|
+
|
141
|
+
rb.MovePosition(transform.position + controller.forward * speed * Time.deltaTime);
|
142
|
+
|
143
|
+
//controller.forward*speed=velocity
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
private void Jump(){
|
152
|
+
|
153
|
+
if (isGrounded)
|
154
|
+
|
155
|
+
{
|
156
|
+
|
157
|
+
if (Input.GetButtonDown("Jump"))
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
isGrounded = false;
|
162
|
+
|
163
|
+
rb.velocity = new Vector3(rb.velocity.x, jumppower, rb.velocity.z);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
private void Fire()
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
if (Input.GetKeyUp(KeyCode.Z))
|
176
|
+
|
177
|
+
{
|
178
|
+
|
179
|
+
var bulletInstance = Instantiate<GameObject>(bulletPrefab, shotpoint.position, shotpoint.rotation);
|
180
|
+
|
181
|
+
bulletInstance.GetComponent<Rigidbody>().AddForce(bulletInstance.transform.position+controller.forward * bulletspeed);
|
182
|
+
|
183
|
+
Destroy(bulletInstance, 5f);
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
private void CheckGround() {
|
192
|
+
|
193
|
+
if (isGrounded) {
|
194
|
+
|
195
|
+
return;
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
if (Physics.CheckSphere(rb.position, myCollider.radius - 0.1f, LayerMask.GetMask("Ground")))
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
isGrounded = true;
|
206
|
+
|
207
|
+
velocity.y = 0f;
|
208
|
+
|
43
|
-
|
209
|
+
}else{
|
210
|
+
|
211
|
+
isGrounded = false;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
44
218
|
|
45
219
|
```
|
46
220
|
|
47
221
|
|
48
222
|
|
223
|
+
```Waypointに沿って方向転換するスクリプト
|
224
|
+
|
225
|
+
using UnityEngine;
|
226
|
+
|
227
|
+
using UniRx;
|
228
|
+
|
229
|
+
using UniRx.Triggers;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
namespace LineTrace
|
234
|
+
|
235
|
+
{
|
236
|
+
|
237
|
+
public class DirectionController2d : MonoBehaviour
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
/// <summary>
|
242
|
+
|
243
|
+
/// 正面ベクトル
|
244
|
+
|
245
|
+
/// </summary>
|
246
|
+
|
247
|
+
public Vector3 forward { get; private set; }
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
private Direction mDirection;
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
/// <summary>
|
256
|
+
|
257
|
+
/// 向いている方向
|
258
|
+
|
259
|
+
/// </summary>
|
260
|
+
|
261
|
+
public Direction direction
|
262
|
+
|
263
|
+
{
|
264
|
+
|
265
|
+
get { return mDirection; }
|
266
|
+
|
267
|
+
set
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
mDirection = value;
|
272
|
+
|
273
|
+
var f = (current.GetWayPointByDirection(mDirection) - transform.position);
|
274
|
+
|
275
|
+
f.y = 0F;
|
276
|
+
|
277
|
+
forward = f.normalized;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
private Line current;
|
286
|
+
|
287
|
+
[SerializeField] private float distance = 0.1F;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
[SerializeField] private bool autoRotation = false;
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
[SerializeField] private float rotateSpeed = 30F;
|
296
|
+
|
297
|
+
// Use this for initialization
|
298
|
+
|
299
|
+
void Start()
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
var lineManager = FindObjectOfType<LineManager>();
|
304
|
+
|
305
|
+
current = lineManager.GetLineAtNearDistance(transform.position);
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
this.UpdateAsObservable()
|
310
|
+
|
311
|
+
// 目的のポイントに接近したかどうか
|
312
|
+
|
313
|
+
.Where(
|
314
|
+
|
315
|
+
_ =>
|
316
|
+
|
317
|
+
Mathf.Abs(Vector2.Distance(transform.position.XZ(),
|
318
|
+
|
319
|
+
current.GetWayPointByDirection(mDirection).XZ())) < distance)
|
320
|
+
|
321
|
+
.Subscribe(_ =>
|
322
|
+
|
323
|
+
{
|
324
|
+
|
325
|
+
var next = current.GetNextLineByDirection(mDirection);
|
326
|
+
|
327
|
+
current = next ?? current;
|
328
|
+
|
329
|
+
direction = mDirection;
|
330
|
+
|
331
|
+
});
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
// 目的地の方向に自動で回転する
|
336
|
+
|
337
|
+
if (autoRotation)
|
338
|
+
|
339
|
+
{
|
340
|
+
|
341
|
+
this.UpdateAsObservable()
|
342
|
+
|
343
|
+
.Where(_ => forward != Vector3.zero)
|
344
|
+
|
345
|
+
.Subscribe(_ =>
|
346
|
+
|
347
|
+
{
|
348
|
+
|
349
|
+
var arrivedForward = forward.XZ();
|
350
|
+
|
351
|
+
var cross = transform.forward.XZ().Cross(arrivedForward);
|
352
|
+
|
353
|
+
var dot = Vector2.Dot(transform.forward.XZ(), arrivedForward);
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
if (dot < 0.98F)
|
358
|
+
|
359
|
+
{
|
360
|
+
|
361
|
+
transform.Rotate(Vector3.down, rotateSpeed * Mathf.Sign(cross) * Time.deltaTime);
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
});
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
public enum Direction
|
376
|
+
|
377
|
+
{
|
378
|
+
|
379
|
+
front,
|
380
|
+
|
381
|
+
back,
|
382
|
+
|
383
|
+
none
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
```
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
```適用すると悪化する弾丸側スクリプト
|
394
|
+
|
395
|
+
using System.Collections;
|
396
|
+
|
397
|
+
using System.Collections.Generic;
|
398
|
+
|
399
|
+
using UnityEngine;
|
400
|
+
|
401
|
+
using LineTrace;
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
public class protbsllet : MonoBehaviour
|
406
|
+
|
407
|
+
{
|
408
|
+
|
409
|
+
private DirectionController2d controller;
|
410
|
+
|
411
|
+
private Vector3 velocity;
|
412
|
+
|
413
|
+
// Start is called before the first frame update
|
414
|
+
|
415
|
+
void Start()
|
416
|
+
|
417
|
+
{
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
// Update is called once per frame
|
426
|
+
|
427
|
+
void Update()
|
428
|
+
|
429
|
+
{
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
```
|
438
|
+
|
439
|
+
|
440
|
+
|
49
441
|
### 試したこと
|
50
442
|
|
51
443
|
玉本体の回転固定と解除
|
@@ -60,6 +452,8 @@
|
|
60
452
|
|
61
453
|
### 補足情報(FW/ツールのバージョンなど)
|
62
454
|
|
63
|
-
|
455
|
+
Waypointのスクリプト等は
|
456
|
+
|
64
|
-
|
457
|
+
[Unityで3D横スクロールの挙動を考える](http://adarapata.hatenablog.com/entry/2016/12/15/180722)
|
458
|
+
|
65
|
-
こ
|
459
|
+
こちらのサイト様のものをほぼそのままお借りしています
|