RaycastHit.textureCoord always Vector2.zero

This Code:

for (int y = 0; y < Width; y++)
{
	for (int x = 0; x < Width; x++)
	{
		RaycastHit Rayhit0;
		Vector3 vertexPos = new Vector3(x,y,0);
		if (Physics.Raycast(vertexPos.normalized * (Radius + MaxHeight),-vertexPos.normalized, out Rayhit0)) {
			q++;						
			if (q == 500) {
				Debug.Log ("Position: "+(Rayhit0.textureCoord).ToString() + " Collider Type: " + Rayhit0.collider.GetType().ToString());
			}
		}
	}
}

Always prints out (No matter what it returns “Vector2(0,0)”):

1 Answer

1

Well, is your mesh actually uv mapped? So does it have uv coordinates? Of course if not it would always return “0, 0”.

It is UV-mapped, if I assign a texture to the material that the gameObject uses, it works perfectly fine.

Please do not post a follow-up as an Answer. I converted this one for you.

Cache transforms in Start to avoid implicit 'Search' per frame. Line 49 will never select 3 (Random.Range Int is exclusive). 88 is using float syntax with the 'f'. 13 is declared but not used. Turn on the Profiler and see.