回答編集履歴

1

「上下に動く時は遅くなってしまう」のコードを修正

2019/09/23 04:20

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -26,57 +26,57 @@
26
26
 
27
27
  {
28
28
 
29
- [RequireComponent(typeof(Rigidbody))]
30
-
31
- [RequireComponent(typeof(CapsuleCollider))]
32
-
33
- [RequireComponent(typeof(Animator))]
34
-
35
- public class ThirdPersonCharacter : MonoBehaviour
36
-
37
- {
38
-
39
- // ここまで変更なしのため省略
40
-
41
-
42
-
43
- void UpdateAnimator(Vector3 move)
44
-
45
- {
46
-
47
- // update the animator parameters
48
-
49
-
50
-
51
- // ここの2行を...
52
-
53
- /*
54
-
55
- m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
56
-
57
- m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
58
-
59
- */
60
-
61
-
62
-
63
- // このようにして、アニメーションのForwardとTurnが即座に目標値に到達するようにしてみる
64
-
65
- m_Animator.SetFloat("Forward", m_ForwardAmount);
66
-
67
- m_Animator.SetFloat("Turn", m_TurnAmount);
68
-
69
-
70
-
71
- // 残りは変更なしのため省略
72
-
73
- }
74
-
75
-
76
-
77
- // 残りは変更なしのため省略
78
-
79
- }
29
+ [RequireComponent(typeof(Rigidbody))]
30
+
31
+ [RequireComponent(typeof(CapsuleCollider))]
32
+
33
+ [RequireComponent(typeof(Animator))]
34
+
35
+ public class ThirdPersonCharacter : MonoBehaviour
36
+
37
+ {
38
+
39
+ // ここまで変更なしのため省略
40
+
41
+
42
+
43
+ void UpdateAnimator(Vector3 move)
44
+
45
+ {
46
+
47
+ // update the animator parameters
48
+
49
+
50
+
51
+ // ここの2行を...
52
+
53
+ /*
54
+
55
+ m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
56
+
57
+ m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
58
+
59
+ */
60
+
61
+
62
+
63
+ // このようにして、アニメーションのForwardとTurnが即座に目標値に到達するようにしてみる
64
+
65
+ m_Animator.SetFloat("Forward", m_ForwardAmount);
66
+
67
+ m_Animator.SetFloat("Turn", m_TurnAmount);
68
+
69
+
70
+
71
+ // 残りは変更なしのため省略
72
+
73
+ }
74
+
75
+
76
+
77
+ // 残りは変更なしのため省略
78
+
79
+ }
80
80
 
81
81
  }
82
82
 
@@ -100,85 +100,85 @@
100
100
 
101
101
  {
102
102
 
103
- [RequireComponent(typeof (ThirdPersonCharacter))]
104
-
105
- public class Move2 : MonoBehaviour
106
-
107
- {
108
-
109
- private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
110
-
111
- private Transform m_Cam; // A reference to the main camera in the scenes transform
112
-
113
- private Vector3 m_CamForward; // The current forward direction of the camera
114
-
115
- private Vector3 m_Move;
116
-
117
- private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
118
-
119
- private Quaternion m_InitialRotation; // The rotation of the transform at the Start
120
-
121
-
122
-
123
- private void Start()
124
-
125
- {
126
-
127
- // get the third person character ( this should never be null due to require component )
128
-
129
- m_Character = GetComponent<ThirdPersonCharacter>();
130
-
131
- m_InitialRotation = transform.rotation;
132
-
133
- }
134
-
135
-
136
-
137
-
138
-
139
- private void Update() {
140
-
141
-
142
-
143
- }
144
-
145
-
146
-
147
-
148
-
149
- // Fixed update is called in sync with physics
150
-
151
- private void FixedUpdate()
152
-
153
- {
154
-
155
- // read inputs
156
-
157
- float h = Input.GetAxisRaw("Horizontal");
158
-
159
- float v = Input.GetAxisRaw("Vertical");
160
-
161
- bool crouch = Input.GetKey(KeyCode.C);
162
-
163
-
164
-
165
- // キャラ操作の前後左右をカメラの方向に合わせる
166
-
167
- // 入力値をそのままキャラクターの移動方向として処理する
168
-
169
- m_Move = v*Vector3.forward + h*Vector3.right;
170
-
171
-
172
-
173
- // pass all parameters to the character control script
174
-
175
- m_Character.Move(m_InitialRotation * m_Move, crouch, m_Jump);
176
-
177
- m_Jump = false;
178
-
179
- }
180
-
181
- }
103
+ [RequireComponent(typeof (ThirdPersonCharacter))]
104
+
105
+ public class Move2 : MonoBehaviour
106
+
107
+ {
108
+
109
+ private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
110
+
111
+ private Transform m_Cam; // A reference to the main camera in the scenes transform
112
+
113
+ private Vector3 m_CamForward; // The current forward direction of the camera
114
+
115
+ private Vector3 m_Move;
116
+
117
+ private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
118
+
119
+ private Quaternion m_InitialRotation; // The rotation of the transform at the Start
120
+
121
+
122
+
123
+ private void Start()
124
+
125
+ {
126
+
127
+ // get the third person character ( this should never be null due to require component )
128
+
129
+ m_Character = GetComponent<ThirdPersonCharacter>();
130
+
131
+ m_InitialRotation = transform.rotation;
132
+
133
+ }
134
+
135
+
136
+
137
+
138
+
139
+ private void Update() {
140
+
141
+
142
+
143
+ }
144
+
145
+
146
+
147
+
148
+
149
+ // Fixed update is called in sync with physics
150
+
151
+ private void FixedUpdate()
152
+
153
+ {
154
+
155
+ // read inputs
156
+
157
+ float h = Input.GetAxisRaw("Horizontal");
158
+
159
+ float v = Input.GetAxisRaw("Vertical");
160
+
161
+ bool crouch = Input.GetKey(KeyCode.C);
162
+
163
+
164
+
165
+ // キャラ操作の前後左右をカメラの方向に合わせる
166
+
167
+ // 入力値をそのままキャラクターの移動方向として処理する
168
+
169
+ m_Move = v*Vector3.forward + h*Vector3.right;
170
+
171
+
172
+
173
+ // pass all parameters to the character control script
174
+
175
+ m_Character.Move(m_InitialRotation * m_Move, crouch, m_Jump);
176
+
177
+ m_Jump = false;
178
+
179
+ }
180
+
181
+ }
182
182
 
183
183
  }
