質問編集履歴
4
質問内容を変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,276 +8,494 @@
|
|
8
8
|
|
9
9
|
デバッグログで接触していることも検知してあります。
|
10
10
|
|
11
|
-
プレイヤーを動かしているスクリプト、
|
12
|
-
|
13
|
-
|
11
|
+
プレイヤーを動かしているスクリプトを貼っています。
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
|
18
|
-
|
19
|
-
using UnityEngine;
|
20
|
-
|
21
|
-
using UnityStandardAssets.CrossPlatformInput;
|
22
|
-
|
23
|
-
using System.Collections;
|
24
|
-
|
25
|
-
|
13
|
+
もう一つスクリプトがありますが、関係ないとみて貼っていません。
|
26
|
-
|
27
|
-
|
28
|
-
|
14
|
+
|
29
|
-
namespace UnityStandardAssets.Characters.ThirdPerson
|
15
|
+
```namespace UnityStandardAssets.Characters.ThirdPerson
|
30
16
|
|
31
17
|
{
|
32
18
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
19
|
+
[RequireComponent(typeof(Rigidbody))]
|
20
|
+
|
21
|
+
[RequireComponent(typeof(CapsuleCollider))]
|
22
|
+
|
23
|
+
[RequireComponent(typeof(Animator))]
|
24
|
+
|
25
|
+
public class ThirdPersonCharacter : MonoBehaviour
|
26
|
+
|
27
|
+
{
|
28
|
+
|
29
|
+
[SerializeField] float m_MovingTurnSpeed = 360;
|
30
|
+
|
31
|
+
[SerializeField] float m_StationaryTurnSpeed = 180;
|
32
|
+
|
33
|
+
[SerializeField] float m_JumpPower = 12f;
|
34
|
+
|
35
|
+
[Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f;
|
36
|
+
|
37
|
+
[SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others
|
38
|
+
|
39
|
+
[SerializeField] float m_MoveSpeedMultiplier = 1f;
|
40
|
+
|
41
|
+
[SerializeField] float m_AnimSpeedMultiplier = 1f;
|
42
|
+
|
43
|
+
[SerializeField] float m_GroundCheckDistance = 0.1f;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
Rigidbody m_Rigidbody;
|
48
|
+
|
49
|
+
Animator m_Animator;
|
50
|
+
|
51
|
+
bool m_IsGrounded;
|
52
|
+
|
53
|
+
float m_OrigGroundCheckDistance;
|
54
|
+
|
55
|
+
const float k_Half = 0.5f;
|
56
|
+
|
57
|
+
float m_TurnAmount;
|
58
|
+
|
59
|
+
float m_ForwardAmount;
|
60
|
+
|
61
|
+
Vector3 m_GroundNormal;
|
62
|
+
|
63
|
+
float m_CapsuleHeight;
|
64
|
+
|
65
|
+
Vector3 m_CapsuleCenter;
|
66
|
+
|
67
|
+
CapsuleCollider m_Capsule;
|
68
|
+
|
69
|
+
bool m_Crouching;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
void Start()
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
m_Animator = GetComponent<Animator>();
|
80
|
+
|
81
|
+
m_Rigidbody = GetComponent<Rigidbody>();
|
82
|
+
|
83
|
+
m_Capsule = GetComponent<CapsuleCollider>();
|
84
|
+
|
85
|
+
m_CapsuleHeight = m_Capsule.height;
|
86
|
+
|
87
|
+
m_CapsuleCenter = m_Capsule.center;
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
|
92
|
+
|
93
|
+
m_OrigGroundCheckDistance = m_GroundCheckDistance;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
public void Move(Vector3 move, bool crouch, bool jump)
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
// convert the world relative moveInput vector into a local-relative
|
108
|
+
|
109
|
+
// turn amount and forward amount required to head in the desired
|
110
|
+
|
111
|
+
// direction.
|
112
|
+
|
113
|
+
if (move.magnitude > 1f) move.Normalize();
|
114
|
+
|
115
|
+
move = transform.InverseTransformDirection(move);
|
116
|
+
|
117
|
+
CheckGroundStatus();
|
118
|
+
|
119
|
+
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
|
120
|
+
|
121
|
+
m_TurnAmount = Mathf.Atan2(move.x, move.z);
|
122
|
+
|
123
|
+
m_ForwardAmount = move.z;
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
ApplyExtraTurnRotation();
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
// control and velocity handling is different when grounded and airborne:
|
132
|
+
|
133
|
+
if (m_IsGrounded)
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
HandleGroundedMovement(crouch, jump);
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
else
|
142
|
+
|
143
|
+
{
|
144
|
+
|
145
|
+
HandleAirborneMovement();
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
ScaleCapsuleForCrouching(crouch);
|
152
|
+
|
153
|
+
PreventStandingInLowHeadroom();
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
// send input and other state parameters to the animator
|
158
|
+
|
159
|
+
UpdateAnimator(move);
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
void ScaleCapsuleForCrouching(bool crouch)
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
if (m_IsGrounded && crouch)
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
if (m_Crouching) return;
|
176
|
+
|
177
|
+
m_Capsule.height = m_Capsule.height / 2f;
|
178
|
+
|
179
|
+
m_Capsule.center = m_Capsule.center / 2f;
|
180
|
+
|
181
|
+
m_Crouching = true;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
else
|
186
|
+
|
187
|
+
{
|
188
|
+
|
189
|
+
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
|
190
|
+
|
191
|
+
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
|
192
|
+
|
193
|
+
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
|
194
|
+
|
195
|
+
{
|
196
|
+
|
197
|
+
m_Crouching = true;
|
198
|
+
|
199
|
+
return;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
m_Capsule.height = m_CapsuleHeight;
|
204
|
+
|
205
|
+
m_Capsule.center = m_CapsuleCenter;
|
206
|
+
|
207
|
+
m_Crouching = false;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
void PreventStandingInLowHeadroom()
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
// prevent standing up in crouch-only zones
|
220
|
+
|
221
|
+
if (!m_Crouching)
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
|
226
|
+
|
227
|
+
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
|
228
|
+
|
229
|
+
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
m_Crouching = true;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
void UpdateAnimator(Vector3 move)
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
// update the animator parameters
|
250
|
+
|
251
|
+
m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
|
252
|
+
|
253
|
+
m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
|
254
|
+
|
255
|
+
m_Animator.SetBool("Crouch", m_Crouching);
|
256
|
+
|
257
|
+
m_Animator.SetBool("OnGround", m_IsGrounded);
|
258
|
+
|
259
|
+
if (!m_IsGrounded)
|
260
|
+
|
261
|
+
{
|
262
|
+
|
263
|
+
m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y);
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
// calculate which leg is behind, so as to leave that leg trailing in the jump animation
|
270
|
+
|
271
|
+
// (This code is reliant on the specific run cycle offset in our animations,
|
272
|
+
|
273
|
+
// and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5)
|
274
|
+
|
275
|
+
float runCycle =
|
276
|
+
|
277
|
+
Mathf.Repeat(
|
278
|
+
|
279
|
+
m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1);
|
280
|
+
|
281
|
+
float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount;
|
282
|
+
|
283
|
+
if (m_IsGrounded)
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
m_Animator.SetFloat("JumpLeg", jumpLeg);
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
// the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector,
|
294
|
+
|
295
|
+
// which affects the movement speed because of the root motion.
|
296
|
+
|
297
|
+
if (m_IsGrounded && move.magnitude > 0)
|
298
|
+
|
299
|
+
{
|
300
|
+
|
301
|
+
m_Animator.speed = m_AnimSpeedMultiplier;
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
else
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
// don't use that while airborne
|
310
|
+
|
311
|
+
m_Animator.speed = 1;
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
void HandleAirborneMovement()
|
322
|
+
|
323
|
+
{
|
324
|
+
|
325
|
+
// apply extra gravity from multiplier:
|
326
|
+
|
327
|
+
Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity;
|
328
|
+
|
329
|
+
m_Rigidbody.AddForce(extraGravityForce);
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
void HandleGroundedMovement(bool crouch, bool jump)
|
342
|
+
|
343
|
+
{
|
344
|
+
|
345
|
+
// check whether conditions are right to allow a jump:
|
346
|
+
|
347
|
+
if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded"))
|
348
|
+
|
349
|
+
{
|
350
|
+
|
351
|
+
// jump!
|
352
|
+
|
353
|
+
m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
|
354
|
+
|
355
|
+
m_IsGrounded = false;
|
356
|
+
|
357
|
+
m_Animator.applyRootMotion = false;
|
358
|
+
|
359
|
+
m_GroundCheckDistance = 0.1f;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
void ApplyExtraTurnRotation()
|
368
|
+
|
369
|
+
{
|
370
|
+
|
371
|
+
// help the character turn faster (this is in addition to root rotation in the animation)
|
372
|
+
|
373
|
+
float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
|
374
|
+
|
375
|
+
transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
public void OnAnimatorMove()
|
384
|
+
|
385
|
+
{
|
386
|
+
|
387
|
+
// we implement this function to override the default root motion.
|
388
|
+
|
389
|
+
// this allows us to modify the positional speed before it's applied.
|
390
|
+
|
391
|
+
if (m_IsGrounded && Time.deltaTime > 0)
|
392
|
+
|
393
|
+
{
|
394
|
+
|
395
|
+
Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
// we preserve the existing y part of the current velocity.
|
400
|
+
|
401
|
+
v.y = m_Rigidbody.velocity.y;
|
402
|
+
|
403
|
+
m_Rigidbody.velocity = v;
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
void CheckGroundStatus()
|
414
|
+
|
415
|
+
{
|
416
|
+
|
417
|
+
RaycastHit hitInfo;
|
418
|
+
|
419
|
+
#if UNITY_EDITOR
|
420
|
+
|
421
|
+
// helper to visualise the ground check ray in the scene view
|
422
|
+
|
423
|
+
Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
|
424
|
+
|
425
|
+
#endif
|
426
|
+
|
427
|
+
// 0.1f is a small offset to start the ray from inside the character
|
428
|
+
|
429
|
+
// it is also good to note that the transform position in the sample assets is at the base of the character
|
430
|
+
|
431
|
+
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance))
|
432
|
+
|
433
|
+
{
|
434
|
+
|
435
|
+
m_GroundNormal = hitInfo.normal;
|
436
|
+
|
437
|
+
m_IsGrounded = true;
|
438
|
+
|
439
|
+
m_Animator.applyRootMotion = true;
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
else
|
444
|
+
|
445
|
+
{
|
446
|
+
|
447
|
+
m_IsGrounded = false;
|
448
|
+
|
449
|
+
m_GroundNormal = Vector3.up;
|
450
|
+
|
451
|
+
m_Animator.applyRootMotion = false;
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
void OnCollisionEnter(Collision col)
|
54
464
|
|
55
465
|
{
|
56
466
|
|
57
|
-
|
467
|
+
if (col.gameObject.tag == "MoveStage")
|
58
|
-
|
59
|
-
if (Camera.main != null)
|
60
468
|
|
61
469
|
{
|
62
470
|
|
63
|
-
|
471
|
+
transform.SetParent(col.transform);
|
64
472
|
|
65
473
|
}
|
66
474
|
|
67
|
-
|
475
|
+
}
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
void OnCollisionExit(Collision col)
|
480
|
+
|
481
|
+
{
|
482
|
+
|
483
|
+
if (col.gameObject.tag == "MoveStage")
|
68
484
|
|
69
485
|
{
|
70
486
|
|
71
|
-
|
487
|
+
transform.SetParent(null);
|
72
|
-
|
73
|
-
"Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
|
74
|
-
|
75
|
-
// we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
|
76
488
|
|
77
489
|
}
|
78
490
|
|
79
|
-
|
80
|
-
|
81
|
-
// get the third person character ( this should never be null due to require component )
|
82
|
-
|
83
|
-
m_Character = GetComponent<ThirdPersonCharacter>();
|
84
|
-
|
85
491
|
}
|
86
492
|
|
87
493
|
|
88
494
|
|
89
|
-
|
90
|
-
|
91
|
-
private void Update()
|
92
|
-
|
93
|
-
{
|
94
|
-
|
95
|
-
if (!m_Jump)
|
96
|
-
|
97
|
-
{
|
98
|
-
|
99
|
-
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
// Fixed update is called in sync with physics
|
110
|
-
|
111
|
-
private void FixedUpdate()
|
112
|
-
|
113
|
-
{
|
114
|
-
|
115
|
-
// read inputs
|
116
|
-
|
117
|
-
float h = CrossPlatformInputManager.GetAxis("Horizontal");
|
118
|
-
|
119
|
-
float v = CrossPlatformInputManager.GetAxis("Vertical");
|
120
|
-
|
121
|
-
bool crouch = Input.GetKey(KeyCode.C);
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
// calculate move direction to pass to character
|
126
|
-
|
127
|
-
if (m_Cam != null)
|
128
|
-
|
129
|
-
{
|
130
|
-
|
131
|
-
// calculate camera relative direction to move:
|
132
|
-
|
133
|
-
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
|
134
|
-
|
135
|
-
m_Move = v*m_CamForward + h*m_Cam.right;
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
else
|
140
|
-
|
141
|
-
{
|
142
|
-
|
143
|
-
// we use world-relative directions in the case of no main camera
|
144
|
-
|
145
|
-
m_Move = v*Vector3.forward + h*Vector3.right;
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
#if !MOBILE_INPUT
|
150
|
-
|
151
|
-
// walk speed multiplier
|
152
|
-
|
153
|
-
if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
|
154
|
-
|
155
|
-
#endif
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
// pass all parameters to the character control script
|
160
|
-
|
161
|
-
m_Character.Move(m_Move, crouch, m_Jump);
|
162
|
-
|
163
|
-
m_Jump = false;
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
void OnCollisionStay(Collision col)
|
170
|
-
|
171
|
-
{
|
172
|
-
|
173
|
-
if (col.gameObject.tag == "MoveStage")
|
174
|
-
|
175
|
-
{
|
176
|
-
|
177
|
-
transform.parent = col.gameObject.transform;
|
178
|
-
|
179
|
-
Debug.Log("parenting");
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
void OnCollisionExit()
|
186
|
-
|
187
|
-
{
|
188
|
-
|
189
|
-
transform.parent = null;
|
190
|
-
|
191
|
-
Debug.Log("exit");
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
495
|
}
|
198
496
|
|
199
497
|
}
|
200
498
|
|
201
|
-
|
202
|
-
|
203
499
|
コード
|
204
500
|
|
205
501
|
```
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
``` Vector3 initPosition;
|
210
|
-
|
211
|
-
Vector3 newPosition;
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
public float distance = 1f;
|
216
|
-
|
217
|
-
private List<Rigidbody> _rigidbodies = new List<Rigidbody>();
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
// Use this for initialization
|
222
|
-
|
223
|
-
void Start()
|
224
|
-
|
225
|
-
{
|
226
|
-
|
227
|
-
/*
|
228
|
-
|
229
|
-
このスクリプトを付けたゲームオブジェクト(this)の
|
230
|
-
|
231
|
-
トランスフォームコンポーネント(transform)の
|
232
|
-
|
233
|
-
位置(position)をinitPositionに格納している。
|
234
|
-
|
235
|
-
*/
|
236
|
-
|
237
|
-
initPosition = this.transform.position;
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
// Update is called once per frame
|
244
|
-
|
245
|
-
void Update()
|
246
|
-
|
247
|
-
{
|
248
|
-
|
249
|
-
/*
|
250
|
-
|
251
|
-
プッシャーが反復運動するよう、フレーム毎に位置を更新している。
|
252
|
-
|
253
|
-
ここでは、z軸方向に反復運動するようにしている。
|
254
|
-
|
255
|
-
反復運動の移動モデルはsin関数である。
|
256
|
-
|
257
|
-
*/
|
258
|
-
|
259
|
-
newPosition = new Vector3(initPosition.x + distance * Mathf.Sin(Time.time),
|
260
|
-
|
261
|
-
initPosition.y,
|
262
|
-
|
263
|
-
initPosition.z);
|
264
|
-
|
265
|
-
/*
|
266
|
-
|
267
|
-
Start関数内ではthis.transform.positionにてゲームオブジェクトの
|
268
|
-
|
269
|
-
トランスフォームコンポーネントの情報を取得していた。
|
270
|
-
|
271
|
-
トランスフォームコンポーネント以外のコンポーネントは
|
272
|
-
|
273
|
-
GetComponent()の様に取得する必要がある(Unity5から)。
|
274
|
-
|
275
|
-
*/
|
276
|
-
|
277
|
-
this.GetComponent<Rigidbody>().MovePosition(newPosition);
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
コード
|
282
|
-
|
283
|
-
```
|
3
質問内容の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,10 @@
|
|
8
8
|
|
9
9
|
デバッグログで接触していることも検知してあります。
|
10
10
|
|
11
|
+
プレイヤーを動かしているスクリプト、
|
12
|
+
|
13
|
+
床を動かしているスクリプトを貼っています。
|
14
|
+
|
11
15
|
|
12
16
|
|
13
17
|
``` using System;
|
@@ -199,3 +203,81 @@
|
|
199
203
|
コード
|
200
204
|
|
201
205
|
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
``` Vector3 initPosition;
|
210
|
+
|
211
|
+
Vector3 newPosition;
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
public float distance = 1f;
|
216
|
+
|
217
|
+
private List<Rigidbody> _rigidbodies = new List<Rigidbody>();
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
// Use this for initialization
|
222
|
+
|
223
|
+
void Start()
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
/*
|
228
|
+
|
229
|
+
このスクリプトを付けたゲームオブジェクト(this)の
|
230
|
+
|
231
|
+
トランスフォームコンポーネント(transform)の
|
232
|
+
|
233
|
+
位置(position)をinitPositionに格納している。
|
234
|
+
|
235
|
+
*/
|
236
|
+
|
237
|
+
initPosition = this.transform.position;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
// Update is called once per frame
|
244
|
+
|
245
|
+
void Update()
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
/*
|
250
|
+
|
251
|
+
プッシャーが反復運動するよう、フレーム毎に位置を更新している。
|
252
|
+
|
253
|
+
ここでは、z軸方向に反復運動するようにしている。
|
254
|
+
|
255
|
+
反復運動の移動モデルはsin関数である。
|
256
|
+
|
257
|
+
*/
|
258
|
+
|
259
|
+
newPosition = new Vector3(initPosition.x + distance * Mathf.Sin(Time.time),
|
260
|
+
|
261
|
+
initPosition.y,
|
262
|
+
|
263
|
+
initPosition.z);
|
264
|
+
|
265
|
+
/*
|
266
|
+
|
267
|
+
Start関数内ではthis.transform.positionにてゲームオブジェクトの
|
268
|
+
|
269
|
+
トランスフォームコンポーネントの情報を取得していた。
|
270
|
+
|
271
|
+
トランスフォームコンポーネント以外のコンポーネントは
|
272
|
+
|
273
|
+
GetComponent()の様に取得する必要がある(Unity5から)。
|
274
|
+
|
275
|
+
*/
|
276
|
+
|
277
|
+
this.GetComponent<Rigidbody>().MovePosition(newPosition);
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
コード
|
282
|
+
|
283
|
+
```
|
2
質問内容の修正
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
1
質問内容の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,71 +6,195 @@
|
|
6
6
|
|
7
7
|
タグはしっかり設定しています。
|
8
8
|
|
9
|
-
|
9
|
+
デバッグログで接触していることも検知してあります。
|
10
|
+
|
11
|
+
|
12
|
+
|
10
|
-
|
13
|
+
``` using System;
|
14
|
+
|
15
|
+
using UnityEngine;
|
16
|
+
|
17
|
+
using UnityStandardAssets.CrossPlatformInput;
|
18
|
+
|
19
|
+
using System.Collections;
|
20
|
+
|
21
|
+
using System.Collections.Generic;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
namespace UnityStandardAssets.Characters.ThirdPerson
|
26
|
+
|
27
|
+
{
|
28
|
+
|
29
|
+
[RequireComponent(typeof (ThirdPersonCharacter))]
|
30
|
+
|
11
|
-
|
31
|
+
public class ThirdPersonUserControl : MonoBehaviour
|
12
|
-
|
13
|
-
void Start ()
|
14
32
|
|
15
33
|
{
|
16
34
|
|
35
|
+
private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
|
36
|
+
|
37
|
+
private Transform m_Cam; // A reference to the main camera in the scenes transform
|
38
|
+
|
39
|
+
private Vector3 m_CamForward; // The current forward direction of the camera
|
40
|
+
|
41
|
+
private Vector3 m_Move;
|
42
|
+
|
43
|
+
private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
private void Start()
|
50
|
+
|
51
|
+
{
|
52
|
+
|
17
|
-
|
53
|
+
// get the transform of the main camera
|
54
|
+
|
55
|
+
if (Camera.main != null)
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
m_Cam = Camera.main.transform;
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
else
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
Debug.LogWarning(
|
68
|
+
|
69
|
+
"Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
|
70
|
+
|
71
|
+
// we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// get the third person character ( this should never be null due to require component )
|
78
|
+
|
79
|
+
m_Character = GetComponent<ThirdPersonCharacter>();
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
private void Update()
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
if (!m_Jump)
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// Fixed update is called in sync with physics
|
106
|
+
|
107
|
+
private void FixedUpdate()
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
// read inputs
|
112
|
+
|
113
|
+
float h = CrossPlatformInputManager.GetAxis("Horizontal");
|
114
|
+
|
115
|
+
float v = CrossPlatformInputManager.GetAxis("Vertical");
|
116
|
+
|
117
|
+
bool crouch = Input.GetKey(KeyCode.C);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// calculate move direction to pass to character
|
122
|
+
|
123
|
+
if (m_Cam != null)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
// calculate camera relative direction to move:
|
128
|
+
|
129
|
+
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
|
130
|
+
|
131
|
+
m_Move = v*m_CamForward + h*m_Cam.right;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
else
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
// we use world-relative directions in the case of no main camera
|
140
|
+
|
141
|
+
m_Move = v*Vector3.forward + h*Vector3.right;
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
#if !MOBILE_INPUT
|
146
|
+
|
147
|
+
// walk speed multiplier
|
148
|
+
|
149
|
+
if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
|
150
|
+
|
151
|
+
#endif
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
// pass all parameters to the character control script
|
156
|
+
|
157
|
+
m_Character.Move(m_Move, crouch, m_Jump);
|
158
|
+
|
159
|
+
m_Jump = false;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
void OnCollisionStay(Collision col)
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
if (col.gameObject.tag == "MoveStage")
|
170
|
+
|
171
|
+
{
|
172
|
+
|
173
|
+
transform.parent = col.gameObject.transform;
|
174
|
+
|
175
|
+
Debug.Log("parenting");
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
void OnCollisionExit()
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
transform.parent = null;
|
186
|
+
|
187
|
+
Debug.Log("exit");
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
18
192
|
|
19
193
|
}
|
20
194
|
|
21
|
-
|
22
|
-
|
23
|
-
void Update ()
|
24
|
-
|
25
|
-
{
|
26
|
-
|
27
|
-
Vector3 lossScale = transform.lossyScale;
|
28
|
-
|
29
|
-
Vector3 localScale = transform.localScale;
|
30
|
-
|
31
|
-
transform.localScale = new Vector3(
|
32
|
-
|
33
|
-
localScale.x / lossScale.x * defaultScale.x,
|
34
|
-
|
35
|
-
localScale.y / lossScale.y * defaultScale.y,
|
36
|
-
|
37
|
-
localScale.z / lossScale.z * defaultScale.z
|
38
|
-
|
39
|
-
);
|
40
|
-
|
41
|
-
|
195
|
+
}
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
196
|
+
|
46
|
-
|
47
|
-
|
197
|
+
|
48
|
-
|
49
|
-
var emptyObject = new GameObject();
|
50
|
-
|
51
|
-
emptyObject.transform.parent = collision.gameObject.transform;
|
52
|
-
|
53
|
-
transform.parent = emptyObject.transform;
|
54
|
-
|
55
|
-
}
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
void OnCollisionExit(Collision collision){
|
64
|
-
|
65
|
-
if (transform.parent != null && collision.gameObject.tag == "MoveFloor")
|
66
|
-
|
67
|
-
{
|
68
|
-
|
69
|
-
transform.parent = null;
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
}
|
74
198
|
|
75
199
|
コード
|
76
200
|
|