How to make cameras per player.
Options
So I was using the default unity FPSController, but then upon joining a multiplayer game, the camera would switch to another's. So how can I make a camera per player and make it so that's the only one activated for them?
I tried creating a camera per player join, yet I found myself back at square one.
(I looked online, yet there were not a lot of results.)
Thanks in advance!
I tried creating a camera per player join, yet I found myself back at square one.
(I looked online, yet there were not a lot of results.)
Thanks in advance!
0
Comments
-
There are a few ways to do this, but what I do is the following.
Have 1 camera in the scene on a gameobject that is not a child of anything, so at the root level.
Have an instance script on this so I can control it easily (other camera views, etc...).public class MainCameraController : MonoBehaviour { public static MainCameraController instance; void Awake () { instance = this; } // Setup the camera with the default view public void Configure(Transform parent) { gameObject.transform.parent = parent; gameObject.transform.localPosition = new Vector3(0, 100, 0); gameObject.transform.localRotation = Quaternion.Euler(90f, 0, 0); } }
Then for the server setup the camera in the script which spawns the playerpublic void Spawn() { if (BoltNetwork.isServer) { //Setup the main camera for the players ship MainCameraController.instance.Configure(entity.transform); } }
For the clients, setup the camera when they are given access to the player gameobject the server spawns[BoltGlobalBehaviour(BoltNetworkModes.Client, "Game")] public class ClientCallBacks : Bolt.GlobalEventListener { public override void ControlOfEntityGained (BoltEntity arg) { //Setup the main camera for the players ship MainCameraController.instance.Configure(arg.transform); } }
You'll need to set the correct position and rotation of the camera, for what you want it to look like.
Hope that gives you some ideas on how to do it1 -
Thanks for the reply. I was able to code the 1st and 3rd code block. But for the 2nd, I am confused on where to put it. This is currently where I create the player object
http://pastebin.com/TGZJ9GcC
That is in a C# file created by following the getting started 101 tutorial.0 -
You should be able to put the second script into there ok.
E.g.[BoltGlobalBehaviour] public class NetworkCallbacks : Bolt.GlobalEventListener { public override void SceneLoadLocalDone(string map){ var pos = new Vector3 (268, 2, 163); BoltNetwork.Instantiate (BoltPrefabs.RigidBodyFPSController_1, pos, Quaternion.identity); if (BoltNetwork.isServer) { //Setup the main camera for the server player MainCameraController.instance.Configure(entity.transform); } } }
The other option is to use server and client call backs, and control the camera being added from there.
Chapter 2 of the advanced tutorial (which just happens to have info about cameras) covers the details on the server / client callbacks.0