184
184
 
@@ -194,6 +194,184 @@
194
194
 
195
195
 
196
196
 
197
+ **「上下に動く時は遅くなってしまう」について追記**
198
+
199
+ すいません、前後方向の速さ調整が不十分で、水平方向よりも遅くなっていました。付け焼き刃ですが`ThirdPersonCharacter`を下記のようにして、
200
+
201
+ ```C#
202
+
203
+ using UnityEngine;
204
+
205
+
206
+
207
+ namespace UnityStandardAssets.Characters.ThirdPerson
208
+
209
+ {
210
+
211
+ [RequireComponent(typeof(Rigidbody))]
212
+
213
+ [RequireComponent(typeof(CapsuleCollider))]
214
+
215
+ [RequireComponent(typeof(Animator))]
216
+
217
+ public class ThirdPersonCharacter : MonoBehaviour
218
+
219
+ {
220
+
221
+ public Vector2 MoveSpeedMultiplier2D { get; set; } = Vector2.one; // プロパティを追加
222
+
223
+
224
+
225
+ // 省略
226
+
227
+
228
+
229
+ public void OnAnimatorMove()
230
+
231
+ {
232
+
233
+ // we implement this function to override the default root motion.
234
+
235
+ // this allows us to modify the positional speed before it's applied.
236
+
237
+ if (m_IsGrounded && Time.deltaTime > 0)
238
+
239
+ {
240
+
241
+ // X、Zに追加の速度調整を行う
242
+
243
+ Vector3 v = Vector3.Scale(m_Animator.deltaPosition, new Vector3(MoveSpeedMultiplier2D.x, 0.0f, MoveSpeedMultiplier2D.y)) * m_MoveSpeedMultiplier / Time.deltaTime;
244
+
245
+
246
+
247
+ // we preserve the existing y part of the current velocity.
248
+
249
+ v.y = m_Rigidbody.velocity.y;
250
+
251
+ m_Rigidbody.velocity = v;
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ // 省略
260
+
261
+ }
262
+
263
+ }
264
+
265
+ ```
266
+
267
+ `Move2`を下記のようにしました。
268
+
269
+ ```C#
270
+
271
+ using UnityEngine;
272
+
273
+
274
+
275
+ namespace UnityStandardAssets.Characters.ThirdPerson
276
+
277
+ {
278
+
279
+ [RequireComponent(typeof (ThirdPersonCharacter))]
280
+
281
+ public class Move2 : MonoBehaviour
282
+
283
+ {
284
+
285
+ private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
286
+
287
+ private Transform m_Cam; // A reference to the main camera in the scenes transform
288
+
289
+ private Vector3 m_CamForward; // The current forward direction of the camera
290
+
291
+ private Vector3 m_Move;
292
+
293
+ private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
294
+
295
+ private Vector2 m_Gradient; // The gradient of the X-Z plane of the transform at the Start
296
+
297
+
298
+
299
+ private void Start()
300
+
301
+ {
302
+
303
+ // get the third person character ( this should never be null due to require component )
304
+
305
+ m_Character = GetComponent<ThirdPersonCharacter>();
306
+
307
+ m_Gradient = new Vector2(-transform.up.x / transform.up.y, -transform.up.z / transform.up.y);
308
+
309
+ }
310
+
311
+
312
+
313
+ private void Update()
314
+
315
+ {
316
+
317
+ }
318
+
319
+
320
+
321
+ // Fixed update is called in sync with physics
322
+
323
+ private void FixedUpdate()
324
+
325
+ {
326
+
327
+ // read inputs
328
+
329
+ float h = Input.GetAxisRaw("Horizontal");
330
+
331
+ float v = Input.GetAxisRaw("Vertical");
332
+
333
+ bool crouch = Input.GetKey(KeyCode.C);
334
+
335
+
336
+
337
+ // キャラ操作の前後左右をカメラの方向に合わせる
338
+
339
+ // 入力値をそのままキャラクターの移動方向として処理する
340
+
341
+ m_Move = v*Vector3.forward + h*Vector3.right;
342
+
343
+
344
+
345
+ // pass all parameters to the character control script
346
+
347
+ Vector3 move = m_Move + Vector3.up * Vector2.Dot(new Vector2(h, v), this.m_Gradient);
348
+
349
+ float absMoveX = Mathf.Abs(m_Move.x);
350
+
351
+ float absMoveZ = Mathf.Abs(m_Move.z);
352
+
353
+ float multiplierX = absMoveX > 0.01f ? new Vector2(move.x, move.y).magnitude / absMoveX : 1.0f;
354
+
355
+ float multiplierZ = absMoveZ > 0.01f ? new Vector2(move.z, move.y).magnitude / absMoveZ : 1.0f;
356
+
357
+ this.m_Character.MoveSpeedMultiplier2D = new Vector2(multiplierX, multiplierZ);
358
+
359
+ m_Character.Move(move, crouch, m_Jump);
360
+
361
+ m_Jump = false;
362
+
363
+ }
364
+
365
+ }
366
+
367
+ }
368
+
369
+ ```
370
+
371
+ 何だか`ThirdPersonCharacter`は普通の正立したキャラクターを前提にした部分が随所にあるようで、けっこうやっかいですね...
372
+
373
+
374
+
197
375
  **おまけ**
