Same Mesh, different physics material per face
Hi,
I have a question about physics materials. How can I script (C#) it so that each face has a different physics material value.
For example a Cube.
Top face = no friction on the physics material. Bottom face = Low friction on the physics material. Sides x 4 face = Medium friction on the physics material.
Thanks! Sharing is caring!
Answer by HenryStrattonFW · Jan 20, 2017 at 11:23 PM
The only way I can think of to get one cube mesh to have multiple materials would be do have 4 separate collider components, each set up to cover one face of the cube, that way each collider could be set to a different physics material. So one cube mesh, then slim box colliders configured to cover one face each.
Might be another way but I'm fairly sure this would be a relatively simple way to achieve this.
@ HenryStrattonFW Yea that's what I'm doing now. I have a bunch of boxcolliders "overlapping" the different sides, but I'm not sure if that's the "cleanest" way to do it? Was wondering if there was a code way to handle it ins$$anonymous$$d.
Thanks for the suggestion.
Sharing is caring!
The only way I can think of to do it in code would in my $$anonymous$$d be just as messy to be honest but for the sake of completeness here goes. You could various position checks and or raycasts to work out which surface of the cube a target object can be colliding with (assu$$anonymous$$g only a single convex shape colliding with the cube) you could then have one cube collider and just swap the Physics$$anonymous$$aterial based on which face is able to be hit (if you consider a sphere and a cube, there is only ever one face that it could interact with at a time).
However this sort of approach would only allow one object to be properly utilised alongside the cube as the second you have two objects you could have two different faces "active" at once which is then unsupportable with only one physics material.
Sorry I missed your reply. You're right, it could get complicated really fast depending on the collider mesh's complexity.
Guess I have to stick with various box colliders on different sides of the mesh then.
Thanks for helping me bounce ideas.
Sharing is caring!