- Home /
Get pixel colour on a ray hit
I'm trying to get the pixel colour where the ray hits the texture, it's a simple black and white maze map.
I know I'm doing something wrong here, but if somebody could help I'd really appreciate it. Here's the code:
if (Physics.Raycast (ray, hit)) {
var TextureMap: Texture2D = hit.transform.renderer.material.mainTexture;
var pixelUV = hit.textureCoord;
pixelUV.x *= TextureMap.width;
pixelUV.y *= TextureMap.height;
print ( "x=" + pixelUV.x + ",y=" + pixelUV.y + " " + TextureMap.GetPixel (pixelUV.x,pixelUV.y) );
}
X and Y are both coming out as zero. the texture is RGB Compressed DXT1 - does it need to be something else ?
Answer by jonas-echterhoff · Sep 06, 2011 at 06:54 AM
What kind of collider is the Texture on? hit.textureCoord
only works on MeshColliders (as documented in the scripting reference). Other then that, I see nothing wrong with the code.
Ahah! it's a box collider. yes I get it now. Thanks Jonas
Your answer
![](https://koobas.hobune.stream/wayback/20220612202415im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RaycastHit.textureCoord ranges from (0,0) to (1,1) for each face. 2 Answers
RaycastHit.textureCoord is broken? 2 Answers
Why does my Mesh Collider RayCastHit only return a texturecoord on Unity Remote? 0 Answers
How to get Texture Name from textureCoord or RaycastHit in a Mesh with multiple materials? 0 Answers
RaycastHit.textureCoord not working on my non-Convex collider. 2 Answers