- Home /
Multiplayer Layers and Colliders confusion.
Ok so how would I avoid different players having things like raycast and so on colliding with specific things that are only meant for the specific player?
Like in my case I have a box collider around the player which detects certain things like a ray which is cast from the player. So I want to make sure that other players raycast also does not hit it and cause problems.
How to avoid it?
Answer by Tomer-Barkan · Nov 16, 2013 at 11:18 PM
One way would be to have different layers for different players, and then setting up the layer collision matrix so that the different layers do not collide with each other.
A second option would be to have an owner id in the scripts, and then checking in OnTriggerEnter()
whether the item collided with belongs to the correct player. This will only work on trigger colliders though, since non-trigger collisions will be resolved by the physics engine (the layer collision matrix will work for both)
Well the collider is a trigger. What I mainly(if not the only) thing I want to avoid is other players raycast hitting it.
Ok so how would I get each new players collider into its own layer... Or give it a unique ID like you say?
The second option seems better, would it also be better on the cpu?
See the link in my answer to find out how to set the layer collision matrix, and tell unity which layers may collide to which other layers.
See this link about how to define and assign layers to objects:
http://docs.unity3d.com/Documentation/Components/Layers.html
The second option requires a bit more calculations, since the collision takes place and only when handling it do you decide to ignore it. Not sure how much worse though, you could do some benchmark testing to find out... If the layer matrix works for you, use it, it also requires less code.
Oh ok I think I know what to do now...
Set the raycast to only hit a certain layer and give each players vision box its own layer which will be the layer per player.
Confused myself a little on how to do that but I'll ask that later.
Ok.
Your answer
Follow this Question
Related Questions
When using URP, colliders stop working 2 Answers
Error CS1061 I can't find a solution! [C#] 1 Answer
Collision bug. 2 Answers
How do you make collider interactions and movement smooth? 0 Answers
No Collision 3 Answers