I’ve got two player objects, and two layers: “Player1” and “Player2”, respectively. I’ve got a rope that gets created during runtime that gets placed in layer “Rope1”. I have my collision matrix set so that the “Player1” and “Player2” layers are unable to collide with the “Rope1” layer.
When the rope gets created at runtime, I manually set the layer on the rope like so:
myRope.layer = 9;
In this case, 9 corresponds with the “Rope1” layer. Unfortunately, the rope still collides with both the “Player1” and “Player2” layers. Is there something else I need to do in order to make the rope object ignore the player layers? I can’t change the layer in the editor, because it’s created via plugin without a prefab.
I took a few screenshots that you can see here: Imgur: The magic of the Internet



The rope is being correctly assigned to the “Rope1” layer, and according to the collision matrix, “Rope1”, and “Player2” should not collide, but the image clearly shows them colliding.
Am I missing something obvious?
2 Answers
2
You cannot assign a physics layer by setting the .layer to the number of the layer. There is a special encoding, which allow for multiple layer masks to be turned on and off using the same number.
To assign the correct number just create a LayerMask public attribute on a MonoBehaviour. This will allow you to edit the value using a drop down in the inspector. Then you can print the value from the script and see what number comes out. You can also learn the encoding. There is another answer here which explains it - How do I use layermasks? - Unity Answers
Solved it!
For this project, I’m using QuickRopes2. Turns out, when it creates a rope, the series of joints that it uses are hidden in the inspector, which makes it hard to see them. In looking at the “GenerateJointObjects()” function, it manually creates a series of joints, then assigns them a layer based on the parent.
Because I was changing the layer of the parent after the rope was created, none of the joints were properly layered. Instead, I had to add a line to GenerateJointObjects() function to set them to my desired layer instead.
Apologies everyone! Turns out, I was being a bit foolish in not looking at the code I was using.
Are you sure layer 9 is "Rope 1"? If the layers are zero indexed and listed in ascending order in the layer matrix (which I thought was the case but I don't have Unity open right now to confirm), layer 9 would be "Player 2" and "Rope 1" should be 5.
– HuacanachaLayer 9 is in fact "Rope1". Unity has a couple empty layers that it didn't want me to edit. In the first image, you can see in the top right that the layer is set to "Rope1".
– TheDigitalistic