質問編集履歴

1

2019/10/27 キャラクターの動作関連のスクリプトを追加しました。

2019/10/27 03:04

投稿

Higawind
Higawind

スコア17

test CHANGED
File without changes
test CHANGED
@@ -14,10 +14,6 @@
14
14
 
15
15
 
16
16
 
17
- すべてのスクリプトを記載するのは厳しいため、プレイヤーオブジェクトを生成する部分のスクリプトのみ載せています。
18
-
19
-
20
-
21
17
  下記のスクリプト中に問題のある箇所があったり、上述の問題が発生している
22
18
 
23
19
  原因などがほかにありましたら、お教えいただければ幸いです。
@@ -26,25 +22,93 @@
26
22
 
27
23
 
28
24
 
29
- ### 発生している問題・エラメッセ
25
+ ### 該当のソスコ
26
+
30
-
27
+ ```using System.Collections;
28
+
31
-
29
+ using System.Collections.Generic;
30
+
31
+ using UnityEngine;
32
+
33
+
34
+
35
+ public class gameManage : MonoBehaviour
36
+
37
+ {
38
+
39
+ //誰かがログインする度に生成するプレイヤーPrefab
40
+
41
+ public GameObject playerPrefab;
42
+
43
+
44
+
45
+ void Start()
46
+
47
+ {
48
+
49
+
50
+
51
+ if (!PhotonNetwork.connected) //Phootnに接続されていなければ
52
+
53
+ {
54
+
55
+ Application.LoadLevel("Launcher"); //ログイン画面に戻る
56
+
57
+ return;
58
+
59
+ }
60
+
61
+ //Photonに接続していれば自プレイヤーを生成
62
+
63
+
64
+
65
+ GameObject Player = PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 3f, -9f), Quaternion.identity, 0);
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+ characterMove characterMove = Player.GetComponent<characterMove>();
76
+
77
+ characterMove.enabled = true;
78
+
79
+ Attack attack = Player.GetComponent<Attack>();
80
+
81
+ attack.enabled = true;
82
+
83
+
84
+
85
+ }
86
+
87
+
88
+
89
+ // Update is called once per frame
90
+
91
+ void Update()
92
+
93
+ {
94
+
95
+
96
+
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+ }
32
104
 
33
105
  ```
34
106
 
35
- エラーメッセージ
107
+
36
108
 
37
109
  ```
38
110
 
39
-
40
-
41
- ### 該当のソースコード
42
-
43
-
44
-
45
-
46
-
47
- ```using System.Collections;
111
+ using System.Collections;
48
112
 
49
113
  using System.Collections.Generic;
50
114
 
@@ -52,73 +116,291 @@
52
116
 
53
117
 
54
118
 
55
- public class gameManage : MonoBehaviour
119
+ public class characterMove : MonoBehaviour
56
120
 
