I need to make the camera follow only the local player on Unity Netcode, but there is a twist

Hello! I am trying to do a multiplayer kart game similar to Mario Kart, but when i was doing the network scripts for the camera i had a problem.

I want to have the camera to only follow the local player, like im playing in the editor and my camera only follows me while the camera on the build only follows the player in the build.

But the camera CAN’T have any parent relation with the player, cause if it do it will have the same rotation and it looks janky.

I tried instantiating the camera as the player connects to the network, but it creates 2 cameras but they don’t know which player to follow.

WHAT I AM TRYING TO DO

The only way i got it working was making the camera a parent of the player, because it can get the transform of the player it is attached to, not EVERY player in the scene.

I need a way to make that camera child not follow the same rotation as the parent (player), because it script already have a specific type of camera follow that looks fine to me.

OR

Have a way to instantiate the cameras apart from the player so they are not their child but still have to get the transform of their respective local players. Example ( Player 1 connects and instantiates a camera, so this camera gets this players script and transform that instantiated it, the same thing for the Player 2)

The solution to this is very simple provided you understand the mental model of networking. Which not few initially struggle with, it’s just something you need to go over and over again until it finally clicks. :wink:

So when you spawn a network player, you get the OnNetworkSpawn event. Inside, you can check for IsOwner and if true, you instantiate (or simply SetActive) the camera object. Since there’s only one owner, and the owner of a player object is the local player, this will create just one camera for the local player. I assume you’re missing this check or you’re doing it at the wrong time (ie Awake) so that every client instantiates a new camera for every player.

I have all my cameras in the root of the scene. In fact I have one camera for each local player (up to four) and then instantiate Cinemachine cameras for each player, since they can toggle between first, third person and top-down views. There’s also a Cinecam for the “Press button to join” in a splitscreen scenario. I strongly recommend to adopt Cinemachine since you will write far less, even no scripts at all to implement the actual camera behaviour.