RaycastHit.textureCoord is broken?

Can anyone please confirm if RaycastHit.textureCoord is actually working? No matter what I do it always returns 0,0. I have a Convex MeshCollider on a UV’d cube mesh.

I even recreated the basic example from the docs RaycastHit-textureCoord and that doesn’t work either.

Via debugging, I have determined that the raycast is hitting the correct object, and returns the correct texture name.

It does work, but not with a convex mesh collider. It only works with a normal mesh collider. If you check “convex”, Unity will create a new mesh which will be the convex hull of the original mesh. Therefore the meshcollider has no logical relation to the original mesh and doesn’t have texture cordinates at all. The convex mesh usually looks different from the original mesh.

You only need a convex collider if the object is used as rigidbody. If you need both, you can add two colliders, one convex and a normal one. Place them on different layers to control the behaviour / interaction.

I’m not sure i understand what a shader has to do with all that. A collider is a pure logical object that has no relation to rendering. So a collider always works the same, no matter if the object is actually visible.

Reading or writing pixels to a texture requires the texture to be marked as read- / writeable. Textures that are created at runtime are automatically read/writeable.

If you experience problems with changing textures and not seeing any result in the scene might be related to (static) batching. Do not mark an object as static if you change anything on that object. If an object is batched the original renderer becomes useless as the object is now part of a big (batched) mesh.

Ok, I figured it out.

When importing the mesh, you must tick Generate Colliders to add the Mesh Collider, instead of adding one yourself. and make sure Convex is unticked on the Mesh Collider component.