質問編集履歴
2
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -194,6 +194,422 @@
|
|
194
194
|
|
195
195
|
```
|
196
196
|
|
197
|
+
```C#
|
198
|
+
|
199
|
+
//参考になるか分かりませんが、プレイヤーオブジェクトとして使っているUnityちゃんのソースコードです。
|
200
|
+
|
201
|
+
// Mecanimのアニメーションデータが、原点で移動しない場合の Rigidbody付きコントローラ
|
202
|
+
|
203
|
+
// サンプル
|
204
|
+
|
205
|
+
// 2014/03/13 N.Kobyasahi
|
206
|
+
|
207
|
+
//
|
208
|
+
|
209
|
+
using UnityEngine;
|
210
|
+
|
211
|
+
using System.Collections;
|
212
|
+
|
213
|
+
using System.Runtime.CompilerServices;
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
namespace UnityChan
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
// 必要なコンポーネントの列記
|
222
|
+
|
223
|
+
[RequireComponent(typeof(Animator))]
|
224
|
+
|
225
|
+
[RequireComponent(typeof(CapsuleCollider))]
|
226
|
+
|
227
|
+
[RequireComponent(typeof(Rigidbody))]
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
public class UnityChanControlScriptWithRgidBody : MonoBehaviour
|
232
|
+
|
233
|
+
{
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public float animSpeed = 1.5f; // アニメーション再生速度設定
|
238
|
+
|
239
|
+
public float lookSmoother = 3.0f; // a smoothing setting for camera motion
|
240
|
+
|
241
|
+
public bool useCurves; // Mecanimでカーブ調整を使うか設定する
|
242
|
+
|
243
|
+
// このスイッチが入っていないとカーブは使われない
|
244
|
+
|
245
|
+
public float useCurvesHeight = 0.5f; // カーブ補正の有効高さ(地面をすり抜けやすい時には大きくする)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
// 以下キャラクターコントローラ用パラメタ
|
250
|
+
|
251
|
+
// 前進速度
|
252
|
+
|
253
|
+
public float PlayerSpeed = 3.0f;
|
254
|
+
|
255
|
+
// 後退速度
|
256
|
+
|
257
|
+
//public float backwardSpeed = 2.0f;
|
258
|
+
|
259
|
+
// 旋回速度
|
260
|
+
|
261
|
+
public float rotateSpeed = 2.0f;
|
262
|
+
|
263
|
+
// ジャンプ威力
|
264
|
+
|
265
|
+
public float jumpPower = 3.0f;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
// 移動速度の入力に対する追従度
|
270
|
+
|
271
|
+
public float moveForceMultiplier;
|
272
|
+
|
273
|
+
//カメラ取得
|
274
|
+
|
275
|
+
public GameObject mainCamera;
|
276
|
+
|
277
|
+
// カメラの向きに合わせて移動させたい場合はtrue(慣性)
|
278
|
+
|
279
|
+
public bool isUseCameraDirection = true;
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
// キャラクターコントローラ(カプセルコライダ)の参照
|
284
|
+
|
285
|
+
private CapsuleCollider col;
|
286
|
+
|
287
|
+
//private Rigidbody rb;
|
288
|
+
|
289
|
+
public Rigidbody rb; //自分で書き換えた
|
290
|
+
|
291
|
+
// キャラクターコントローラ(カプセルコライダ)の移動量
|
292
|
+
|
293
|
+
private Vector3 velocity;
|
294
|
+
|
295
|
+
private Vector3 velocityX;
|
296
|
+
|
297
|
+
// CapsuleColliderで設定されているコライダのHeiht、Centerの初期値を収める変数
|
298
|
+
|
299
|
+
private float orgColHight;
|
300
|
+
|
301
|
+
private Vector3 orgVectColCenter;
|
302
|
+
|
303
|
+
private Animator anim; // キャラにアタッチされるアニメーターへの参照
|
304
|
+
|
305
|
+
private AnimatorStateInfo currentBaseState; // base layerで使われる、アニメーターの現在の状態の参照
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
//private GameObject cameraObject; // メインカメラへの参照
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
// アニメーター各ステートへの参照
|
314
|
+
|
315
|
+
static int idleState = Animator.StringToHash ("Base Layer.Idle");
|
316
|
+
|
317
|
+
static int locoState = Animator.StringToHash ("Base Layer.Locomotion");
|
318
|
+
|
319
|
+
static int jumpState = Animator.StringToHash ("Base Layer.Jump");
|
320
|
+
|
321
|
+
static int restState = Animator.StringToHash ("Base Layer.Rest");
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
// 初期化
|
326
|
+
|
327
|
+
void Start ()
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
// Animatorコンポーネントを取得する
|
332
|
+
|
333
|
+
anim = GetComponent<Animator>();
|
334
|
+
|
335
|
+
// CapsuleColliderコンポーネントを取得する(カプセル型コリジョン)
|
336
|
+
|
337
|
+
col = GetComponent<CapsuleCollider>();
|
338
|
+
|
339
|
+
rb = GetComponent<Rigidbody>();
|
340
|
+
|
341
|
+
//メインカメラを取得する
|
342
|
+
|
343
|
+
//cameraObject = GameObject.FindWithTag ("MainCamera");
|
344
|
+
|
345
|
+
// CapsuleColliderコンポーネントのHeight、Centerの初期値を保存する
|
346
|
+
|
347
|
+
orgColHight = col.height;
|
348
|
+
|
349
|
+
orgVectColCenter = col.center;
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
void Update()
|
356
|
+
|
357
|
+
{
|
358
|
+
|
359
|
+
//範囲指定
|
360
|
+
|
361
|
+
float fPosX, fPosY;
|
362
|
+
|
363
|
+
fPosX = fPosY = 0.0f;
|
364
|
+
|
365
|
+
if (transform.position.x > 30.0f)
|
366
|
+
|
367
|
+
{
|
368
|
+
|
369
|
+
fPosX = 30.0f;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
else if (transform.position.x < -30.0f)
|
374
|
+
|
375
|
+
{
|
376
|
+
|
377
|
+
fPosX = -30.0f;
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
else
|
382
|
+
|
383
|
+
{
|
384
|
+
|
385
|
+
fPosX = transform.position.x;
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
if (transform.position.y < 0.55f)
|
390
|
+
|
391
|
+
{
|
392
|
+
|
393
|
+
fPosY = 0.55f;
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
else
|
398
|
+
|
399
|
+
{
|
400
|
+
|
401
|
+
fPosY = transform.position.y;
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
transform.position = new Vector3(fPosX, fPosY, transform.position.z);
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
// 以下、メイン処理.リジッドボディと絡めるので、FixedUpdate内で処理を行う.
|
414
|
+
|
415
|
+
void FixedUpdate ()
|
416
|
+
|
417
|
+
{
|
418
|
+
|
419
|
+
Vector3 moveVector = Vector3.zero; // 移動速度の入力
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
float h = Input.GetAxis ("Horizontal"); // 入力デバイスの水平軸をhで定義
|
424
|
+
|
425
|
+
float v = Input.GetAxis ("Vertical"); // 入力デバイスの垂直軸をvで定義
|
426
|
+
|
427
|
+
anim.SetFloat ("Speed", v); // Animator側で設定している"Speed"パラメタにvを渡す
|
428
|
+
|
429
|
+
anim.SetFloat ("Direction", h); // Animator側で設定している"Direction"パラメタにhを渡す
|
430
|
+
|
431
|
+
anim.speed = animSpeed; // Animatorのモーション再生速度に animSpeedを設定する
|
432
|
+
|
433
|
+
currentBaseState = anim.GetCurrentAnimatorStateInfo (0); // 参照用のステート変数にBase Layer (0)の現在のステートを設定する
|
434
|
+
|
435
|
+
rb.useGravity = true;//ジャンプ中に重力を切るので、それ以外は重力の影響を受けるようにする
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
// 以下、キャラクターの移動処理
|
444
|
+
|
445
|
+
velocity = new Vector3 (0, 0, v); // 上下のキー入力からZ軸方向の移動量を取得
|
446
|
+
|
447
|
+
// キャラクターのローカル空間での方向に変換
|
448
|
+
|
449
|
+
velocity = transform.TransformDirection (velocity);
|
450
|
+
|
451
|
+
//以下のvの閾値は、Mecanim側のトランジションと一緒に調整する
|
452
|
+
|
453
|
+
if (v > 0.1) {
|
454
|
+
|
455
|
+
velocity *= PlayerSpeed; // 移動速度を掛ける
|
456
|
+
|
457
|
+
} else if (v < -0.1) {
|
458
|
+
|
459
|
+
velocity *= PlayerSpeed; // 移動速度を掛ける
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
// 上下のキー入力でキャラクターを移動させる
|
468
|
+
|
469
|
+
transform.localPosition += velocity * Time.fixedDeltaTime;
|
470
|
+
|
471
|
+
// 左右のキー入力でキャラクタをY軸で旋回させる
|
472
|
+
|
473
|
+
transform.Rotate(0, h * rotateSpeed, 0);
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
if (isUseCameraDirection)
|
482
|
+
|
483
|
+
{
|
484
|
+
|
485
|
+
Vector3 cameraForward = mainCamera.transform.forward;
|
486
|
+
|
487
|
+
//Vector3 cameraRight = mainCamera.transform.right;
|
488
|
+
|
489
|
+
cameraForward.y = 0.0f; // 水平方向に移動させたい場合はy方向成分を0にする
|
490
|
+
|
491
|
+
//cameraRight.y = 0.0f;
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
moveVector = PlayerSpeed * (/*cameraRight.normalized * h +*/ cameraForward.normalized * v);
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
else
|
500
|
+
|
501
|
+
{
|
502
|
+
|
503
|
+
moveVector.x = PlayerSpeed * h;
|
504
|
+
|
505
|
+
moveVector.z = PlayerSpeed * v;
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
//Rigidbodyを使った慣性
|
512
|
+
|
513
|
+
rb.AddForce(moveForceMultiplier * (moveVector - rb.velocity));
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
// 以下、Animatorの各ステート中での処理
|
518
|
+
|
519
|
+
// Locomotion中
|
520
|
+
|
521
|
+
// 現在のベースレイヤーがlocoStateの時
|
522
|
+
|
523
|
+
if (currentBaseState.nameHash == locoState) {
|
524
|
+
|
525
|
+
//カーブでコライダ調整をしている時は、念のためにリセットする
|
526
|
+
|
527
|
+
if (useCurves) {
|
528
|
+
|
529
|
+
resetCollider ();
|
530
|
+
|
531
|
+
}
|
532
|
+
|
533
|
+
}
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
// IDLE中の処理
|
538
|
+
|
539
|
+
// 現在のベースレイヤーがidleStateの時
|
540
|
+
|
541
|
+
else if (currentBaseState.nameHash == idleState) {
|
542
|
+
|
543
|
+
//カーブでコライダ調整をしている時は、念のためにリセットする
|
544
|
+
|
545
|
+
if (useCurves) {
|
546
|
+
|
547
|
+
resetCollider ();
|
548
|
+
|
549
|
+
}
|
550
|
+
|
551
|
+
// スペースキーを入力したらRest状態になるs
|
552
|
+
|
553
|
+
if (Input.GetButtonDown ("Jump")) {
|
554
|
+
|
555
|
+
anim.SetBool ("Rest", true);
|
556
|
+
|
557
|
+
}
|
558
|
+
|
559
|
+
}
|
560
|
+
|
561
|
+
// REST中の処理
|
562
|
+
|
563
|
+
// 現在のベースレイヤーがrestStateの時
|
564
|
+
|
565
|
+
else if (currentBaseState.nameHash == restState) {
|
566
|
+
|
567
|
+
//cameraObject.SendMessage("setCameraPositionFrontView"); // カメラを正面に切り替える
|
568
|
+
|
569
|
+
// ステートが遷移中でない場合、Rest bool値をリセットする(ループしないようにする)
|
570
|
+
|
571
|
+
if (!anim.IsInTransition (0)) {
|
572
|
+
|
573
|
+
anim.SetBool ("Rest", false);
|
574
|
+
|
575
|
+
}
|
576
|
+
|
577
|
+
}
|
578
|
+
|
579
|
+
}
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
|
590
|
+
|
591
|
+
|
592
|
+
|
593
|
+
// キャラクターのコライダーサイズのリセット関数
|
594
|
+
|
595
|
+
void resetCollider ()
|
596
|
+
|
597
|
+
{
|
598
|
+
|
599
|
+
// コンポーネントのHeight、Centerの初期値を戻す
|
600
|
+
|
601
|
+
col.height = orgColHight;
|
602
|
+
|
603
|
+
col.center = orgVectColCenter;
|
604
|
+
|
605
|
+
}
|
606
|
+
|
607
|
+
}
|
608
|
+
|
609
|
+
}
|
610
|
+
|
611
|
+
```
|
612
|
+
|
197
613
|
|
198
614
|
|
199
615
|
### 試したこと
|
1
参考にしたサイトを貼り忘れていました
test
CHANGED
File without changes
|
test
CHANGED
@@ -202,7 +202,7 @@
|
|
202
202
|
|
203
203
|
下のサイトを参考にプレイヤーから標的に放物線を描いてとんでいくコードを書いた。
|
204
204
|
|
205
|
-
|
205
|
+
https://qiita.com/_udonba/items/a71e11c8dd039171f86c
|
206
206
|
|
207
207
|
### 補足情報(FW/ツールのバージョンなど)
|
208
208
|
|