- Home /
Photon Unity Networking - Player Camera Instantiate
Hello, I'm trying to integrate PUN 2 in a working game. It is basically an arena where each player can control a ball; it should be a mobile game but for now I'm testing it as a desktop build. The ball is working correctly in single player mode.
Using PUN documentation and examples, I've made a login panel to go into a room (main scene). If I do it with 1 player, everything works fine: I login, create a room, join it and can control the player inside the main scene. If I log 2 players, from 2 different game instances, the player controls will be swapped (player 1 will be controlling player 2 and vice-versa).
In the lobby, I use this to get into the main scene (called "Test"):
public void OnStartGameButtonClicked()
{
PhotonNetwork.CurrentRoom.IsOpen = false;
PhotonNetwork.CurrentRoom.IsVisible = false;
PhotonNetwork.LoadLevel("Test");
}
Inside the main scene, I have a GameManager object with a GameManager script inside it (following PUN standards). In the GameManager script, I run this method to instantiate players:
private void StartGame()
{
GameObject player = PhotonNetwork.Instantiate("OnlinePlayerPrefab", SpawnPoints[0].position, Quaternion.identity, 0);
GameObject camera = player.transform.Find("Main Camera").gameObject as GameObject;
CameraController camController = camera.GetComponent("CameraController") as CameraController;
camController.target = player;
Player playerMainScript = player.GetComponent("Player") as Player;
GameObject playerUI = Instantiate(playerMainScript.playerUIPrefab);
playerMainScript.variableJoystick = GameObject.Find(playerUI.name).GetComponentInChildren<VariableJoystick>();
playerUI.SendMessage("SetTarget", playerMainScript, SendMessageOptions.RequireReceiver);
}
}
It basically instantiates a player prefab; then it finds the camera (that is a child object of the player prefab) and then gets the "CameraController" scripts inside it and sets the camera target to the player prefab that has just been instantiated. It then instantiate a "Player" (the script for the player movement and several commands), and the instantiates an UI that is defined in the Player script. The player UI contains buttons for movement of the player, so we set the instantiated ball as its target.
Here is the code of the Player script, if it can be useful to understand: https://pastebin.com/p95MMq9J And here is the PlayerUI script: https://pastebin.com/jA5ivq51
Here is the Player prefab structure and the main scene hierarchy:
You can see that I have a (non networked) canvas in the scene; each player will put its PlayerUI prefab in there; could this be causing the swapping issue?
What I've tried so far: Instead of having the Camera as a child of the player prefab, I tried (in the GameManager script) to use PhotonNetwork.Instantiate to create a camera; this isn't working, as when I try to log in, no camera is found.
Really can't get this problem solved in any way. Really appreciate any solution or tips to start from :) Thank you!
Your answer
Follow this Question
Related Questions
How do I instantiate and set the parent of a Gameobject using Photon? 0 Answers
Photon Networking Instantiating Problem 0 Answers
Player Won't Instantiate with Photon Unity Network [PUN] 0 Answers
OnConnectedToMaster() vs IsConnectedAndReady? 1 Answer
script always instantiates monster even when its not supposed to? 1 Answer