- Home /
Networking - Camera Swapping to Target Incorrect Player
I spawn a prefab that has an camera child and the player should just see through that camera. If there's just one player on the network, he has the right cam, but when the second player joins, he changes his camera with the one from him. Each camera is a child of each player. I use a prefab, and the camera begins disabled. I attempt to enable it in the player script with the following code:
function Awake(){
GetComponent(Camera).enabled = true;
}
However, I'm still getting this strange result. When I add a second player to the game, the first player's camera gets moved to the second player, while the second player's camera follows the first player, but the controls are correct. In short, the cameras swap places.
Should I change my camera setup, or is there a way to make this work? Thanks!
Answer by syclamoth · Nov 27, 2011 at 01:36 AM
You need to check for if the camera is local!
function Awake() {
if(networkView.isMine)
{
camera.enabled = true;
}
}
If the camera script itself doesn't have a network-view component, use the one from the player instead.
You didn't mention that you need to add the camera then disable it so that this code will work but thank you :)
Answer by gevarre · Dec 08, 2011 at 08:49 AM
I've got the same situation: I've got a prefab vehicle (named HoverRig_Prefab) with a camera further down in the hierarchy. The top level node has the network-view component and I put your camera-enabling code also on the top node, but when I start the game, I get this error message:
MissingComponentException: There is no 'Camera' attached to the "HoverRig Prefab(Clone)" game object, but a script is trying to access it. You probably need to add a Camera to the game object "HoverRig Prefab(Clone)". Or your script needs to check if the component is attached before using it.
I'm not sure what you mean by using the network-view component from the player, but could that be the problem, or do I need to do something else?
You need to add a camera onto the HoverRig then disable it. If you mean in the code then make sure you use camera not Camera they are different
Your answer
Follow this Question
Related Questions
mirror player camera using one camera 0 Answers
Swapping between Diffuse and Transparent/Diffuse Shaders 0 Answers
Broadcast camera render over network 2 Answers
Multiplayer camera switch issue 0 Answers