57
121
  {
58
122
 
123
+ public PhotonView myPV;
124
+
59
- //誰かがログインする度に生成するプレイヤーPrefab
125
+ public PhotonTransformView myPTV;
60
-
126
+
61
- public GameObject playerPrefab;
127
+ public Camera mainCam;
128
+
129
+
130
+
62
-
131
+ // このスクリプトで使う変数一覧
132
+
63
-
133
+ public CharacterController charaCon; // キャラクターコンポーネント用の変数
134
+
135
+ public Animator animCon; // アニメーションするための変数
136
+
137
+ public float idoSpeed = 5.0f; // 移動速度(Public=インスペクタで調整可能)
138
+
139
+ public float kaitenSpeed = 1200.0f; // プレイヤーの回転速度(Public=インスペクタで調整可能)
140
+
141
+
142
+
143
+ private Vector3 movePower = Vector3.zero; // キャラクター移動量(未使用)
144
+
145
+ private float jumpPower = 10.0f; // キャラクター跳躍力(未使用)
146
+
147
+ private const float gravityPower = 9.8f; // キャラクター重力(未使用)
148
+
149
+ private Vector3 moveDirection;
150
+
151
+
152
+
153
+
154
+
155
+ public void Hit() // ヒット時のアニメーションイベント(今のところからっぽ。ないとエラーが出る)
156
+
157
+ {
158
+
159
+
160
+
161
+ }
162
+
163
+
164
+
165
+
166
+
167
+ // ■最初に1回だけ実行する処理
64
168
 
65
169
  void Start()
66
170
 
67
171
  {
68
172
 
69
-
173
+ //charaCon = GetComponent<CharacterController>(); // キャラクターコントローラーのコンポーネントを参照する
174
+
70
-
175
+ //animCon = GetComponent<Animator>(); // アニメーターのコンポーネントを参照する
176
+
71
- if (!PhotonNetwork.connected) //Phootnに接続さていなけれ
177
+ if (myPV.isMine) //自キャラであれば実行
72
178
 
73
179
  {
74
180
 
181
+ //MainCameraのtargetにこのゲームオブジェクトを設定
182
+
183
+ mainCam.GetComponent<FollowingCamera>().target = this.gameObject.transform;
184
+
185
+ }
186
+
187
+ }
188
+
189
+
190
+
191
+
192
+
193
+ // ■毎フレーム常に実行する処理
194
+
195
+ void Update()
196
+
197
+ {
198
+
199
+ /*
200
+
75
- Application.LoadLevel("Launcher"); //ログイン画面に戻る
201
+ if (!myPV.isMine) //自キャラであれば実行
202
+
203
+ {
76
204
 
77
205
  return;
78
206
 
79
207
  }
80
208
 
81
- //Photonに接続していれば自プレイヤーを生成
82
-
83
-
84
-
85
- GameObject Player = PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 3f, -9f), Quaternion.identity, 0);
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
- characterMove characterMove = Player.GetComponent<characterMove>();
96
-
97
- characterMove.enabled = true;
98
-
99
- Attack attack = Player.GetComponent<Attack>();
100
-
101
- attack.enabled = true;
102
-
103
-
104
-
105
- }
106
-
107
-
108
-
109
- // Update is called once per frame
110
-
111
- void Update()
112
-
113
- {
114
-
115
-
116
-
117
- }
118
-
119
-
120
-
121
-
209
+ */
210
+
211
+ //スムーズな同期のためにPhotonTransformViewに速度値を渡す
212
+
213
+ Vector3 velocity = charaCon.velocity;
214
+
215
+ myPTV.SetSynchronizedValues(velocity, 0);
216
+
217
+
218
+
219
+
220
+
221
+ float holizontal = Input.GetAxis("Horizontal");
222
+
223
+ float vertical = Input.GetAxis("Vertical");
224
+
225
+ Rigidbody rigid = GetComponent<Rigidbody>();
226
+
227
+ moveDirection = transform.transform.forward * idoSpeed * Input.GetAxis("Vertical");
228
+
229
+ moveDirection.y -= 10f * Time.deltaTime;
230
+
231
+
232
+
233
+ if (Input.GetKeyDown(KeyCode.Space))
234
+
235
+ {
236
+
237
+ //上にジャンプする。
238
+
239
+ //Debug.Log(moveDirection);
240
+
241
+ charaCon.Move(moveDirection * Time.deltaTime);
242
+
243
+ //rigid.AddForce(Vector3.down * 80, ForceMode.Acceleration);
244
+
245
+ }
246
+
247
+
248
+
249
+ // ▼▼▼移動処理▼▼▼
250
+
251
+ if (animCon.GetBool("Attack") == true || animCon.GetBool("SPAttack") == true || animCon.GetBool("Dead") == true)
252
+
253
+ {
254
+
255
+ if (animCon.GetBool("SPAttack") == true)
256
+
257
+ {
258
+
259
+ Invoke("SPReset", 1.3f);
260
+
261
+ }
262
+
263
+ return;
264
+
265
+ }
266
+
267
+
268
+
269
+ if (Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0) // テンキーや3Dスティックの入力(GetAxis)がゼロの時の動作
270
+
271
+ {
272
+
273
+
274
+
275
+ animCon.SetBool("Run", false); // Runモーションしない
276
+
277
+ }
278
+
279
+
280
+
281
+ else// テンキーや3Dスティックの入力(GetAxis)がゼロではない時の動作
282
+
283
+ {
284
+
285
+
286
+
287
+ var cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; // カメラが追従するための動作
288
+
289
+ Vector3 direction = cameraForward * Input.GetAxis("Vertical") + Camera.main.transform.right * Input.GetAxis("Horizontal"); // テンキーや3Dスティックの入力(GetAxis)があるとdirectionに値を返す
290
+
291
+ animCon.SetBool("Run", true); // Runモーションする
292
+
293
+
294
+
295
+ MukiWoKaeru(direction); // 向きを変える動作の処理を実行する(後述)
296
+
297
+ IdoSuru(direction); // 移動する動作の処理を実行する(後述)
298
+
299
+ }
300
+
301
+
302
+
303
+
304
+
305
+ // ▼▼▼アクション処理▼▼▼
306
+
307
+ animCon.SetBool("Attack", Input.GetKey("j") || Input.GetButtonDown("Attack")); // キーorボタンを押したらアクションを実行
308
+
309
+ animCon.SetBool("Guard", Input.GetKey("l") || Input.GetButtonDown("Guard")); // キーorボタンを押したらアクション2を実行
310
+
311
+ animCon.SetBool("SPAttack", Input.GetKey("k") || Input.GetButtonDown("SPAttack")); // キーorボタンを押したらアクション3を実行
312
+
313
+ animCon.SetBool("Jump", Input.GetKey("space") || Input.GetButtonDown("Jump")); // キーorボタンを押したらジャンプを実行(仮)
314
+
315
+
316
+
317
+ if (LocalVariables.currentHP <= 0)
318
+
319
+ {
320
+
321
+ Debug.Log("死亡");
322
+
323
+ animCon.SetBool("Dead", true);
324
+
325
+ Invoke("DeadReset", 8.0f);
326
+
327
+ }
328
+
329
+
330
+
331
+ }
332
+
333
+
334
+
335
+ private void SPReset()
336
+
337
+ {
338
+
339
+ animCon.SetBool("SPAttack", false);
340
+
341
+ }
342
+
343
+
344
+
345
+ private void DeadReset()
346
+
347
+ {
348
+
349
+ Destroy(this.gameObject);
350
+
351
+ animCon.SetBool("Dead", false);
352
+
353
+ }
354
+
355
+
356
+
357
+ // ■向きを変える動作の処理
358
+
359
+ void MukiWoKaeru(Vector3 mukitaiHoukou)
360
+
361
+ {
362
+
363
+
364
+
365
+ Quaternion q = Quaternion.LookRotation(mukitaiHoukou); // 向きたい方角をQuaternion型に直す
366
+
367
+ transform.rotation = Quaternion.RotateTowards(transform.rotation, q, kaitenSpeed * Time.deltaTime); // 向きを q に向けてじわ~っと変化させる.
368
+
369
+
370
+
371
+
372
+
373
+ }
374
+
375
+
376
+
377
+
378
+
379
+ // ■移動する動作の処理
380
+
381
+ void IdoSuru(Vector3 idosuruKyori)
382
+
383
+ {
384
+
385
+ charaCon.Move(idosuruKyori * Time.deltaTime * idoSpeed); // プレイヤーの移動距離は時間×移動スピードの値
386
+
387
+
388
+
389
+ }
390
+
391
+
392
+
393
+ public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
394
+
395
+ {
396
+
397
+
398
+
399
+ }
400
+
401
+
402
+
403
+
122
404
 
123
405
  }
124
406