How do I give each player their own camera?

Hi,

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!

1 Like

disable all cameras by default. enable only the camera with that player’s player prefab.

Well that didn’t work,…

here’s what I tried:

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).

that did it!

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;

now it works beautifully. Thanks for the help!

2 Likes

thanks, ive been having the same issues. Well done!

instead of downloading the enitre M2H project to see what code you modded, can you upload the entire script?

he gave you all the info needed, tutorial_3_playerscript…

1 Like

i know but i dont want to have to download that whole project just to get one script from it…

It has been a while since this thread was last updated, but are you sure you want to run that in Update()?

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.

1 Like

Thank you sooooo much for publishing your answer!

I had something similar, but way too complicated - and it didn’t work. This even I can get my head around - so thanks again!

Arthur

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.

Oops!

I forgot to check the network tutorial. Thanks for reminding me!

It is way more simple than I thought. I had a script that created cameras, destroyed cameras, all the works… and, to round it all off, didn’t work.

Thanks legend411!

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?

This is what I used to snap the camera to a object with a tag of player rifle which is just above the guys gun so it FPS…

Youtube: http://www.youtube.com/watch?v=OxbWcDxcBsE

var PlayerScope : GameObject;
function LateUpdate()
{

//Creates an array of gameobjects that have tag “PlayerRifle”
var rifleArray : GameObject[ ] = GameObject.FindGameObjectsWithTag(“PlayerRifle”);

for(rifle in rifleArray)
{
if(rifle.networkView.isMine)
{
PlayerScope = rifle;
}
}
if(PlayerScope.networkView.isMine)
{
transform.position = PlayerScope.transform.position;
transform.rotation = PlayerScope.transform.rotation;
}
}

Hi sorry for the noob question but how to do this ? What you meant is not add component isnt it ?

I use Unet (the newst one) and I had the same issue. It works perfectly for me ! thx !

Instead of Update, you can just put the camera assignment code into OnStartLocalPlayer.

1 Like