198
376
 
199
377
  ご質問者さんの進めていらっしゃる45°回転方式で問題なさそうですが、オブジェクトを正立させたまま見た目だけ傾けることはできないかと思い、個人的な興味から実験してみました。
@@ -214,39 +392,39 @@
214
392
 
215
393
  {
216
394
 
217
- [SerializeField][Range(-45.0f, 45.0f)] private float angle = 45.0f;
395
+ [SerializeField][Range(-45.0f, 45.0f)] private float angle = 45.0f;
218
-
396
+
219
- private new Camera camera;
397
+ private new Camera camera;
220
-
221
-
222
-
398
+
399
+
400
+
223
- private void Awake()
401
+ private void Awake()
224
-
402
+
225
- {
403
+ {
226
-
404
+
227
- this.camera = this.GetComponent<Camera>();
405
+ this.camera = this.GetComponent<Camera>();
228
-
406
+
229
- }
407
+ }
230
-
231
-
232
-
408
+
409
+
410
+
233
- private void OnPreCull()
411
+ private void OnPreCull()
234
-
412
+
235
- {
413
+ {
236
-
414
+
237
- var theta = this.angle * Mathf.Deg2Rad;
415
+ var theta = this.angle * Mathf.Deg2Rad;
238
-
416
+
239
- var tiltMatrix = Matrix4x4.identity;
417
+ var tiltMatrix = Matrix4x4.identity;
240
-
418
+
241
- tiltMatrix[5] = Mathf.Cos(theta);
419
+ tiltMatrix[5] = Mathf.Cos(theta);
242
-
420
+
243
- tiltMatrix[6] = Mathf.Sin(theta);
421
+ tiltMatrix[6] = Mathf.Sin(theta);
244
-
422
+
245
- this.camera.ResetWorldToCameraMatrix();
423
+ this.camera.ResetWorldToCameraMatrix();
246
-
424
+
247
- this.camera.worldToCameraMatrix *= tiltMatrix;
425
+ this.camera.worldToCameraMatrix *= tiltMatrix;
248
-
426
+
249
- }
427
+ }
250
428
 
251
429
  }
252
430