実現したいこと
PUN2を使用して2プレイヤー用のゲームを作りたいです。
メニュー画面からプレイヤー1とプレイヤー2を選び
1プレイヤーは人間(A・Sキーで左右移動)。2プレイヤーはその人間が持っているボール(Z・Xキーで左右移動)です。人間が親オブジェクトでボールが子オブジェクトです。
発生している問題・分からないこと
実行すると、
プレイヤー2側がボールを動かしても
プレイヤー1側から見てプレイヤー2側のボールが動きません。
該当のソースコード
Aの移動
1Aの移動 2using Photon.Pun; 3using Photon.Realtime; 4using UnityEngine; 5 6 7public class Aの移動 : MonoBehaviourPunCallbacks 8{ 9 public GameObject A; 10 11 12 13 14 15 // Start is called once before the first execution of Update after the MonoBehaviour is created 16 void Start() 17 { 18 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 25 if (!photonView.IsMine) return; 26 { 27 28 29 if (Input.GetKey(KeyCode.A)) 30 { 31 A.transform.position += transform.right * 0.1f; 32 33 } 34 if (Input.GetKey(KeyCode.S)) 35 { 36 A.transform.position -= transform.right * 0.1f; 37 38 } 39 40 41 42 43 44 } 45 } 46 47 48 49 50 51 52 53 54 }
Bの移動
1Bの移動 2using Photon.Pun; 3using UnityEngine; 4using Photon.Realtime; 5 6public class Bの移動 : MonoBehaviourPunCallbacks 7{ 8 9 10 11 PhotonView operatorView; // 操作権を持つプレイヤー 12 // Start is called once before the first execution of Update after the MonoBehaviour is created 13 void Start() 14 { 15 16 } 17 18 // Update is called once per frame 19 void Update() 20 { 21 if (photonView.IsMine) return; 22 23 { 24 25 if (Input.GetKey(KeyCode.Z)) 26 { 27 transform.position += transform.right * 0.1f; 28 // B.transform.RotateAround(A.transform.position, Vector3.forward, 50 * Time.deltaTime); 29 } 30 31 32 if (Input.GetKey(KeyCode.X)) 33 { 34 transform.position -= transform.right * 0.1f; 35 //B.transform.RotateAround(A.transform.position, Vector3.forward, -50 * Time.deltaTime); 36 } 37 } 38 39 40 } 41 public void BBB() 42 { 43 photonView.TransferOwnership(PhotonNetwork.LocalPlayer); 44 } 45 46} 47 48 49 50 51
メニュースクリプト using UnityEngine; using UnityEngine.SceneManagement; public class メニュースクリプト : MonoBehaviour { public static byte type; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { type = 0; } // Update is called once per frame void Update() { } public void B() { type = 1; SceneManager.LoadScene("ゲーム画面"); } public void B1() { type = 2; SceneManager.LoadScene("プレイ画面"); } }
PUN2
1PUN2 2using Photon.Pun; 3using Photon.Realtime; 4using UnityEngine; 5 6using UnityEngine.UI; 7 8public class PUN2 : MonoBehaviourPunCallbacks 9{ 10 public static int A; 11 12 13 public Bの移動 b; 14 15 16 17 public GameObject f; 18 19 private void Start() 20 { 21 22 if (!photonView.IsMine) 23 { 24 enabled = false; 25 return; 26 } 27 28 // PhotonServerSettingsの設定内容を使ってマスターサーバーへ接続する 29 PhotonNetwork.ConnectUsingSettings(); 30 } 31 32 // マスターサーバーへの接続が成功した時に呼ばれるコールバック 33 public override void OnConnectedToMaster() 34 { 35 // "Room"という名前のルームに参加する(ルームが存在しなければ作成して参加する) 36 PhotonNetwork.JoinOrCreateRoom("Room", new RoomOptions(), TypedLobby.Default); 37 } 38 39 // ゲームサーバーへの接続が成功した時に呼ばれるコールバック 40 public override void OnJoinedRoom() 41 { 42 43 44 45 46 47 48 49 50 51 52 53 54 byte type = メニュースクリプト.type; 55 // ▼ ボタン1:Aを生成 56 if (type == 1) 57 { 58 textA.enabled = true; 59 60 //インスタンス生成 61 GameObject s = PhotonNetwork.Instantiate("Sp", transform.position, Quaternion.identity); 62 63 64 65 66 67 //AからはBは動かない。 68 var c = f.GetComponentInChildren<Bの移動>(); 69 c.enabled = false; 70 71 72 } 73 74 // ▼ ボタン2:AがあればB操作をON 75 if (type == 2) 76 { 77 78 textB.enabled = true; 79 80 // まず A があるか確認 81 GameObject a = GameObject.FindWithTag("A"); 82 83// 2. 生成したインスタンスの子からスクリプトを探す 84 var c = f.GetComponentInChildren<Bの移動>(); 85 c.enabled = true; 86 87 88 89 90 91 92 //Aの移動をオフにする。 93 var A = f.GetComponent<Aの移動>(); 94 A.enabled = false; 95 96 97 98 99 100 101 } 102 } 103 104} 105 106 107 108 109 110 111 112
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
子オブジェクトのオブジェクト同期ができない。
補足
特になし
2026/07/09 04:01
2026/07/09 06:01