質問編集履歴

1

モデルの挙動に関するScriptを追加しました。

2018/12/10 12:05

投稿

nishi835
nishi835

スコア15

test CHANGED
File without changes
test CHANGED
@@ -23,3 +23,503 @@
23
23
 
24
24
 
25
25
  ![イメージ説明](258d9f8b1146b299752fa629e22a05d4.png)
26
+
27
+
28
+
29
+ 少し長いですが、3Dモデルを動かしているスクリプトの関係のありそうな部分を添付いたします。
30
+
31
+
32
+
33
+ ```C#
34
+
35
+ void Update()
36
+
37
+ {
38
+
39
+ var cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; // カメラが追従するための動作
40
+
41
+ Vector3 direction = Vector3.zero;
42
+
43
+
44
+
45
+ // 地面に接地している時
46
+
47
+ if (CheckGrounded())
48
+
49
+ {
50
+
51
+ if (Input.GetAxis("Vertical01") == 0 && Input.GetAxis("Horizontal01") == 0) // テンキーや3Dスティックの入力(GetAxis)がゼロの時の動作
52
+
53
+ {
54
+
55
+ animCon.SetBool("Walk", false);
56
+
57
+ animCon.SetBool("Run", false);
58
+
59
+ }
60
+
61
+ else if (!(Input.GetButton("SideRight") || Input.GetButton("SideLeft")) && !(animCon.GetBool("FrontFlip") || animCon.GetBool("BackFlip")))
62
+
63
+ {
64
+
65
+ if (Input.GetButton("Dash"))
66
+
67
+ {
68
+
69
+ animCon.SetBool("Run", true);
70
+
71
+ dashTime += Time.deltaTime;
72
+
73
+
74
+
75
+ endurance -= consRate * Time.deltaTime;
76
+
77
+
78
+
79
+ if (Input.GetButtonDown("Slide"))
80
+
81
+ {
82
+
83
+ animCon.SetBool("Slide", true);
84
+
85
+ }
86
+
87
+ }
88
+
89
+ else
90
+
91
+ {
92
+
93
+ animCon.SetBool("Run", false);
94
+
95
+ animCon.SetBool("Walk", true);
96
+
97
+ dashTime = 0.0f;
98
+
99
+ }
100
+
101
+
102
+
103
+ // フリップ
104
+
105
+ if (Input.GetAxis("Flip") > 0)
106
+
107
+ {
108
+
109
+ animCon.SetBool("FrontFlip", true);
110
+
111
+ }
112
+
113
+ else if (Input.GetAxis("Flip") < 0)
114
+
115
+ {
116
+
117
+ animCon.SetBool("BackFlip", true);
118
+
119
+ }
120
+
121
+
122
+
123
+ direction = cameraForward * Input.GetAxis("Vertical01") + Camera.main.transform.right * Input.GetAxis("Horizontal01"); // テンキーや3Dスティックの入力(GetAxis)があるとdirectionに値を返す
124
+
125
+ Turn(direction); // 向きを変える動作の処理を実行する(後述)
126
+
127
+ }
128
+
129
+
130
+
131
+ moveDirection = direction * moveSpeed; //移動スピードを向いている方向に与える
132
+
133
+ moveDirection.y = 0f; //Y方向への速度をゼロにする
134
+
135
+
136
+
137
+ if (isWallJump)
138
+
139
+ {
140
+
141
+ animCon.SetBool("Idle", true);
142
+
143
+ isWallJump = false;
144
+
145
+ }
146
+
147
+
148
+
149
+ // 着地したらアニメーションパラメータのFallをfalseにする
150
+
151
+ animCon.SetBool("Fall", false);
152
+
153
+ animCon.SetBool("Jump", false);
154
+
155
+ animCon.SetBool("WallJump", false);
156
+
157
+
158
+
159
+ var input = new Vector3(Input.GetAxis("Horizontal01"), 0f, Input.GetAxis("Vertical01"));
160
+
161
+
162
+
163
+ if (input.magnitude > 0f)
164
+
165
+ {
166
+
167
+ // 着地後移動キーを押したら着地アニメーション終了
168
+
169
+ animCon.SetBool("Landing", false);
170
+
171
+ }
172
+
173
+
174
+
175
+ var stateInfo = animCon.GetCurrentAnimatorStateInfo(0);
176
+
177
+
178
+
179
+ // ジャンプ
180
+
181
+ if (Input.GetButtonDown("Jump") && !(Input.GetButton("SideRight") || Input.GetButton("SideLeft")) && !(animCon.GetBool("FrontFlip") || animCon.GetBool("BackFlip") || stateInfo.IsName("Roll")))
182
+
183
+ {
184
+
185
+ rb.AddForce(0, jumpPower, 0, ForceMode.Impulse);
186
+
187
+ isJump = true;
188
+
189
+ }
190
+
191
+
192
+
193
+ // ジャンプ力リセット
194
+
195
+ jumpPower = defaultJumpPower;
196
+
197
+ }
198
+
199
+ else // 空中なら
200
+
201
+ {
202
+
203
+ // ジャンプアクション後、空中に移動したらジャンプアニメーションを始める
204
+
205
+ if (isJump)
206
+
207
+ {
208
+
209
+ animCon.SetBool("Jump", true);
210
+
211
+ isJump = false;
212
+
213
+ }
214
+
215
+
216
+
217
+ //animCon.SetBool("isGrounded", false);
218
+
219
+ animCon.SetBool("Idle", false);
220
+
221
+
222
+
223
+ if (!animCon.GetBool("Fall")) // アニメーションパラメータFallがfalseの時で地面との距離が遠かったらFallをtrueにする
224
+
225
+ {
226
+
227
+ if (!Physics.SphereCast(new Ray(spine.position, Vector3.down), 0.5f, distanceToTheGround, LayerMask.GetMask("Field")))
228
+
229
+ {
230
+
231
+ animCon.SetBool("Fall", true);
232
+
233
+ }
234
+
235
+ }
236
+
237
+ else if (animCon.GetBool("Fall")) // 落下アニメーションの時はレイを飛ばし着地アニメーションにする
238
+
239
+ {
240
+
241
+ if (Physics.Linecast(spine.position, spine.position + Vector3.down * distanceToLanding, LayerMask.GetMask("Field")) && !isRoll)
242
+
243
+ {
244
+
245
+ animCon.SetBool("Landing", true);
246
+
247
+ }
248
+
249
+ }
250
+
251
+
252
+
253
+ if (Input.GetButtonDown("Jump"))
254
+
255
+ {
256
+
257
+ isRoll = true;
258
+
259
+ animCon.SetBool("Landing", false);
260
+
261
+ }
262
+
263
+
264
+
265
+ jumpPower += gravity * Time.deltaTime; // 時間経過とともに壁ジャンプ力を増す
266
+
267
+ }
268
+
269
+
270
+
271
+ if (Input.GetButtonDown("Action"))
272
+
273
+ {
274
+
275
+ if (rader.CanHang())
276
+
277
+ {
278
+
279
+ //Rayの作成       ↓Rayを飛ばす原点   ↓Rayを飛ばす方向
280
+
281
+ Ray ray = new Ray(transform.position, new Vector3(0, -1, 0));
282
+
283
+
284
+
285
+ //Rayが当たったオブジェクトの情報を入れる箱
286
+
287
+ RaycastHit hit;
288
+
289
+
290
+
291
+ //Rayの飛ばせる距離
292
+
293
+ int distance = 10;
294
+
295
+ Physics.Raycast(ray, out hit, distance);
296
+
297
+
298
+
299
+ float h_angle = hit.transform.eulerAngles.y;
300
+
301
+ float p_angle = transform.eulerAngles.y;
302
+
303
+
304
+
305
+ for (int i = 0; i < 4; i++)
306
+
307
+ {
308
+
309
+ h_angle -= 90 * i;
310
+
311
+ if (h_angle < 0) { h_angle = 360 + h_angle; }
312
+
313
+ float diff = Mathf.Abs(h_angle - p_angle);
314
+
315
+ if (diff > 180) { diff = 360 - diff; }
316
+
317
+ if (diff <= 45)
318
+
319
+ {
320
+
321
+ transform.rotation = Quaternion.Euler(0.0f, h_angle, 0.0f);
322
+
323
+ break;
324
+
325
+ }
326
+
327
+ }
328
+
329
+
330
+
331
+ animCon.SetBool("HangWall", true);
332
+
333
+
334
+
335
+ StartCoroutine(DelayMethod(0.65f, () =>
336
+
337
+ {
338
+
339
+ transform.position = Vector3.Lerp(transform.position, transform.position + new Vector3(0.0f, 0.0f, -0.15f), 1.0f);
340
+
341
+ }));
342
+
343
+ }
344
+
345
+ }
346
+
347
+
348
+
349
+ if (animCon.GetBool("HangWall"))
350
+
351
+ {
352
+
353
+ Physics.gravity = new Vector3(0.0f, 0.0f, 0.0f);
354
+
355
+ }
356
+
357
+ else
358
+
359
+ {
360
+
361
+ Physics.gravity = new Vector3(0.0f, defaultGravity, 0.0f);
362
+
363
+ }
364
+
365
+
366
+
367
+ switch(animClip)
368
+
369
+ {
370
+
371
+ case AnimClip.GetHit:
372
+
373
+ if(animCon.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f)
374
+
375
+ {
376
+
377
+ animClip = AnimClip.Idle;
378
+
379
+ animCon.CrossFadeInFixedTime(AnimClip.Idle.ToString(), 0.5f);
380
+
381
+ }
382
+
383
+ break;
384
+
385
+ }
386
+
387
+
388
+
389
+ // 移動
390
+
391
+ //charaCon.Move(moveDirection * Time.deltaTime); //CharacterControllerの付いているこのオブジェクトを移動させる処理
392
+
393
+ rb.AddForce(moveDirection.x * moveSpeed, 0, moveDirection.z * moveSpeed);
394
+
395
+ }
396
+
397
+
398
+
399
+ // 汎用コルーチン
400
+
401
+ private IEnumerator DelayMethod(float waitTime, Action action)
402
+
403
+ {
404
+
405
+ yield return new WaitForSeconds(waitTime);
406
+
407
+ action();
408
+
409
+ }
410
+
411
+
412
+
413
+ // 向きを変える動作の処理
414
+
415
+ void Turn(Vector3 direction)
416
+
417
+ {
418
+
419
+ Quaternion q = Quaternion.LookRotation(direction); // 向きたい方角をQuaternion型に直す
420
+
421
+ transform.rotation = Quaternion.RotateTowards(transform.rotation, q, turnSpeed * Time.deltaTime); // 向きを q に向けてじわ~っと変化させる.
422
+
423
+ }
424
+
425
+
426
+
427
+ // スタミナを返す
428
+
429
+ public int Endurance()
430
+
431
+ {
432
+
433
+ return (int)endurance;
434
+
435
+ }
436
+
437
+
438
+
439
+ // 着地判定
440
+
441
+ public bool CheckGrounded()
442
+
443
+ {
444
+
445
+ //CharacterControlle.IsGroundedがtrueならRaycastを使わずに判定終了
446
+
447
+ //if (charaCon.isGrounded) { return true; }
448
+
449
+
450
+
451
+ //放つ光線の初期位置と姿勢
452
+
453
+ //若干身体にめり込ませた位置から発射しないと正しく判定できない時がある
454
+
455
+ var ray = new Ray(this.transform.position + new Vector3(0.0f, 0.1f, -0.1f), Vector3.down);
456
+
457
+
458
+
459
+ //探索距離
460
+
461
+ var tolerance = 0.3f;
462
+
463
+
464
+
465
+ //Raycastがhitするかどうかで判定
466
+
467
+ //地面にのみ衝突するようにレイヤを指定する
468
+
469
+ return Physics.Raycast(ray, tolerance, LayerMask.GetMask("Field"));
470
+
471
+ }
472
+
473
+
474
+
475
+ // 壁ジャンプ
476
+
477
+ private void OnControllerColliderHit(ControllerColliderHit hit)
478
+
479
+ {
480
+
481
+ if (!CheckGrounded())
482
+
483
+ //if (!charaCon.isGrounded)
484
+
485
+ {
486
+
487
+ if (hit.normal.y < 0.1f)
488
+
489
+ {
490
+
491
+ if (Input.GetButtonDown("Jump"))
492
+
493
+ {
494
+
495
+ animCon.SetBool("WallJump", true);
496
+
497
+ isWallJump = true;
498
+
499
+
500
+
501
+ float angle = Mathf.Atan2(hit.normal.x, hit.normal.z) * Mathf.Rad2Deg;
502
+
503
+ transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
504
+
505
+ moveDirection.x = hit.normal.x * moveSpeed;
506
+
507
+ moveDirection.z = hit.normal.z * moveSpeed;
508
+
509
+ moveDirection.y = jumpPower;
510
+
511
+ }
512
+
513
+ }
514
+
515
+ }
516
+
517
+ }
518
+
519
+
520
+
521
+ }
522
+
523
+
524
+
525
+ ```