問題の概要
PUN(Photon Unity Networking)を使って、オンラインマルチプレイのゲームを制作しています。
同期の確認のためにビルドして、Unityのプロジェクト上とビルドしたexeファイルでそれぞれルームに入ったのですが
どちらの画面でも後に入ったほうのプレイヤーしか操作できず、カメラのターゲットは動かせないほうのプレイヤーに照準が向いています。
実現したいことは、「3人のキャラクターをマルチプレイ環境で、別々にコントロールしたい(カメラもそれぞれにちゃんと追従する)」です。
下記のスクリプト中に問題のある箇所があったり、上述の問題が発生している
原因などがほかにありましたら、お教えいただければ幸いです。
よろしくお願いいたします…!
該当のソースコード
using
1using System.Collections.Generic; 2using UnityEngine; 3 4public class gameManage : MonoBehaviour 5{ 6 //誰かがログインする度に生成するプレイヤーPrefab 7 public GameObject playerPrefab; 8 9 void Start() 10 { 11 12 if (!PhotonNetwork.connected) //Phootnに接続されていなければ 13 { 14 Application.LoadLevel("Launcher"); //ログイン画面に戻る 15 return; 16 } 17 //Photonに接続していれば自プレイヤーを生成 18 19 GameObject Player = PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 3f, -9f), Quaternion.identity, 0); 20 21 22 23 24 characterMove characterMove = Player.GetComponent<characterMove>(); 25 characterMove.enabled = true; 26 Attack attack = Player.GetComponent<Attack>(); 27 attack.enabled = true; 28 29 } 30 31 // Update is called once per frame 32 void Update() 33 { 34 35 } 36 37 38}
using System.Collections; using System.Collections.Generic; using UnityEngine; public class characterMove : MonoBehaviour { public PhotonView myPV; public PhotonTransformView myPTV; public Camera mainCam; // このスクリプトで使う変数一覧 public CharacterController charaCon; // キャラクターコンポーネント用の変数 public Animator animCon; // アニメーションするための変数 public float idoSpeed = 5.0f; // 移動速度(Public=インスペクタで調整可能) public float kaitenSpeed = 1200.0f; // プレイヤーの回転速度(Public=インスペクタで調整可能) private Vector3 movePower = Vector3.zero; // キャラクター移動量(未使用) private float jumpPower = 10.0f; // キャラクター跳躍力(未使用) private const float gravityPower = 9.8f; // キャラクター重力(未使用) private Vector3 moveDirection; public void Hit() // ヒット時のアニメーションイベント(今のところからっぽ。ないとエラーが出る) { } // ■最初に1回だけ実行する処理 void Start() { //charaCon = GetComponent<CharacterController>(); // キャラクターコントローラーのコンポーネントを参照する //animCon = GetComponent<Animator>(); // アニメーターのコンポーネントを参照する if (myPV.isMine) //自キャラであれば実行 { //MainCameraのtargetにこのゲームオブジェクトを設定 mainCam.GetComponent<FollowingCamera>().target = this.gameObject.transform; } } // ■毎フレーム常に実行する処理 void Update() { /* if (!myPV.isMine) //自キャラであれば実行 { return; } */ //スムーズな同期のためにPhotonTransformViewに速度値を渡す Vector3 velocity = charaCon.velocity; myPTV.SetSynchronizedValues(velocity, 0); float holizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Rigidbody rigid = GetComponent<Rigidbody>(); moveDirection = transform.transform.forward * idoSpeed * Input.GetAxis("Vertical"); moveDirection.y -= 10f * Time.deltaTime; if (Input.GetKeyDown(KeyCode.Space)) { //上にジャンプする。 //Debug.Log(moveDirection); charaCon.Move(moveDirection * Time.deltaTime); //rigid.AddForce(Vector3.down * 80, ForceMode.Acceleration); } // ▼▼▼移動処理▼▼▼ if (animCon.GetBool("Attack") == true || animCon.GetBool("SPAttack") == true || animCon.GetBool("Dead") == true) { if (animCon.GetBool("SPAttack") == true) { Invoke("SPReset", 1.3f); } return; } if (Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0) // テンキーや3Dスティックの入力(GetAxis)がゼロの時の動作 { animCon.SetBool("Run", false); // Runモーションしない } else// テンキーや3Dスティックの入力(GetAxis)がゼロではない時の動作 { var cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; // カメラが追従するための動作 Vector3 direction = cameraForward * Input.GetAxis("Vertical") + Camera.main.transform.right * Input.GetAxis("Horizontal"); // テンキーや3Dスティックの入力(GetAxis)があるとdirectionに値を返す animCon.SetBool("Run", true); // Runモーションする MukiWoKaeru(direction); // 向きを変える動作の処理を実行する(後述) IdoSuru(direction); // 移動する動作の処理を実行する(後述) } // ▼▼▼アクション処理▼▼▼ animCon.SetBool("Attack", Input.GetKey("j") || Input.GetButtonDown("Attack")); // キーorボタンを押したらアクションを実行 animCon.SetBool("Guard", Input.GetKey("l") || Input.GetButtonDown("Guard")); // キーorボタンを押したらアクション2を実行 animCon.SetBool("SPAttack", Input.GetKey("k") || Input.GetButtonDown("SPAttack")); // キーorボタンを押したらアクション3を実行 animCon.SetBool("Jump", Input.GetKey("space") || Input.GetButtonDown("Jump")); // キーorボタンを押したらジャンプを実行(仮) if (LocalVariables.currentHP <= 0) { Debug.Log("死亡"); animCon.SetBool("Dead", true); Invoke("DeadReset", 8.0f); } } private void SPReset() { animCon.SetBool("SPAttack", false); } private void DeadReset() { Destroy(this.gameObject); animCon.SetBool("Dead", false); } // ■向きを変える動作の処理 void MukiWoKaeru(Vector3 mukitaiHoukou) { Quaternion q = Quaternion.LookRotation(mukitaiHoukou); // 向きたい方角をQuaternion型に直す transform.rotation = Quaternion.RotateTowards(transform.rotation, q, kaitenSpeed * Time.deltaTime); // 向きを q に向けてじわ~っと変化させる. } // ■移動する動作の処理 void IdoSuru(Vector3 idosuruKyori) { charaCon.Move(idosuruKyori * Time.deltaTime * idoSpeed); // プレイヤーの移動距離は時間×移動スピードの値 } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { } }
補足情報(FW/ツールのバージョンなど)
Unity 2019.2.3f1 (64-bit) です。
あなたの回答
tips
プレビュー