質問編集履歴
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -335,3 +335,135 @@
|
|
335
335
|
Visual Studio 2019
|
336
336
|
|
337
337
|
Unity 2020.3.17f
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
### 追記
|
342
|
+
|
343
|
+
問題が解決したので追記です。
|
344
|
+
|
345
|
+
このやり方だと特定のオブジェクトに.csをアタッチし、それを特定のオブジェクトにドラッグ&ドロップしなければいけないのであまり好ましいやり方ではないです。
|
346
|
+
|
347
|
+
参考にされた方でもっとスマートなやり方があればこの記事の返答欄で教えていただけると助かります。
|
348
|
+
|
349
|
+
自分でも調べてみるのでまたわかったらここに書きます。
|
350
|
+
|
351
|
+
```C#
|
352
|
+
|
353
|
+
public class PlayerMover : BasePlayerComponent
|
354
|
+
|
355
|
+
{
|
356
|
+
|
357
|
+
private PlayerPrameter _playerPrameter;
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
// ここで直接CameraManager を付けたオブジェクトをアタッチし、情報を持ってきている。
|
362
|
+
|
363
|
+
[SerializeField]
|
364
|
+
|
365
|
+
private CameraManager _cameraManager;
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
private float _moveSpeed;
|
370
|
+
|
371
|
+
private float _rotSpeed;
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
protected override void OnStart()
|
376
|
+
|
377
|
+
{
|
378
|
+
|
379
|
+
base.OnStart();
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
_playerPrameter = GetComponent<PlayerPrameter>();
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
_moveSpeed = _playerPrameter.MoveSpeed;
|
388
|
+
|
389
|
+
_rotSpeed = _playerPrameter.RotSpeed;
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
// プレイヤー移動
|
396
|
+
|
397
|
+
public void PlayerMove(float h, float v)
|
398
|
+
|
399
|
+
{
|
400
|
+
|
401
|
+
Vector3 velocity = Vector3.zero;
|
402
|
+
|
403
|
+
float speedExe = _moveSpeed * Time.deltaTime;
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
if (v > 0)
|
408
|
+
|
409
|
+
{
|
410
|
+
|
411
|
+
velocity.z -= 1;
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
if (v < 0)
|
416
|
+
|
417
|
+
{
|
418
|
+
|
419
|
+
velocity.z += 1;
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
if (h > 0)
|
424
|
+
|
425
|
+
{
|
426
|
+
|
427
|
+
velocity.x -= 1;
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
if (h < 0)
|
432
|
+
|
433
|
+
{
|
434
|
+
|
435
|
+
velocity.x += 1;
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
velocity = velocity.normalized * speedExe;
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
if (velocity.magnitude > 0)
|
446
|
+
|
447
|
+
{
|
448
|
+
|
449
|
+
transform.rotation = Quaternion.Slerp(transform.rotation,
|
450
|
+
|
451
|
+
Quaternion.LookRotation(_cameraManager.HRotation * -velocity),
|
452
|
+
|
453
|
+
_rotSpeed);
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
// プレイヤーの位置(transform.position)の更新
|
458
|
+
|
459
|
+
// カメラの水平回転(refCamera.hRotation)で回した移動方向(velocity)を足し込む
|
460
|
+
|
461
|
+
transform.position += _cameraManager.HRotation * -velocity;
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
```
|