- Home /
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)"):
Answer by Bunny83 · Jul 12, 2016 at 08:49 PM
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.
Your whole code doesn't seem to make much sense. You generate a vector which you normalize, so you only cover a very small area of your mesh. And what's the point of "q"?
You should print out the hitpoint as well se the "vertexpos" to see their values. Also keep in $$anonymous$$d that your raycast is casted in worldspace around the world origin.
Some more context info would help. Is it a 2d or 3d game / application? What kind of mesh is in the center of your world you raycast against? A screenshot wouldn't hurt. Finally what's your goal? Your approach seem a bit strange. You only cover a single "line" around a quater circle.
The point of q is so that it doesn't print alot to the console (around 16000) so ins$$anonymous$$d I just tried took a sample somewhere on the map. There is 6 identical piece of code to this one which cover the other 5 sides of a sphere.
Finally my goal is to raycast against the sphere, get the normal distance to the middle and then color the map based on this. (I didn't want to post 300 lines of code if around 15 could explain the problem)
Your answer
![](https://koobas.hobune.stream/wayback/20220612084717im_/https://answers.unity.com/themes/thub/images/avi.jpg)