0
\$\begingroup\$

I have a gameobject with a mesh collider. The mesh is simply a flat plane.

I raycast to the mesh and I'd like to get the index on the material's basemap where I got a hit.

I assumed hit.textureCoord or hit.textureCoord2 would do the trick, but all I get is either (0,0) or (1,1) and nothing inbetween.

I triple checked and the collider is definitely a mesh collider, so I can only assume I misunderstood what textureCoord does

\$\endgroup\$
4
  • \$\begingroup\$ You know that texture coordinates are expressed in a 0-1 range, so 0.5 is the middle of the texture? By "index" do you mean pixel coordinate in a texture? If so, just multiply the texture coordinate by the texture resolution and round to an integer. \$\endgroup\$
    – DMGregory
    Commented Jan 15 at 13:26
  • \$\begingroup\$ This looks related to your previous question. Did you mean to clarify that question with an edit instead of posting a new question about the same topic? \$\endgroup\$
    – DMGregory
    Commented Jan 15 at 13:27
  • \$\begingroup\$ @DMGregory problem is I only get either (0,0) or (1,1) and nothing in between. And I didn't mean to clarify that question, I think I might have figured that one out, and I plan on answering that question if it works out, but I'm stuck at this point \$\endgroup\$ Commented Jan 15 at 14:21
  • \$\begingroup\$ Please work up a minimal verified example - folks can't do much to troubleshoot code they haven't seen. Also: the docs have a simple example of using hit.textureCoord2 to paint on has a mesh collider; you might try that with your mesh / Texture2D combo to verify they are in working order. \$\endgroup\$
    – Pikalek
    Commented Jan 15 at 15:46

1 Answer 1

0
\$\begingroup\$

Apparently I misunderstood what hit.textureCoord does. Since the mesh I was dealing with had only 4 vertices and 2 triangles (a simple plane), it can only return (0,0) or (1,1), for each of the two triangles.

In general, hit.textureCoord seems to return a vector2 associated with the triangle hit, rather than anything to do with the basemap on the material itself.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .