質問編集履歴
9
mis
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,610 +124,168 @@
|
|
124
124
|
|
125
125
|
```
|
126
126
|
|
127
|
-
|
127
|
+
|
128
|
+
|
129
|
+
|
128
130
|
|
129
131
|
```C#
|
130
132
|
|
133
|
+
using System.Collections;
|
134
|
+
|
135
|
+
using System.Collections.Generic;
|
136
|
+
|
131
137
|
using UnityEngine;
|
132
138
|
|
133
139
|
using UnityEngine.UI;
|
134
140
|
|
141
|
+
|
142
|
+
|
143
|
+
GameObject Player;
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
public class OniController : MonoBehaviour
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
GameObject Player;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
void Start()
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
this.Player = GameObject.Find("Player");
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
public void OnCollisionEnter(Collider hit)
|
176
|
+
|
177
|
+
{
|
178
|
+
|
179
|
+
if (hit.CompareTag("Player"))
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
int z = Random.Range(63, 64);
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
this.Player.transform.position = new Vector3(4, 0, z);
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
訂正したスクリプト
|
212
|
+
|
213
|
+
```C#
|
214
|
+
|
135
215
|
using System.Collections;
|
136
216
|
|
137
217
|
using System.Collections.Generic;
|
138
218
|
|
139
|
-
|
219
|
+
using UnityEngine;
|
220
|
+
|
140
|
-
|
221
|
+
using UnityEngine.UI;
|
222
|
+
|
223
|
+
|
224
|
+
|
141
|
-
|
225
|
+
GameObject Player;
|
142
|
-
|
226
|
+
|
227
|
+
|
228
|
+
|
143
|
-
|
229
|
+
public class OniController : MonoBehaviour
|
144
230
|
|
145
231
|
{
|
146
232
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
float m_CapsuleHeight;
|
194
|
-
|
195
|
-
Vector3 m_CapsuleCenter;
|
196
|
-
|
197
|
-
CapsuleCollider m_Capsule;
|
198
|
-
|
199
|
-
bool m_Crouching;
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
void Start()
|
210
|
-
|
211
|
-
{
|
212
|
-
|
213
|
-
m_Animator = GetComponent<Animator>();
|
214
|
-
|
215
|
-
m_Rigidbody = GetComponent<Rigidbody>();
|
216
|
-
|
217
|
-
m_Capsule = GetComponent<CapsuleCollider>();
|
218
|
-
|
219
|
-
m_CapsuleHeight = m_Capsule.height;
|
220
|
-
|
221
|
-
m_CapsuleCenter = m_Capsule.center;
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
|
226
|
-
|
227
|
-
m_OrigGroundCheckDistance = m_GroundCheckDistance;
|
233
|
+
|
234
|
+
|
235
|
+
GameObject Player;
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
void Start()
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
this.Player = GameObject.Find("Player");
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
public void OnCollisionEnter(Collision hit)
|
258
|
+
|
259
|
+
{
|
260
|
+
|
261
|
+
if (hit.gameObject.tag == "Player"))
|
262
|
+
|
263
|
+
{
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
int z = Random.Range(63, 64);
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
this.Player.transform.position = new Vector3(4, 0, z);
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
228
278
|
|
229
279
|
}
|
230
280
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
{
|
240
|
-
|
241
|
-
// 接触対象はPlayerタグですか?
|
242
|
-
|
243
|
-
if (hit.CompareTag("Ham"))
|
244
|
-
|
245
|
-
{
|
246
|
-
|
247
|
-
m_MoveSpeedMultiplier = 5f;
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
Invoke("SpeedDown", 5);
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
public void SpeedDown()
|
262
|
-
|
263
|
-
{
|
264
|
-
|
265
|
-
m_MoveSpeedMultiplier = 2.0f;
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
public void Move(Vector3 move, bool crouch, bool jump)
|
278
|
-
|
279
|
-
{
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
// convert the world relative moveInput vector into a local-relative
|
286
|
-
|
287
|
-
// turn amount and forward amount required to head in the desired
|
288
|
-
|
289
|
-
// direction.
|
290
|
-
|
291
|
-
if (move.magnitude > 1f) move.Normalize();
|
292
|
-
|
293
|
-
move = transform.InverseTransformDirection(move);
|
294
|
-
|
295
|
-
CheckGroundStatus();
|
296
|
-
|
297
|
-
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
|
298
|
-
|
299
|
-
m_TurnAmount = Mathf.Atan2(move.x, move.z);
|
300
|
-
|
301
|
-
m_ForwardAmount = move.z;
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
ApplyExtraTurnRotation();
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
// control and velocity handling is different when grounded and airborne:
|
310
|
-
|
311
|
-
if (m_IsGrounded)
|
312
|
-
|
313
|
-
{
|
314
|
-
|
315
|
-
HandleGroundedMovement(crouch, jump);
|
316
|
-
|
317
|
-
}
|
318
|
-
|
319
|
-
else
|
320
|
-
|
321
|
-
{
|
322
|
-
|
323
|
-
HandleAirborneMovement();
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
ScaleCapsuleForCrouching(crouch);
|
330
|
-
|
331
|
-
PreventStandingInLowHeadroom();
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
// send input and other state parameters to the animator
|
336
|
-
|
337
|
-
UpdateAnimator(move);
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
void ScaleCapsuleForCrouching(bool crouch)
|
346
|
-
|
347
|
-
{
|
348
|
-
|
349
|
-
if (m_IsGrounded && crouch)
|
350
|
-
|
351
|
-
{
|
352
|
-
|
353
|
-
if (m_Crouching) return;
|
354
|
-
|
355
|
-
m_Capsule.height = m_Capsule.height / 2f;
|
356
|
-
|
357
|
-
m_Capsule.center = m_Capsule.center / 2f;
|
358
|
-
|
359
|
-
m_Crouching = true;
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
else
|
364
|
-
|
365
|
-
{
|
366
|
-
|
367
|
-
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
|
368
|
-
|
369
|
-
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
|
370
|
-
|
371
|
-
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
|
372
|
-
|
373
|
-
{
|
374
|
-
|
375
|
-
m_Crouching = true;
|
376
|
-
|
377
|
-
return;
|
378
|
-
|
379
|
-
}
|
380
|
-
|
381
|
-
m_Capsule.height = m_CapsuleHeight;
|
382
|
-
|
383
|
-
m_Capsule.center = m_CapsuleCenter;
|
384
|
-
|
385
|
-
m_Crouching = false;
|
386
|
-
|
387
|
-
}
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
void PreventStandingInLowHeadroom()
|
394
|
-
|
395
|
-
{
|
396
|
-
|
397
|
-
// prevent standing up in crouch-only zones
|
398
|
-
|
399
|
-
if (!m_Crouching)
|
400
|
-
|
401
|
-
{
|
402
|
-
|
403
|
-
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
|
404
|
-
|
405
|
-
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
|
406
|
-
|
407
|
-
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
|
408
|
-
|
409
|
-
{
|
410
|
-
|
411
|
-
m_Crouching = true;
|
412
|
-
|
413
|
-
}
|
414
|
-
|
415
|
-
}
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
void UpdateAnimator(Vector3 move)
|
424
|
-
|
425
|
-
{
|
426
|
-
|
427
|
-
// update the animator parameters
|
428
|
-
|
429
|
-
m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
|
430
|
-
|
431
|
-
m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
|
432
|
-
|
433
|
-
m_Animator.SetBool("Crouch", m_Crouching);
|
434
|
-
|
435
|
-
m_Animator.SetBool("OnGround", m_IsGrounded);
|
436
|
-
|
437
|
-
if (!m_IsGrounded)
|
438
|
-
|
439
|
-
{
|
440
|
-
|
441
|
-
m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y);
|
442
|
-
|
443
|
-
}
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
// calculate which leg is behind, so as to leave that leg trailing in the jump animation
|
448
|
-
|
449
|
-
// (This code is reliant on the specific run cycle offset in our animations,
|
450
|
-
|
451
|
-
// and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5)
|
452
|
-
|
453
|
-
float runCycle =
|
454
|
-
|
455
|
-
Mathf.Repeat(
|
456
|
-
|
457
|
-
m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1);
|
458
|
-
|
459
|
-
float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount;
|
460
|
-
|
461
|
-
if (m_IsGrounded)
|
462
|
-
|
463
|
-
{
|
464
|
-
|
465
|
-
m_Animator.SetFloat("JumpLeg", jumpLeg);
|
466
|
-
|
467
|
-
}
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
// the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector,
|
472
|
-
|
473
|
-
// which affects the movement speed because of the root motion.
|
474
|
-
|
475
|
-
if (m_IsGrounded && move.magnitude > 0)
|
476
|
-
|
477
|
-
{
|
478
|
-
|
479
|
-
m_Animator.speed = m_AnimSpeedMultiplier;
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
}
|
486
|
-
|
487
|
-
else
|
488
|
-
|
489
|
-
{
|
490
|
-
|
491
|
-
// don't use that while airborne
|
492
|
-
|
493
|
-
m_Animator.speed = 1;
|
494
|
-
|
495
|
-
}
|
496
|
-
|
497
|
-
}
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
void HandleAirborneMovement()
|
504
|
-
|
505
|
-
{
|
506
|
-
|
507
|
-
// apply extra gravity from multiplier:
|
508
|
-
|
509
|
-
Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity;
|
510
|
-
|
511
|
-
m_Rigidbody.AddForce(extraGravityForce);
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f;
|
516
|
-
|
517
|
-
}
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
void HandleGroundedMovement(bool crouch, bool jump)
|
524
|
-
|
525
|
-
{
|
526
|
-
|
527
|
-
// check whether conditions are right to allow a jump:
|
528
|
-
|
529
|
-
if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded"))
|
530
|
-
|
531
|
-
{
|
532
|
-
|
533
|
-
// jump!
|
534
|
-
|
535
|
-
m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
|
536
|
-
|
537
|
-
m_IsGrounded = false;
|
538
|
-
|
539
|
-
m_Animator.applyRootMotion = false;
|
540
|
-
|
541
|
-
m_GroundCheckDistance = 0.1f;
|
542
|
-
|
543
|
-
}
|
544
|
-
|
545
|
-
}
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
void ApplyExtraTurnRotation()
|
550
|
-
|
551
|
-
{
|
552
|
-
|
553
|
-
// help the character turn faster (this is in addition to root rotation in the animation)
|
554
|
-
|
555
|
-
float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
|
556
|
-
|
557
|
-
transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
|
558
|
-
|
559
|
-
}
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
public void OnAnimatorMove()
|
566
|
-
|
567
|
-
{
|
568
|
-
|
569
|
-
// we implement this function to override the default root motion.
|
570
|
-
|
571
|
-
// this allows us to modify the positional speed before it's applied.
|
572
|
-
|
573
|
-
if (m_IsGrounded && Time.deltaTime > 0)
|
574
|
-
|
575
|
-
{
|
576
|
-
|
577
|
-
Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
// we preserve the existing y part of the current velocity.
|
582
|
-
|
583
|
-
v.y = m_Rigidbody.velocity.y;
|
584
|
-
|
585
|
-
m_Rigidbody.velocity = v;
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
}
|
594
|
-
|
595
|
-
}
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
void CheckGroundStatus()
|
602
|
-
|
603
|
-
{
|
604
|
-
|
605
|
-
RaycastHit hitInfo;
|
606
|
-
|
607
|
-
#if UNITY_EDITOR
|
608
|
-
|
609
|
-
// helper to visualise the ground check ray in the scene view
|
610
|
-
|
611
|
-
Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
|
612
|
-
|
613
|
-
#endif
|
614
|
-
|
615
|
-
// 0.1f is a small offset to start the ray from inside the character
|
616
|
-
|
617
|
-
// it is also good to note that the transform position in the sample assets is at the base of the character
|
618
|
-
|
619
|
-
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance))
|
620
|
-
|
621
|
-
{
|
622
|
-
|
623
|
-
m_GroundNormal = hitInfo.normal;
|
624
|
-
|
625
|
-
m_IsGrounded = true;
|
626
|
-
|
627
|
-
m_Animator.applyRootMotion = true;
|
628
|
-
|
629
|
-
}
|
630
|
-
|
631
|
-
else
|
632
|
-
|
633
|
-
{
|
634
|
-
|
635
|
-
m_IsGrounded = false;
|
636
|
-
|
637
|
-
m_GroundNormal = Vector3.up;
|
638
|
-
|
639
|
-
m_Animator.applyRootMotion = false;
|
640
|
-
|
641
|
-
}
|
642
|
-
|
643
|
-
}
|
644
|
-
|
645
|
-
}
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
650
288
|
|
651
289
|
}
|
652
290
|
|
653
|
-
|
654
|
-
|
655
|
-
```C#
|
656
|
-
|
657
|
-
using System.Collections;
|
658
|
-
|
659
|
-
using System.Collections.Generic;
|
660
|
-
|
661
|
-
using UnityEngine;
|
662
|
-
|
663
|
-
using UnityEngine.UI;
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
GameObject Player;
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
public class OniController : MonoBehaviour
|
672
|
-
|
673
|
-
{
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
GameObject Player;
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
void Start()
|
682
|
-
|
683
|
-
{
|
684
|
-
|
685
|
-
this.Player = GameObject.Find("Player");
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
}
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
public void OnCollisionEnter(Collider hit)
|
700
|
-
|
701
|
-
{
|
702
|
-
|
703
|
-
if (hit.CompareTag("Player"))
|
704
|
-
|
705
|
-
{
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
int z = Random.Range(63, 64);
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
this.Player.transform.position = new Vector3(4, 0, z);
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
}
|
722
|
-
|
723
|
-
}
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
}
|
732
|
-
|
733
291
|
```
|
8
m
test
CHANGED
File without changes
|
test
CHANGED
@@ -652,6 +652,82 @@
|
|
652
652
|
|
653
653
|
|
654
654
|
|
655
|
-
|
655
|
+
```C#
|
656
|
+
|
657
|
+
using System.Collections;
|
658
|
+
|
659
|
+
using System.Collections.Generic;
|
660
|
+
|
661
|
+
using UnityEngine;
|
662
|
+
|
663
|
+
using UnityEngine.UI;
|
664
|
+
|
665
|
+
|
666
|
+
|
667
|
+
GameObject Player;
|
668
|
+
|
669
|
+
|
670
|
+
|
671
|
+
public class OniController : MonoBehaviour
|
672
|
+
|
673
|
+
{
|
674
|
+
|
675
|
+
|
676
|
+
|
677
|
+
GameObject Player;
|
678
|
+
|
679
|
+
|
680
|
+
|
681
|
+
void Start()
|
682
|
+
|
683
|
+
{
|
684
|
+
|
685
|
+
this.Player = GameObject.Find("Player");
|
686
|
+
|
687
|
+
|
688
|
+
|
689
|
+
|
690
|
+
|
691
|
+
|
692
|
+
|
693
|
+
}
|
694
|
+
|
695
|
+
|
696
|
+
|
697
|
+
|
698
|
+
|
699
|
+
public void OnCollisionEnter(Collider hit)
|
700
|
+
|
701
|
+
{
|
702
|
+
|
703
|
+
if (hit.CompareTag("Player"))
|
704
|
+
|
705
|
+
{
|
706
|
+
|
707
|
+
|
708
|
+
|
709
|
+
|
710
|
+
|
711
|
+
int z = Random.Range(63, 64);
|
712
|
+
|
713
|
+
|
714
|
+
|
715
|
+
this.Player.transform.position = new Vector3(4, 0, z);
|
716
|
+
|
717
|
+
|
718
|
+
|
719
|
+
|
720
|
+
|
721
|
+
}
|
722
|
+
|
723
|
+
}
|
724
|
+
|
725
|
+
|
726
|
+
|
727
|
+
|
728
|
+
|
729
|
+
|
730
|
+
|
731
|
+
}
|
656
732
|
|
657
733
|
```
|
7
miiss
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
プレイヤー同士の当たり判定について
|
test
CHANGED
File without changes
|
6
miss
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,6 +38,10 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
+
|
42
|
+
|
43
|
+
Oni側のスクリプト
|
44
|
+
|
41
45
|
```C#
|
42
46
|
|
43
47
|
using System.Collections;
|
@@ -120,6 +124,8 @@
|
|
120
124
|
|
121
125
|
```
|
122
126
|
|
127
|
+
プレイヤーの操作のためのスクリプト
|
128
|
+
|
123
129
|
```C#
|
124
130
|
|
125
131
|
using UnityEngine;
|
5
miss
test
CHANGED
File without changes
|
test
CHANGED
@@ -119,3 +119,533 @@
|
|
119
119
|
|
120
120
|
|
121
121
|
```
|
122
|
+
|
123
|
+
```C#
|
124
|
+
|
125
|
+
using UnityEngine;
|
126
|
+
|
127
|
+
using UnityEngine.UI;
|
128
|
+
|
129
|
+
using System.Collections;
|
130
|
+
|
131
|
+
using System.Collections.Generic;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
namespace UnityStandardAssets.Characters.ThirdPerson
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
[RequireComponent(typeof(Rigidbody))]
|
142
|
+
|
143
|
+
[RequireComponent(typeof(CapsuleCollider))]
|
144
|
+
|
145
|
+
[RequireComponent(typeof(Animator))]
|
146
|
+
|
147
|
+
public class ThirdPersonCharacter : MonoBehaviour
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
[SerializeField] float m_MovingTurnSpeed = 360;
|
152
|
+
|
153
|
+
[SerializeField] float m_StationaryTurnSpeed = 180;
|
154
|
+
|
155
|
+
[SerializeField] float m_JumpPower = 12f;
|
156
|
+
|
157
|
+
[Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f;
|
158
|
+
|
159
|
+
[SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
float m_MoveSpeedMultiplier = 2.5f;
|
164
|
+
|
165
|
+
[SerializeField] float m_AnimSpeedMultiplier = 1f;
|
166
|
+
|
167
|
+
[SerializeField] float m_GroundCheckDistance = 0.1f;
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
Rigidbody m_Rigidbody;
|
172
|
+
|
173
|
+
Animator m_Animator;
|
174
|
+
|
175
|
+
bool m_IsGrounded;
|
176
|
+
|
177
|
+
float m_OrigGroundCheckDistance;
|
178
|
+
|
179
|
+
const float k_Half = 0.5f;
|
180
|
+
|
181
|
+
float m_TurnAmount;
|
182
|
+
|
183
|
+
float m_ForwardAmount;
|
184
|
+
|
185
|
+
Vector3 m_GroundNormal;
|
186
|
+
|
187
|
+
float m_CapsuleHeight;
|
188
|
+
|
189
|
+
Vector3 m_CapsuleCenter;
|
190
|
+
|
191
|
+
CapsuleCollider m_Capsule;
|
192
|
+
|
193
|
+
bool m_Crouching;
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
void Start()
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
m_Animator = GetComponent<Animator>();
|
208
|
+
|
209
|
+
m_Rigidbody = GetComponent<Rigidbody>();
|
210
|
+
|
211
|
+
m_Capsule = GetComponent<CapsuleCollider>();
|
212
|
+
|
213
|
+
m_CapsuleHeight = m_Capsule.height;
|
214
|
+
|
215
|
+
m_CapsuleCenter = m_Capsule.center;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
|
220
|
+
|
221
|
+
m_OrigGroundCheckDistance = m_GroundCheckDistance;
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
void OnTriggerEnter(Collider hit)
|
232
|
+
|
233
|
+
{
|
234
|
+
|
235
|
+
// 接触対象はPlayerタグですか?
|
236
|
+
|
237
|
+
if (hit.CompareTag("Ham"))
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
m_MoveSpeedMultiplier = 5f;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
Invoke("SpeedDown", 5);
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
public void SpeedDown()
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
m_MoveSpeedMultiplier = 2.0f;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
public void Move(Vector3 move, bool crouch, bool jump)
|
272
|
+
|
273
|
+
{
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
// convert the world relative moveInput vector into a local-relative
|
280
|
+
|
281
|
+
// turn amount and forward amount required to head in the desired
|
282
|
+
|
283
|
+
// direction.
|
284
|
+
|
285
|
+
if (move.magnitude > 1f) move.Normalize();
|
286
|
+
|
287
|
+
move = transform.InverseTransformDirection(move);
|
288
|
+
|
289
|
+
CheckGroundStatus();
|
290
|
+
|
291
|
+
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
|
292
|
+
|
293
|
+
m_TurnAmount = Mathf.Atan2(move.x, move.z);
|
294
|
+
|
295
|
+
m_ForwardAmount = move.z;
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
ApplyExtraTurnRotation();
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
// control and velocity handling is different when grounded and airborne:
|
304
|
+
|
305
|
+
if (m_IsGrounded)
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
HandleGroundedMovement(crouch, jump);
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
else
|
314
|
+
|
315
|
+
{
|
316
|
+
|
317
|
+
HandleAirborneMovement();
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
ScaleCapsuleForCrouching(crouch);
|
324
|
+
|
325
|
+
PreventStandingInLowHeadroom();
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
// send input and other state parameters to the animator
|
330
|
+
|
331
|
+
UpdateAnimator(move);
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
void ScaleCapsuleForCrouching(bool crouch)
|
340
|
+
|
341
|
+
{
|
342
|
+
|
343
|
+
if (m_IsGrounded && crouch)
|
344
|
+
|
345
|
+
{
|
346
|
+
|
347
|
+
if (m_Crouching) return;
|
348
|
+
|
349
|
+
m_Capsule.height = m_Capsule.height / 2f;
|
350
|
+
|
351
|
+
m_Capsule.center = m_Capsule.center / 2f;
|
352
|
+
|
353
|
+
m_Crouching = true;
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
else
|
358
|
+
|
359
|
+
{
|
360
|
+
|
361
|
+
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
|
362
|
+
|
363
|
+
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
|
364
|
+
|
365
|
+
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
|
366
|
+
|
367
|
+
{
|
368
|
+
|
369
|
+
m_Crouching = true;
|
370
|
+
|
371
|
+
return;
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
m_Capsule.height = m_CapsuleHeight;
|
376
|
+
|
377
|
+
m_Capsule.center = m_CapsuleCenter;
|
378
|
+
|
379
|
+
m_Crouching = false;
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
void PreventStandingInLowHeadroom()
|
388
|
+
|
389
|
+
{
|
390
|
+
|
391
|
+
// prevent standing up in crouch-only zones
|
392
|
+
|
393
|
+
if (!m_Crouching)
|
394
|
+
|
395
|
+
{
|
396
|
+
|
397
|
+
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
|
398
|
+
|
399
|
+
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
|
400
|
+
|
401
|
+
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
|
402
|
+
|
403
|
+
{
|
404
|
+
|
405
|
+
m_Crouching = true;
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
void UpdateAnimator(Vector3 move)
|
418
|
+
|
419
|
+
{
|
420
|
+
|
421
|
+
// update the animator parameters
|
422
|
+
|
423
|
+
m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
|
424
|
+
|
425
|
+
m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
|
426
|
+
|
427
|
+
m_Animator.SetBool("Crouch", m_Crouching);
|
428
|
+
|
429
|
+
m_Animator.SetBool("OnGround", m_IsGrounded);
|
430
|
+
|
431
|
+
if (!m_IsGrounded)
|
432
|
+
|
433
|
+
{
|
434
|
+
|
435
|
+
m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y);
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
// calculate which leg is behind, so as to leave that leg trailing in the jump animation
|
442
|
+
|
443
|
+
// (This code is reliant on the specific run cycle offset in our animations,
|
444
|
+
|
445
|
+
// and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5)
|
446
|
+
|
447
|
+
float runCycle =
|
448
|
+
|
449
|
+
Mathf.Repeat(
|
450
|
+
|
451
|
+
m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1);
|
452
|
+
|
453
|
+
float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount;
|
454
|
+
|
455
|
+
if (m_IsGrounded)
|
456
|
+
|
457
|
+
{
|
458
|
+
|
459
|
+
m_Animator.SetFloat("JumpLeg", jumpLeg);
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
// the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector,
|
466
|
+
|
467
|
+
// which affects the movement speed because of the root motion.
|
468
|
+
|
469
|
+
if (m_IsGrounded && move.magnitude > 0)
|
470
|
+
|
471
|
+
{
|
472
|
+
|
473
|
+
m_Animator.speed = m_AnimSpeedMultiplier;
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
else
|
482
|
+
|
483
|
+
{
|
484
|
+
|
485
|
+
// don't use that while airborne
|
486
|
+
|
487
|
+
m_Animator.speed = 1;
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
void HandleAirborneMovement()
|
498
|
+
|
499
|
+
{
|
500
|
+
|
501
|
+
// apply extra gravity from multiplier:
|
502
|
+
|
503
|
+
Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity;
|
504
|
+
|
505
|
+
m_Rigidbody.AddForce(extraGravityForce);
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f;
|
510
|
+
|
511
|
+
}
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
void HandleGroundedMovement(bool crouch, bool jump)
|
518
|
+
|
519
|
+
{
|
520
|
+
|
521
|
+
// check whether conditions are right to allow a jump:
|
522
|
+
|
523
|
+
if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded"))
|
524
|
+
|
525
|
+
{
|
526
|
+
|
527
|
+
// jump!
|
528
|
+
|
529
|
+
m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
|
530
|
+
|
531
|
+
m_IsGrounded = false;
|
532
|
+
|
533
|
+
m_Animator.applyRootMotion = false;
|
534
|
+
|
535
|
+
m_GroundCheckDistance = 0.1f;
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
void ApplyExtraTurnRotation()
|
544
|
+
|
545
|
+
{
|
546
|
+
|
547
|
+
// help the character turn faster (this is in addition to root rotation in the animation)
|
548
|
+
|
549
|
+
float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
|
550
|
+
|
551
|
+
transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
|
552
|
+
|
553
|
+
}
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
public void OnAnimatorMove()
|
560
|
+
|
561
|
+
{
|
562
|
+
|
563
|
+
// we implement this function to override the default root motion.
|
564
|
+
|
565
|
+
// this allows us to modify the positional speed before it's applied.
|
566
|
+
|
567
|
+
if (m_IsGrounded && Time.deltaTime > 0)
|
568
|
+
|
569
|
+
{
|
570
|
+
|
571
|
+
Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
// we preserve the existing y part of the current velocity.
|
576
|
+
|
577
|
+
v.y = m_Rigidbody.velocity.y;
|
578
|
+
|
579
|
+
m_Rigidbody.velocity = v;
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
}
|
588
|
+
|
589
|
+
}
|
590
|
+
|
591
|
+
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
void CheckGroundStatus()
|
596
|
+
|
597
|
+
{
|
598
|
+
|
599
|
+
RaycastHit hitInfo;
|
600
|
+
|
601
|
+
#if UNITY_EDITOR
|
602
|
+
|
603
|
+
// helper to visualise the ground check ray in the scene view
|
604
|
+
|
605
|
+
Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
|
606
|
+
|
607
|
+
#endif
|
608
|
+
|
609
|
+
// 0.1f is a small offset to start the ray from inside the character
|
610
|
+
|
611
|
+
// it is also good to note that the transform position in the sample assets is at the base of the character
|
612
|
+
|
613
|
+
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance))
|
614
|
+
|
615
|
+
{
|
616
|
+
|
617
|
+
m_GroundNormal = hitInfo.normal;
|
618
|
+
|
619
|
+
m_IsGrounded = true;
|
620
|
+
|
621
|
+
m_Animator.applyRootMotion = true;
|
622
|
+
|
623
|
+
}
|
624
|
+
|
625
|
+
else
|
626
|
+
|
627
|
+
{
|
628
|
+
|
629
|
+
m_IsGrounded = false;
|
630
|
+
|
631
|
+
m_GroundNormal = Vector3.up;
|
632
|
+
|
633
|
+
m_Animator.applyRootMotion = false;
|
634
|
+
|
635
|
+
}
|
636
|
+
|
637
|
+
}
|
638
|
+
|
639
|
+
}
|
640
|
+
|
641
|
+
|
642
|
+
|
643
|
+
|
644
|
+
|
645
|
+
}
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
コード
|
650
|
+
|
651
|
+
```
|
4
miss
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
プレイヤー同士の当たり判定について
|
1
|
+
missプレイヤー同士の当たり判定について
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
その際に鬼に捕まった際にはプレイヤーが所定の場所に移動するというスクリプトを書きました。
|
18
18
|
|
19
|
-
プレイヤーのコライダーをtrueにしておくと落下するので、Playerの子要素(Playerとobject名を統一させました。)にコライダーを加え、トリガーをtrueにし、タグをPlayerとすることで実装を行いました。
|
19
|
+
RigidBodyがアタッチされているプレイヤーのコライダーのトリガーをtrueにしておくと落下するので、Playerの子要素(Playerとobject名を統一させました。)にもコライダーを加え、トリガーをtrueにし、タグをPlayerとすることで実装を行いました。
|
20
20
|
|
21
21
|
|
22
22
|
|
3
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
この落下の解決方法につきましてアドバイス
|
35
|
+
この落下の解決方法につきましてアドバイスをいただけますと幸いです。
|
36
36
|
|
37
37
|
よろしくお願いいたします。
|
38
38
|
|
2
miss
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
|
27
27
|
プレーヤー側にスクリプトを組みiskinematicをtureにすることで落下を防ぐことも考えましたが
|
28
28
|
|
29
|
-
鬼が
|
29
|
+
鬼がPlayerに追いついて、ボタンを押した際にPlayerを所定の場所に送るように書き変える予定であることと、
|
30
30
|
|
31
31
|
あるタイミングでもう一度復活をするようなゲームにしたいと考えているので、その方法では実装できませんでした。
|
32
32
|
|
1
ごじ
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,10 @@
|
|
1
1
|
unity3Dで鬼ごっこのゲームを作成しています。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
プレイヤー同士がぶつかった際の当たり判定を試みるとトリガーをtrueにした方のプレイヤーが落下してしまいます。
|
6
|
+
|
7
|
+
様々な記事を読んで見ましたが自力での解決ができなかったため質問させていただきました。
|
2
8
|
|
3
9
|
|
4
10
|
|