I’ve been using the zero 2 hero (M2H) networking tutorial to try and wrap my head around the whole multiplayer networking thing.
I want to change the camera so that each player has their own camera that follows them around.
I created a player prefab that has a camera attached to it. Each player spawns perfectly, but for some reason, the game only ever follows the last camera that was created.
I tried looking around, but I couldn’t find anything!
All cameras were disabled by default. locally on the each cloned player, I added code that enabled that particular camera.
I think I know what the problem might be though. I’m using an authoritative server, and the server spawns all the players, and along with them, their cameras.
I’m just not sure how I should spawn a camera on the client and tell it to follow the client character…
You’ll have to have some way for the client to know what camera/character is supposed to be theirs so they can enable their camera locally (the client needs to execute the code).
just for the sake of posterity, here’s what I had to do…
I created a prefab with the player and the camera.
I disabled the camera and the audio listener in the prefab.
I created two vars in the tutorial_3_playerscript:
public var myCam : Camera;
public var myAudioListener : AudioListener;
the Update function has an if statement that checks who the script owner is. Inside that statement I added:
function Update(){
//Client code
if(owner!=null Network.player==owner){
//Only the client that owns this object executes this code
if (myCam.enabled == false)
myCam.enabled = true;
if (myAudioListener.enabled == false)
myAudioListener.enabled = true;
Personaly I would put it in a function that gets called when the player joins the game and is finished loading the level. Somewhat wastefull in Update but I dont think its a performance issue with the little thats going on. (unless you do this everywhere)
Piling things into update can really get ugly fast and should be avoided if possible.
For the network game I am working on now, each client as a non-networked camera which is bound to their player. It works reasonably well, but I am not pushing for a super secure game as players can freely see the entire map. When you spawn, the camera mounts back to your player and that’s that.
If you look at the cars or third person examples in the Networking Example package from the Unity3d.com resources section, you’ll see the solution is much simpler than you think. You only really need one camera in the scene with a null target variable, and create a script on your player prefab so that when he spawns, check if NetworkView.isMine, and set the camera target to be that player locally.
No problem dude… This stuff is confusing as hell to me too, but I’ve been playing with it every day for a few days, and I think I’m finally starting to understand most of it a little better. RPC’s are amazing. What kind of game are you making?