Help: "textureCoord" not giving me the results I expect

I’m trying to get the UV coordinates from the texture on one face of a standard Unity cube, and then based on those coordinates, use GetPixel on another texture.

My problem is that the UV coordinates aren’t working the way I expect. They start at (0,0) on the bottom left and go to (1,1) at the top right, correct? However, that’s not working. Instead, I get weird results. The bottom left is (1,1), the top right is also (1,1), near the center is (0.5, 0)…I just don’t understand it.

I have a cube in the inspector, a texture assigned to it, and a “SelectRegion” script. Here’s the code I’m using for getting the UV coords:

function Update () {

		if (Input.GetMouseButtonDown(0))
		{
			var hit : RaycastHit;
			var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast (ray, hit, 15))
			{
				if (hit.transform.tag == "MainCube")
				{
					var pixelUV = hit.textureCoord;}
}

I’d greatly appreciate any help you could give me! Thank you!

You need to use a mesh collider.

–Eric

Thanks. However, I think I am already using a mesh collider for the textured cube. Is it a problem with my mesh collider inspector settings, or am I missing something else? The current settings are:

Is Trigger: unchecked
Material: none
Convex: checked
Smooth Sphere Collisions: unchecked
Mesh: Cube

I tried using the standard Unity cube, replaced the box collider with a mesh collider, and it worked as expected. (Using “Debug.Log (pixelUV);”).

–Eric

Hmm…thanks for your help, but even when making a new cube, it still gives me weird UV coordinates.

However, I seem to have bypassed the problem. When I unchecked “Convex”, everything worked as expected. Not sure why, but it works!

I didn’t think about convex vs. non-convex, but that’s good to know.

–Eric

Why are you talking about colliders? UVs come from the mesh…?

Read the docs for RaycastHit.textureCoord.

–Eric