- Home /
Rendering players per camera
Hey guys, I'm hoping you can solve this one for me, as It's giving me quite a headache!
I'm working on a multiplayer game, but it also has splitscreen. Rendering the players for the network players is fine, as you don't have to draw the first person weapon, legs, etc.
Here's where the problem is. For the split screen players, you not only have the player model, but the weapon too. What I've tried doing already is getting the separate list of renderers of the first person weapon, and the first person camera, and tried to force the camera to not render it, but that doesn't seem to work.
Here's the issue I have in more of a visual form:
The green is what I want the other splitscreen players to see in their camera (so basically, just the other split screen players body, not their weapon). The red is what I want the splitscreen players to see only in their camera (their own fps weapon).
I tried using 1 layer called invisible which I add the renderers to a list where the layer is changed OnPreCull(), but to no avail.
I hope you guys can solve my issue here!
Okay guys, I managed to answer my own question!
What you need to do is get the index of your player number (for instance, player 1 is index 0, player 2 is index 1, etc).
Then you make two Hide0, Hide1, layers or how many layers you want for the amount of players you can have.
Also, make layers for Show0, Show1, etc.
Now here comes the tricky part. You have a script on your camera with a function that adds the culling layermask to the ones you DONT want to be shown:
public int HideLayer;
public int ShowLayer;
public void SetCullLayer(int playerindex)
{
// Sets the culling mask
camera.culling$$anonymous$$ask &= ~(1 << Layer$$anonymous$$ask.NameToLayer("Hide" + playerindex));
// Hides all other show layers. Change 3 to whatever number you want for the layers
for (c = 0; c < 3; c++)
{
if (c != playerindex)
camera.culling$$anonymous$$ask &= ~(1 << Layer$$anonymous$$ask.NameToLayer("Show" + c));
}
// Sets the layers
ShowLayer = Layer$$anonymous$$ask.NameToLayer("Show" + playerindex);
HideLayer = Layer$$anonymous$$ask.NameToLayer("Hide" + playerindex);
}
So all of player 1's third person meshes (that they dont want to see) are now added to the Hide0 layer. So now Player 1 can't see their third person body!
Now all thats left is hiding the first person weapon from the OTHER players:
All you do is check to see if your layer SHOULD be shown, like so:
// Hides all other show layers
for (c = 0; c < 3; c++)
{
if (c != playerindex)
camera.culling$$anonymous$$ask &= ~(1 << Layer$$anonymous$$ask.NameToLayer("Show" + c));
}
And then finally you call the OnPreCull to do all the layer setting:
void OnPreCull()
{
for (o = 0; o < Hide$$anonymous$$eshes.Count; o++)
Hide$$anonymous$$eshes[o].layer = HideLayer;
for (f = 0; f < Show$$anonymous$$eshes.Count; f++)
Show$$anonymous$$eshes[f].layer = ShowLayer;
}
Answer by BerggreenDK · Nov 18, 2013 at 12:13 AM
I would use 2 different cameras, placed two different places and then let the camera output cover half of the screen.
I have used multiple cameras before as HUD effects, rendering some different part of the stage/scene where I had the scores, pickup objects etc. Worked fine. You can render without background too, if you need the dashboard to be transparent. But in your case I would setup two different cameras, vertical or horisontal beside each other.
Once you have two cameras, its even more simple to draw the contents, isnt it?
For each camera, you can also specify a culling mask. If you place the "dont show this in camera 1" objects in a seperate mask and the "dont show this in camera 2" objects in another. It seems quite easy to me.
let me know if this works for you.
If I use more than one camera, then I won't be able to preserve the shadowing on the weapons. I'd also like to keep the game as tuned for performance as possible. Having 2 cameras per player, with the ability to have 4 players at once = 8 cameras potentially. Ouch.
I'm sure there's a way you can do it using Unity's culling system, I just think I'm going about it the wrong way.
I'd definitely like to not go the standard '2 Camera' way though, as there's loss in shadow quality and performance.
Your answer
Follow this Question
Related Questions
How do I render only a part of the cameras view? 2 Answers
Render group of objects in front? 0 Answers
Camera with shader? 2 Answers
Can two cameras render at different speeds? 1 Answer
How do I force my cameras to render in a certain order in URP? 1 Answer