- Home /
[Photon Multiplayer] One Camera for all player
Hello. I am working on a multiplayer game in unity using Photon (2D game). What I was using before to make the camera move with the player, is that I made the camera , a child of the player , so that the player gets instantiated with its own camera and the camera follows the player. Now the problem is, as it is a 2D game (platformer like), I want the camera to follow the player with smooth movement (.ie I want to use Lerp). So I made the camera not a child of the player and only camera is available in the scene.What I want is to get the transform of the player that I am controlling and set it to the camera's transform. Like wise , in other's game , the camera should get the transform of that particular player , that they are controlling.
What I am using in my camera script is:
void Update () {
if (GameObject.FindGameObjectWithTag ("Player").GetComponent<PhotonView>().isMine) {
//I want to get the transform of that particular player.
}
}
Or is there any other way to achieve this?? Please Help!!
Answer by Captain_Pineapple · May 07, 2019 at 01:47 PM
Hey there,
most likely you will have a script on your player object to control it (Lets call it "playerlogic"). You should use that script to deal with your problem. In the start function of your "playerlogic" script your should ask for the photonView.ismine
status. If that is true, then set the "playerlogic"s gameobject as "to-follow" object to the main camera script. (You should be able to find it by Camera.main.GetComponent<yourcamerascripthere>().createSomeAssigneFunction(gameobject)
)
Why should you do it like this? Because performance!
When you use FindGameobject this costs a lot of calculationtime. You should always avoid using this in an update. Give your camerascript a gameobject variable of the gameobject it should follow and save the players gameojbect as reference. Never search for it twice.
Hope this helps you. In case there are some questions left or something was unclear let me know.