Multiplayer Camera Layers
I am having trouble layering the cameras for a multiplayer FPS game. One camera on the player shows the gun and the other camera shows everything else (so the gun doesn't go through walls and what not). My problem is that the gun camera is rendering everyone else's guns on top of the surrounding objects. How do I make it so the camera only renders the gun that the player is holding on top of everything else. I hope that made sense. Thankyou
Did you find a solution for this? The struggle is real right now.
It was 4 years ago now, I'm trying my hardest to work out what I did but I am really unsure how I ended up fixing it.
I'm not sure if this is the solution as I haven't done any multiplayer in years but my guess would be:
Start with all guns on the normal camera
When the player is spawned in switch the gun to the "gun camera" This should make the gun only move to the gun camera for the local client and not everyone else on the server, thus fixing the problem.
I'm not sure if this will work but it's my best guess. I hope you find a solution soon.
Thanks for responding! I don't expect you to but hoping one day someone can answer it.
I'm closer to understanding from this video here:
https://www.youtube.com/watch?v=QhKlvdixOX0
This video explains it but it's still going over my head.
gameObject.layer = Layer$$anonymous$$ask.NameToLayer("YourLayerName");
I understand that this sets the layer if you load it after a player has spawned, but wouldn't that just be the same thing? If every spawned player had this piece of code run when they spawned, the result would be the same...
I'll try explain this with my limited knowledge but from my understanding the code
gameObject.layer = Layer$$anonymous$$ask.NameToLayer("YourLayerName");
will only run on the client not the server as it hasn't been specified to, so the following things will happen:
All guns start rendered by the main camera (as they are on the main layer)
Players spawn in and see every gun rendered by the main camera, this will mean they are not rendered in front of everything else (like the problem your having), but causes them to go through objects in the first person view.
You then switch, through code, the layer that the Player's (and only the local player, not every player) gun is on. Now the gun is rendered only by a "gun camera", stopping them from clipping through objects in first person.
So now every gun in still being rendered by the main camera, except the local player's gun.
Because the code is run locally it doesn't effect all the other players guns and will only change the local players gun. This happens for every local player and thus should fix your problem.
If you still need help, let me know
Thank you! That was the answer I was looking for. The video did not mention that properly. Appreciate it!