- Home /
Assigning a (main) Camera to Spawned Prefabs
I've been following along the Unity Networking Tutorial (unity3d.com/learn/tutorials/s/multiplayer-networking) and I've been trying to get it so that the main camera follows each local player as they spawn.
How would one go about doing this? I've tried making the prefab have a child camera but it break when another person spawns as the host then sees themselves move through the eyes of the second player, but I've read it's better for this main camera follow approach.
I've also looked at this post (particularly Loahrs reply), but haven't been able to make it work (https://stackoverflow.com/questions/36261871/how-to-attach-the-camera-to-a-player-object-instantiated-by-hlapi-network-manage).
I've been stuck for some time, so any help would be appreciated :)
Update: The CameraFollow().target part of Loahrs code comes up as an error (this is part of the PlayerController script on the player prefab).
public override void OnStartLocalPlayer() {
//function is only called for local player
//Turns local player blue
GetComponent<MeshRenderer> ().material.color = Color.blue;
Camera.main.GetComponent<CameraFollow>().target = transform; //Fix Camera on "me"
}
I'm trying to work out what is wrong with the line but I can't find Unity manuals on it.
Have you tried setting the depth of the second camera to a different number?
I'm not entirely sure how changing the depth of the main camera would help, as there's only one camera being rendered; it should just locally follow different players (If this is in response to my attempts at having child cameras of the prefab, I was just trying to explain what I've already attempted. I also think the one camera would be easier and better to work with).
Answer by Codez · Dec 15, 2017 at 03:07 AM
Finally found the issue. The reason my code wasn't working was because 'CameraFollow' was trying to access a script of the same name (which mine wasn't) and the '.target()' referencing a function within the 'CameraFollow' script.
A helpful link to anyone else stuck: https://answers.unity.com/questions/1157437/making-my-camera-follow-player-in-multiplayer.html
Answer by Edgar_94_ · Dec 15, 2017 at 01:25 AM
The best practise I have found is that you have a FPS Player with a camera integrated in this prefab AND a TPS Player WITHOUT a camera integrated. This way, if a player is not local, you can only see him looking for it in the scene.
I don't know if I have explained well, but here is an schematic:
3rd person prefab with no camera -> Remote Player 1st person prefab with camera -> Local Player
This way you will find it more easy ;)
I'm not following what you're suggesting... Is the player controlling a 1st person prefab through a 3rd person prefab while viewing the 1st person prefab camera? If so, I'm not sure how this would help. In the context of the Unity Networking Tutorial, what are you saying should be modified?
Your answer
![](https://koobas.hobune.stream/wayback/20220612143805im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with repositioning object to a random position in the FOV 1 Answer
Calculate distance at which an object occupies 0 pixels, given bounding sphere? 1 Answer
New gameobject to spawn in front of editor camera?, 1 Answer
Unet: Spawner spawns for every player one object instead of just one 0 Answers
Having Trouble with Instantiating an object on an axis 2 Answers